当用户缩小浏览器窗口时,如何将2个或更多水平div堆叠成垂直div?

时间:2013-03-07 07:34:31

标签: css html layout liquid

我在同一行有2个div,每个div的宽度为50%,浮点数为left。如果用户正在从智能手机查看页面,我希望他们将一个堆叠在另一个上面。现在,即使浏览器窗口缩小到300px或从智能手机查看,div仍保持在同一行。

2 个答案:

答案 0 :(得分:2)

媒体查询是要走的路。我将采用移动优先方法,并添加媒体查询以扩展设计。即,从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;
    }
}

现场演示:http://jsfiddle.net/jPgWL/

答案 1 :(得分:1)

使用媒体查询:

@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;
  }
}