请问css如何保持元素自适应宽高比?
网友回复
有三种方式,我们以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; ...点击查看剩余70%


