Maintain the aspect ratio of a div with CSS
I want to create a div that can change its width/height as the window's width changes.
Are there any CSS3 rules that would allow the height to change according to the width, while maintaining its aspect ratio ?
I know I can do this via JavaScript, but I would prefer using only CSS.
只需使用padding-bottom
的百分比值创建一个包装器<div>
,如下所示:
div {
width: 100%;
padding-bottom: 75%;
background: gold; /** <-- For the demo **/
}
<div></div>
vw
units:
You can use vw
units for both the width and height of the element. This allows the element's aspect ratio to be preserved, based on the viewport width.
vw : 1/100th of the width of the viewport. [MDN]
Alternatively, you can also use vh
for viewport height, or even vmin
/ vmax
to use the lesser/greater of the viewport dimensions (discussion here).
Example: 1:1 aspect ratio
div {
width: 20vw;
height: 20vw;
background: gold;
}
<div></div>
I've found a way to do this using CSS, but you have to be careful as it may change depending on the flow of your own web site. I've done it in order to embed video with a constant aspect ratio within a fluid width portion of my web site.
Say you have an embedded video like this:
<object>
<param ... /><param ... />...
<embed src="..." ...</embed>
</object>
You could then place this all inside a div with a "video" class. This video class will probably be the fluid element in your website such that, by itself, it has no direct height constraints, but when you resize the browser it will change in width according to the flow of the web site. This would be the element you are probably trying to get your embedded video in while maintaining a certain aspect ratio of the video.
In order to do this, I put an image before the embedded object within the "video" class div.
!!! The important part is that the image has the correct aspect ratio you wish to maintain. Also, make sure the size of the image is AT LEAST as big as the smallest you expect the video (or whatever you are maintaining the AR of) to get based on your layout. This will avoid any potential issues in the resolution of the image when it is percentage-resized. For example, if you wanted to maintain an aspect ratio of 3:2, don't just use a 3px by 2px image. It may work under some circumstances, but I haven't tested it, and it would probably be a good idea to avoid.
You should probably already have a minimum width like this defined for fluid elements of your web site. If not, it is a good idea to do so in order to avoid chopping elements off or having overlap when the browser window gets too small. It is better to have a scroll bar at some point. The smallest in width a web page should get is somewhere around ~600px (including any fixed width columns) because screen resolutions don't come smaller unless you are dealing with phone-friendly sites. !!!
I use a completely transparent png but I don't really think it ends up mattering if you do it right. Like this:
<div class="video">
<img class="maintainaspectratio" src="maintainaspectratio.png" />
<object>
<param ... /><param ... />...
<embed src="..." ...</embed>
</object>
</div>
Now you should be able to add CSS similar to the following:
div.video { ...; position: relative; }
div.video img.maintainaspectratio { width: 100%; }
div.video object { position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; }
div.video embed {width: 100%; height: 100%; }
Make sure you also remove any explicit height or width declaration within the object and embed tags that usually come with copy/pasted embed code.
The way it works depends on the position properties of the video class element and the item you want have maintain a certain aspect ratio. It takes advantage of the way an image will maintain its proper aspect ratio when resized in an element. It tells whatever else is in video class element to take full-advantage of the real estate provided by the dynamic image by forcing its width/height to 100% of the video class element being adjusted by the image.
Pretty cool, eh?
You might have to play around with it a bit to get it to work with your own design, but this actually works surprisingly well for me. The general concept is there.
链接地址: http://www.djcxy.com/p/75744.html上一篇: 如何在div内垂直居中子元素
下一篇: 保持div与CSS的纵横比