Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

div keeping aspect ratio according to width of window

like image 974
jackb Avatar asked Sep 29 '09 23:09

jackb


People also ask

How do I keep my div aspect ratio?

In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.

How do I fix the aspect ratio in CSS?

The ratio 9/16 is ease to change, no need to predefined 100:56.25 or 100:75 . If you want to ensure height first, you should switch width and height, e.g. height:100vh;width: calc(100vh * 9 / 16) for 9:16 portrait.

How do I keep an image aspect ratio in CSS?

The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.

How do you make a div scale proportionally?

For proportional resizing purposes, it makes matters extremely simple: Define the width of an element as a percentage (eg: 100%) of the parent's width, then define the element's padding-top (or -bottom) as a percentage so that the height is the aspect ratio you need. And that's it!


1 Answers

Just create a wrapper <div> with a percentage value for padding-bottom, like this:

.demoWrapper {   padding: 10px;   background: white;   box-sizing: border-box;   resize: horizontal;   border: 1px dashed;   overflow: auto;   max-width: 100%;   height: calc(100vh - 16px); }  div {   width: 100%;   padding-bottom: 75%;   background: gold; /** <-- For the demo **/ }
<div class="demoWrapper">   <div></div> </div>

It will result in a <div> with height equal to 75% of the width of its container (a 4:3 aspect ratio).

This relies on the fact that for padding :

The percentage is calculated with respect to the width of the generated box's containing block [...] (source: w3.org, emphasis mine)

Padding-bottom values for other aspect ratios and 100% width :

aspect ratio  | padding-bottom value --------------|----------------------     16:9      |       56.25%     4:3       |       75%     3:2       |       66.66%     8:5       |       62.5% 

Placing content in the div :

In order to keep the aspect ratio of the div and prevent its content from stretching it, you need to add an absolutely positioned child and stretch it to the edges of the wrapper with:

div.stretchy-wrapper {   position: relative; }  div.stretchy-wrapper > div {   position: absolute;   top: 0; bottom: 0; left: 0; right: 0; } 

Here's a demo and another more in depth demo

like image 55
Web_Designer Avatar answered Oct 08 '22 07:10

Web_Designer