美文网首页
设置div的宽度为100%,高度按比例

设置div的宽度为100%,高度按比例

作者: FrankFang7 | 来源:发表于2020-07-08 17:50 被阅读0次

纯css实现div按宽高比的方法

方法1:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 200px;
            padding-top: 50%;
            /* padding的值为百分比时,上下左右都是相对于页面宽度 */
            background: gold;
        }

        .box2 {
            position: relative;
            width: 100%;
            height: 0;
            padding-top: calc(100% * 9 / 16);
        }

        .embed {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="box1">
    </div>


    <div class="box2">
        <video src="movie.ogg" class="embed" controls="controls">
            您的浏览器不支持 video 标签。
        </video>
    </div>
</body>

</html>

其它方法参:https://www.zhihu.com/question/277241315/answer/1326247510?utm_source=weibo&utm_medium=social&utm_content=snapshot&utm_oi=35383228235776

相关文章

网友评论

      本文标题:设置div的宽度为100%,高度按比例

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