1,方案一:响应式来做,可以根据媒体查询,设定在不同屏幕宽度下div的高度和宽度,具体的设置看你响应式想怎么显示
@media only screen and (min-width: 100px) and (max-width: 640px) { div{ width: 100px; height: 100px; } } @media only screen and (min-width: 641px) and (max-width: 789px) { div{ width: 200px; height: 200px; } }
2.方案二:百分比来做,这里需要运用一个小技巧,padding用百分比做单位时是相对于容器的宽度来算的,所以你可以把高度设为0,内容自然溢出,设置个
padding-bottom
height:0;
width:50%;
padding-bottom:30%
那么这个div的高宽比就是30%:50%
常用的方法是通过背景图标的cover属性让div的高度随着宽度按比例缩放
#banner .bd li { width: 100%; background-repeat: no-repeat; background-size: cover; background-position: center 0; background-position: 50% 50%; -webkit-background-size: 100%; padding-top: 42%; }