Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain div aspect ratio according to height

I need to maintain the width of an element as a percentage of its height. So as the height changes, the width is updated.

The opposite is achievable by using a % value for padding-top, but padding-left as a percentage will be a percentage of the width of an object, not its height.

So with markup like this:

<div class="box">   <div class="inner"></div> </div> 

I'd like to use something like this:

.box {     position: absolute;     margin-top: 50%;     bottom: 0; } .inner {     padding-left: 200%; } 

To ensure the box's aspect ratio is maintained according to it's height. The height is fluid because of it's % margin - as the window's height changes, the box's height will too.

I know how to achieve this with JavaScript, just wondering if there's a clean CSS-only solution?

like image 350
Fijjit Avatar asked Oct 18 '14 09:10

Fijjit


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 you maintain aspect ratio in CSS?

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. Alternatively, you can also use vh for viewport height, or even vmin / vmax to use the lesser/greater of the viewport dimensions (discussion here).

How do you maintain height width ratio of an image?

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.


1 Answers

You can use an image that has the desired proportions as to help with proportional sizing (images can be scaled proportionally by setting one dimension to some value and other to auto). The image does not have to be visible, but it must occupy space.

.box {    position: absolute;    bottom: 0;    left: 0;    height: 50%;  }  .size-helper {    display: block;    width: auto;    height: 100%;  }  .inner {    position: absolute;    top: 0;    bottom: 0;    left: 0;    right: 0;    background: rgba(255, 255, 153, .8);  }
<div class="box">    <img class="size-helper" src="//dummyimage.com/200x100/999/000" width="200" height="100">    <div class="inner">      1. box has fluid height<br>      2. img has 2:1 aspect ratio, 100% height, auto width, static position<br>      2.1 it thus maintains width = 200% of height<br>      2.2 it defines the dimensions of the box<br>      3. inner expands as much as box    </div>  </div>

In the above example, box, inner and helper are all same size.

like image 100
Salman A Avatar answered Oct 07 '22 17:10

Salman A