css如何实现一个自适应容器


当前第2页 返回上一页

1

2

3

4

5

6

.box{

    width: 200px;

}

.text{

    padding: 10%;

}

419fa51a533fd4bd28e47da8a7498f0.png

分析:

这里我们将父容器 .box 宽度设置为 200px ,子元素 .text 的 padding:10% ,因此 .box 的 padding 计算结果为 20px ;

接下来结合主题,我们利用这个原理,来实现等比例的问题:

HTML代码:

1

2

3

4

5

<div class="box">

    <div class="text">

        <img src="http://images.pingan8787.com/2019_07_12guild_page.png" />

    </div>

</div>

css代码:

1

2

3

4

5

6

7

8

9

10

11

.box{

    width: 100%;

}

.text{

    overflow: hidden;

    height: 0;

    padding-bottom: 51.5%;

}

.box .text img{

    width: 100%;

}

这里 .text 的 padding-bottom: 51.5%; 也是按照第一个方法,用图片原始尺寸的宽高比计算出来的,需要注意,这里将 .text 设置 height: 0; 会出现高度比实际高的问题,因此为了避免这个情况,就需要设置 height: 0; 。

相关视频教程推荐:css视频教程

以上就是css如何实现一个自适应容器的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

html怎么设置button大小及颜色

css怎么引用外部ttf字体?

css列表怎么横着排列

css如何添加阴影边框(代码实例)

html和css重点难点问题

css page-break-inside属性怎么用

html代码如何让照片变模糊

css如何实现文本多行省略号

css怎样设置p标签不换行

css怎么去掉div边框

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...