css3 - 没明白盒子的height随width的变化这段css是怎样实现的?
伊谢尔伦
伊谢尔伦 2017-04-17 11:50:38
0
2
531

刚看到这段css代码,实现了一个1:1长宽比例并且随网页变化的盒子,没有明白它设置height的原理是什么?

.item {

width: 20%;
background-color: red;

}

.item:before {

content: '';
display: block;
padding-top: 100%;

}

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
Ty80

The pseudo element item:before is a child element of item, and its content is inserted before item.
content: '' makes the pseudo-element work, but with a content height of 0.
The percentage of padding is based on the width of the parent element.
padding-top: 100% is 100% of the item width, so it is a 1:1 relationship.

Ty80

padding-top: percent is calculated based on the ratio of the width of the containing block

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!