有三种方式,我们以iframe元素为例,三种方式代码如下:
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/reset.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300;700&display=swap');
.padding-hack {
position: relative;
width: 31vw;
padding-top: 56.25%;
/* calculating 16/9 ratio --- 9 / 16 * 100% = 56.25% */
}
.padding-hack iframe{
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
.vw-solution{
width: 31vw;
height: 17.4375vw;
/* calculating 16/9 ratio ---- 31vw / 16 * 9 = 17.4375vw */
}
.aspect-ratio {
width: 31vw;
aspect-ratio: 16 / 9;
/* aspect-ratio auto calculating 16/9 ratio */
}
html{font-family: 'Open Sans Condensed', sans-serif;}
body{
margin:5vh 0;
background-color: #333;
color: #fff;
background-image: repeating-linear-gradient( 45deg, #222, #333 2px, #222 4px);
}
h1{
max-width:95vw;
margin:30px auto;
font-size: clamp(1rem, 2vw, 2rem);
font-weight: bold;
}
h1 span{font-weight:normal;}
h2{font-size:clamp(1rem, 1.5vw, 2rem); padding:1vw 0.8vw; background-color:#fff;}
ul{
display:flex;
gap:1vw;
max-width:95vw;
margin:0 auto;
color:#333;
}
li{background-color:#dedede; border-radius:20px; overflow:hidden;}
iframe{display:block;}
code{
display: block;
padding: 20px;
white-space: pre-wrap;
font-family:monospace, arial;
font-size: clamp(0.7rem, 1vw , 0.85rem);
max-width: 31vw;
}
*{box-sizing:border-box;}
</style>
</head>
<body translate="no" >
<h1>Common CSS ways to do Aspect Ratio <span>(on the YouTube Iframe)</span></h1>
<ul>
<li>
<h2>Padding Hack</h2>
<div class="padding-hack">
<iframe src="//www.bfw.wiki" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<code>
.padding-hack {
position: relative;
width: 31vw;
padding-top: 56.25%;
/* calculating 16/9 ratio --- 9 / 16 * 100% = 56.25% */
}
.padding-hack iframe {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
}
</code>
</li>
<li class="">
<h2>Viewport width solution</h2>
<iframe class="vw-solution" src="//www.bfw.wiki" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<code>
.vw-solution {
width: 31vw;
height: 17.4375vw;
/* calculating 16/9 ratio ---- 31vw / 16 * 9 = 17.4375vw */
}
</code>
</li>
<li>
<h2>Aspect-ratio new CSS property</h2>
<iframe class="aspect-ratio" src="//www.bfw.wiki" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<code>
.aspect-ratio {
width: 31vw;
aspect-ratio: 16 / 9;
/* aspect-ratio auto calculating 16/9 ratio */
}
</code>
</li>
</ul>
</body>
</html>
网友回复
python如何实现torrent的服务端进行文件分发p2p下载?
如何在浏览器中录制摄像头和麦克风数据为mp4视频保存下载本地?
go如何编写一个类似docker的linux的虚拟容器?
python如何写一个bittorrent的种子下载客户端?
ai能通过看一个网页的交互过程视频自主模仿复制网页编写代码吗?
ai先写功能代码通过chrome mcp来进行测试功能最后ai美化页面这个流程能行吗?
vue在手机端上下拖拽元素的时候如何禁止父元素及body的滚动导致无法拖拽完成?
使用tailwindcss如何去掉响应式自适应?
有没有直接在浏览器中运行的离线linux系统?
nginx如何保留post或get数据进行url重定向?