Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain aspect ratio of div but fill screen width and height in CSS?

I have a site to put together that has a fixed aspect ratio of approximately 16:9 landscape, like a video.

I want to have it centred and expand to fill the available width, and the available height, but never to grow larger on either side.

For example:

  1. A tall and thin page would have the content stretching the full width while maintaining a proportional height.
  2. A short wide page would have the content stretching the full height, with a proportional width.

There are two methods I've been looking at:

  1. Use an image with the right aspect ratio to expand a container div, but I couldn't get it to behave the same way across major browsers.
  2. Setting a proportional bottom padding, but that only works relatively to the width and ignores the height. It just keeps getting bigger with the width and displays vertical scroll bars.

I know you could do this with JS quite easily, but I'd like a pure CSS solution.

Any ideas?

like image 678
Henry Gibson Avatar asked Dec 15 '13 01:12

Henry Gibson


People also ask

How do you maintain aspect ratio in CSS?

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 resize an image while maintaining 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 fill the width of the screen?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.


2 Answers

Use the new CSS viewport units vw and vh (viewport width / viewport height)

FIDDLE

Resize vertically and horizontally and you'll see that the element will always fill the maximum viewport size without breaking the ratio and without scrollbars!

(PURE) CSS

div {     width: 100vw;      height: 56.25vw; /* height:width ratio = 9/16 = .5625  */     background: pink;     max-height: 100vh;     max-width: 177.78vh; /* 16/9 = 1.778 */     margin: auto;     position: absolute;     top:0;bottom:0; /* vertical center */     left:0;right:0; /* horizontal center */ } 

* {    margin: 0;    padding: 0;  }  div {    width: 100vw;    height: 56.25vw;    /* 100/56.25 = 1.778 */    background: pink;    max-height: 100vh;    max-width: 177.78vh;    /* 16/9 = 1.778 */    margin: auto;    position: absolute;    top: 0;    bottom: 0;    /* vertical center */    left: 0;    right: 0;    /* horizontal center */  }
<div></div>

If you want to use a maximum of say 90% width and height of the viewport: FIDDLE

* {    margin: 0;    padding: 0;  }  div {    width: 90vw;    /* 90% of viewport vidth */    height: 50.625vw;    /* ratio = 9/16 * 90 = 50.625 */    background: pink;    max-height: 90vh;    max-width: 160vh;    /* 16/9 * 90 = 160 */    margin: auto;    position: absolute;    top: 0;    bottom: 0;    left: 0;    right: 0;  }
<div></div>

Also, browser support is pretty good too: IE9+, FF, Chrome, Safari- caniuse

like image 167
Danield Avatar answered Sep 20 '22 17:09

Danield


Just reformulating Danield's answer in a LESS mixin, for further usage:

// Mixin for ratio dimensions     .viewportRatio(@x, @y) {   width: 100vw;   height: @y * 100vw / @x;   max-width: @x / @y * 100vh;   max-height: 100vh; }  div {    // Force a ratio of 5:1 for all <div>   .viewportRatio(5, 1);    background-color: blue;   margin: 0;   position: absolute;   top: 0; right: 0; bottom: 0; left: 0; } 
like image 20
Christophe Marois Avatar answered Sep 22 '22 17:09

Christophe Marois