有一个div,高度是自适应的,现在我想要这个div的宽度和高度一致,有什么办法?
<div class="container">
<div class="box"></div>
</div>
方法一
.container {
height: 200px;
width: 200px;
background-color: blue;
}
.box{
position: relative;
width: 100px;
height: 0;
padding-bottom: 100px;
background-color: red;
}
方法二
.container {
height: 200px;
width: 200px;
background-color: blue;
}
.box {
position: relative;
width: 50%;
}
/* initial ratio of 1:1 */
.box:before {
content: "";
display: block;
padding-top: 100%;
}
/* initial ratio of 1:2 */
.box:before {
content: "";
display: block;
padding-top: 50%;
}
/* initial ratio of 2:1 */
.box:before {
content: "";
display: block;
padding-top: 200%;
}
网友评论