!!!重要的是图像具有您希望保持的正确长宽比。此外,确保图像的大小至少与您期望视频(或任何您正在维护的A. R.的)根据您的布局获得的最小大小一样大。这将避免百分比调整大小时图像分辨率中的任何潜在问题。例如,如果您想保持3:2的长宽比,不要只使用3px x 2px的图像。它可能在某些情况下有效,但我还没有测试过,避免它可能是个好主意。
div {background: teal;width: 50%;aspect-ratio: 1 / 1;}
<div>aspect-ratio: 1 / 1;</div>
This is the most simple and flexible solution. It directly specifies a fixed width to height (or height to width) aspect ratio for an element. This means you can also specify an aspect ratio according to the elements height. It doesn't rely on the parent width (like the padding technique) or the viewport size (like the following vw unit technique) it relies on the element's own width or height More info on MDN. That is what make it so powerfull compared to other workarounds.
<div class='container'><div class='element'></div><!-- end of element --><div><!-- end of container -->
假设你需要保持“元素”的比率比率=>4比1或2比1…
css看起来像这样
.container{position: relative;height: 0padding-bottom : 75% /* for 4 to 3 ratio */ 25% /* for 4 to 1 ratio ..*/
}
.element{width : 100%;height: 100%;position: absolute;top : 0 ;bottom : 0 ;background : red; /* just for illustration */}
.ratio {display: grid;}.ratio > * {grid-area: 1/1;}
/* below code NOT needed for setting the ratio* I just wanted to mark it visually* and center contents*/.ratio div {border: 1px solid red;display: flex;align-items: center;justify-content: center;}
<div class="ratio"><svg viewBox="0 0 7 1"></svg><div>Fixed ratio 7:1</div></div>
.ratio {display: grid;position: relative;}.ratio > * {grid-area: 1/1;}
.ratio > div {height: 100%;overflow-y: auto;position: absolute;
/* the rest is not needed */border: 1px solid red;padding: 0 1rem;}
<div class="ratio"><svg viewBox="0 0 7 2"></svg><div><h1>Fixed ratio 7:2</h1><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. A scelerisque purus semper eget. Sem nulla pharetra diam sit amet nisl suscipit adipiscing bibendum. A cras semper auctor neque vitae tempus quam pellentesque nec. Morbi enim nunc faucibus a pellentesque sit amet porttitor. Arcu odio ut sem nulla. Sed viverra ipsum nunc aliquet bibendum enim facilisis gravida neque. Cras tincidunt lobortis feugiat vivamus at augue eget. Laoreet sit amet cursus sit amet. Amet nulla facilisi morbi tempus iaculis urna id volutpat. Leo in vitae turpis massa sed elementum tempus egestas sed. Egestas integer eget aliquet nibh. Dolor sit amet consectetur adipiscing elit.
<p>Ut aliquam purus sit amet. Eget magna fermentum iaculis eu non diam phasellus vestibulum. Diam in arcu cursus euismod quis viverra nibh. Nullam vehicula ipsum a arcu cursus vitae congue. Vel orci porta non pulvinar neque laoreet suspendisse. At tellus at urna condimentum mattis pellentesque. Tristique senectus et netus et malesuada. Vel pretium lectus quam id leo in. Interdum velit euismod in pellentesque. Velit euismod in pellentesque massa placerat duis. Vitae suscipit tellus mauris a diam maecenas sed enim.
<p>Mauris a diam maecenas sed enim ut sem. In hendrerit gravida rutrum quisque. Amet dictum sit amet justo donec enim diam. Diam vulputate ut pharetra sit amet aliquam id. Urna porttitor rhoncus dolor purus non enim praesent. Purus in massa tempor nec feugiat nisl pretium. Sagittis vitae et leo duis ut. Facilisi nullam vehicula ipsum a arcu cursus vitae congue mauris. Volutpat odio facilisis mauris sit amet massa vitae tortor condimentum. Aliquam purus sit amet luctus venenatis lectus magna. Sit amet purus gravida quis blandit turpis. Enim eu turpis egestas pretium aenean. Consequat mauris nunc congue nisi. Nunc sed id semper risus in hendrerit gravida rutrum. Ante metus dictum at tempor. Blandit massa enim nec dui nunc mattis enim ut.</div></div>
<div class="wrapper"> <!-- Just to make things a bit smaller --><p>Example of an 4:3 aspect ratio, filled by an image with an 1:1 ratio.</p><div class="image-container ratio-4-3"> <!-- Lets go for a 4:3 aspect ratio --><img src="https://placekitten.com/1000/1000/" alt="Kittens!" /></div><p>Just place other block elements around it; it will work just fine.</p></div>
.ratio {display: grid;grid-template-columns: 1fr;max-width: 200px; /* just for instance, can be 100% and depends on parent */}
.ratio::before {content: '';width: 0;padding-bottom: calc(100% / (16/9)); /* here you can place any ratio */grid-area: 1 / 1 / 1 / 1;}
.ratio>*:first-child {grid-area: 1 / 1 / 1 / 1; /* the same as ::before */background: rgba(0, 0, 0, 0.1); /* just for instance */}
.ratio {display: inline-grid;grid-template-columns: 1fr;width: 200px; /* just for instance, can be 100% and depends on parent */margin-right: 10px; /* just for instance */}
.ratio::before {content: '';width: 0;padding-bottom: calc(100% / (var(--r))); /* here you can place any ratio */grid-area: 1 / 1 / 1 / 1;}
.ratio>*:first-child {grid-area: 1 / 1 / 1 / 1; /* the same as ::before */background: rgba(0, 0, 0, 0.1); /* just for instance */}
body{background-image: linear-gradient(to top right, #FFE6B5, #B3CEBF);padding: 0;margin: 0 auto;overflow-y: hidden; /* this is to avoid scrolling when the height of the viewport is less than what the aspect ratio requires */}
.wrapper {position: relative;width: 100vw;max-height: 100vh;}.sixteen-by-nine.aspect-ratio {padding-bottom: 56.25% /* 9:16 ratio */}.content {position: absolute;top: 0; bottom: 0; left: 0; right: 0;background-color: green}
.centered {position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);height: 100%;width: min(177.8vh, 100%); /* this always keeps a 16:9 ratio within the viewport */font-size: min(3vh,1.7vw); /* if you are also interested in scaling the font size */background-color: blue}