这是使用媒体查询来识别屏幕分辨率来完成的。
More info on media queries here
I've made a simple demo for you to inspect the code
HTML
<div class="center">
<div class="left"></div>
<div class="right"></div>
<div class="clear"></div>
</div>
这有一个包装器center,它在一定分辨率后将两个 div 居中。还有一个 div clear 清除两个浮动元素后的空间。
CSS
.left{width:100%; height:100px; background:green;}
.right{width:100%; height:100px; background:blue; float:left;}
.clear{clear:both;}
@media (min-width: 600px) {
.left{width:50%; height:100px; background:green; float:left;}
.right{width:50%; height:100px; background:blue; float:left;}
}
@media (min-width: 900px) {
.center{margin:0 auto; width:800px;}
}
顶部 CSS 用于最小的屏幕分辨率。之后的媒体查询,检查屏幕分辨率,当这些被识别时,CSS 变为活动状态。
因此,对于您的最小屏幕分辨率(在我的示例中,所有分辨率均低于 600 像素),您的 CSS 为:
.left{width:100%; height:100px; background:green;}
.right{width:100%; height:100px; background:blue; float:left;}
.clear{clear:both;}
然后第一个媒体查询检查屏幕分辨率是否大于600px,如果是,则添加以下类
.left{width:50%; height:100px; background:green; float:left;}
.right{width:50%; height:100px; background:blue; float:left;}
下一个媒体查询检查屏幕是否大于900px,如果是,则添加以下类
.center{margin:0 auto; width:800px;}
See the working code in a fiddle
调整右下方屏幕的大小以查看不同的查询。