我有3列布局。从桌面访问它时,它显示如下:
-------------------------------------------
| columnleft | columncenter | columnright |
-------------------------------------------
从手机/平板电脑/调整大小浏览器中查看时,我希望它是这样的:
----------------
| columnleft |
----------------
| columncenter |
----------------
| columnright |
----------------
我的示例如下,这里是JSFiddle。
<style>
.column-left{ float: left; width: 33%; }
.column-right{ float: right; width: 33%; }
.column-center{ display: inline-block; width: 33%; }
</style>
<div class="container">
<div class="column-left">Column left</div>
<div class="column-center">Column center</div>
<div class="column-right">Column right</div>
</div>
答案 0 :(得分:18)
有很多方法可以做到这一点。首先,您需要将div显示为大屏幕的列,然后使用媒体查询将其更改为中/小屏幕的行。
HTML for all:
<div class="container">
<div class="section">1</div>
<div class="section">2</div>
<div class="section">3</div>
</div>
.container {
display: flex;
}
.section {
flex: 1; /*grow*/
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
flex-direction: column;
}
}
.container:after { /*clear float*/
content: "";
display: table;
clear: both;
}
.section {
float: left;
width: 33.3333%;
border: 1px solid;
box-sizing: border-box;
}
@media (max-width: 768px) { /*breakpoint*/
.section {
float: none;
width: auto;
}
}
.container {
font-size: 0; /*remove white space*/
}
.section {
font-size: 16px; /*reset font size*/
display: inline-block;
vertical-align: top;
width: 33.3333%;
border: 1px solid;
box-sizing: border-box;
}
@media (max-width: 768px) { /*breakpoint*/
.section {
display: block;
width: auto;
}
}
.container {
display: table;
table-layout: fixed; /*euqal column width*/
width: 100%;
}
.section {
display: table-cell;
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.section {
display: block;
}
}
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /*fraction*/
}
.section {
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
grid-template-columns: none;
}
}
答案 1 :(得分:4)
它适用于你。
.column-left{ float: left; width: 33%; }
.column-right{ float: right; width: 33%; }
.column-center{ display: inline-block; width: 33%; }
@media screen and (max-width: 960px) {
.column-left{ float: none; width: 100%; }
.column-right{ float: none; width: 100%; }
.column-center{ display: block; width: 100%; }
}