【发布时间】:2013-03-07 07:34:31
【问题描述】:
我在同一行有 2 个 div,每个 div 的宽度为 50% 和一个浮点数:左。如果用户正在从智能手机查看页面,我希望他们将一个堆叠在另一个之上。现在,即使浏览器窗口缩小到 300 像素或从智能手机查看,这些 div 仍保留在同一行。
【问题讨论】:
-
给 div 一个最小宽度,例如150像素
我在同一行有 2 个 div,每个 div 的宽度为 50% 和一个浮点数:左。如果用户正在从智能手机查看页面,我希望他们将一个堆叠在另一个之上。现在,即使浏览器窗口缩小到 300 像素或从智能手机查看,这些 div 仍保留在同一行。
【问题讨论】:
媒体查询是要走的路。我会采用移动优先的方法,并添加媒体查询以扩大设计。即,从 not 浮动的 div 开始,当屏幕空间达到一定大小时添加浮动和宽度:
<!-- HTML -->
<div class="wrapper">
<div class="column">
<p>This is column 1</p>
</div>
<div class="column">
<p>This is column 2</p>
</div>
</div>
/* CSS */
.wrapper {
/* Ensure wrapper contains the columns, even when they are floated, by
creating a new Block formatting context */
overflow: hidden;
}
.column {
/* Ensure we have some margins when the columns are collapsed, or
other content is displayed below. */
margin-bottom: 2em;
}
/* Edit the min-width condition to match your desired breakpoint. */
@media screen and (min-width: 400px) {
.wrapper .column {
width: 50%;
float: left;
}
}
【讨论】:
使用媒体查询:
@media screen and (min-width: 321px) {
#wrapper {width: 100%;}
.content {
width: 50%;
float: left;
}
}
@media screen and (max-width: 320px) {
#wrapper {width: 100%;}
.content {
width: 100%;
float: none;
}
}
【讨论】: