美文网首页CSS
CSS:利用padding来设置具有固定宽高比的盒子

CSS:利用padding来设置具有固定宽高比的盒子

作者: 王策北 | 来源:发表于2018-10-23 00:29 被阅读0次

    问题需要总结,知识需要沉淀

    实现效果:固定宽高比的盒子

    CSS特性:padding-bottom: 56.25%;//设置宽高比为16:9的盒子

    依据原理:padding-bottom 规定应该从父元素的padding-bottom继承;但是如果padding-bottom的值设置为百分比,则这个百分比是相对于父元素的宽度而言的。

    Sample code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>padding demo</title>
        <style type="text/css" charset="utf-8">
            .contain {
                position: relative;
                width: 100%;
                background-color: black;
            }
            .box {
                width: 100%;
                padding-bottom: 56.25%;
                background-color: white;
            }
            .content {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: hsla(120, 50%, 50%, .5);
            }
        </style>
    </head>
    <body>
        <div class="contain">
            <div class="box">
                <div class="content">
                    
                </div>
            </div>
        </div>
    </body>
    </html>
    

    相关文章

      网友评论

        本文标题:CSS:利用padding来设置具有固定宽高比的盒子

        本文链接:https://www.haomeiwen.com/subject/gaanzftx.html