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:

  • A tall and thin page would have the content stretching the full width while maintaining a proportional height.
  • 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:

  • 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.
  • 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?


    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>

    只需在LESS mixin中重新Danield的答案,以供进一步使用:

    // 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;
    }
    

    My original question for this was how to both have an element of a fixed aspect, but to fit that within a specified container exactly, which makes it a little fiddly. If you simply want an individual element to maintain its aspect ratio it is a lot easier.

    The best method I've come across is by giving an element zero height and then using percentage padding-bottom to give it height. Percentage padding is always proportional to the width of an element, and not its height, even if its top or bottom padding.

    W3C Padding Properties

    So utilising that you can give an element a percentage width to sit within a container, and then padding to specify the aspect ratio, or in other terms, the relationship between its width and height.

    .object {
        width: 80%; /* whatever width here, can be fixed no of pixels etc. */
        height: 0px;
        padding-bottom: 56.25%;
    }
    .object .content {
        position: absolute;
        top: 0px;
        left: 0px;
        height: 100%;
        width: 100%;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        padding: 40px;
    }
    

    So in the above example the object takes 80% of the container width, and then its height is 56.25% of that value. If it's width was 160px then the bottom padding, and thus the height would be 90px - a 16:9 aspect.

    The slight problem here, which may not be an issue for you, is that there is no natural space inside your new object. If you need to put some text in for example and that text needs to take it's own padding values you need to add a container inside and specify the properties in there.

    Also vw and vh units aren't supported on some older browsers, so the accepted answer to my question might not be possible for you and you might have to use something more lo-fi.

    链接地址: http://www.djcxy.com/p/89170.html

    上一篇: 调整为屏幕宽度约束比例的背景图像

    下一篇: 保持div的宽高比,但在CSS中填充屏幕宽度和高度?