如何创建 3 列响应式布局?
How to create a 3-column responsive layout?
我有一个 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>
它会为你工作。
.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%; }
}
有很多方法可以做到。首先,您需要使 div 在大屏幕上显示为列,然后使用媒体查询将它们更改为行以显示 medium/small 屏幕。
HTML 所有人:
<div class="container">
<div class="section">1</div>
<div class="section">2</div>
<div class="section">3</div>
</div>
1。弹性框:
.container {
display: flex;
}
.section {
flex: 1; /*grow*/
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
flex-direction: column;
}
}
2。浮动:
.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;
}
}
3。内联块:
.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;
}
}
4。 CSS table:
.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;
}
}
5。 CSS 网格:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /*fraction*/
}
.section {
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
grid-template-columns: none;
}
}
我有一个 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>
它会为你工作。
.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%; }
}
有很多方法可以做到。首先,您需要使 div 在大屏幕上显示为列,然后使用媒体查询将它们更改为行以显示 medium/small 屏幕。
HTML 所有人:
<div class="container">
<div class="section">1</div>
<div class="section">2</div>
<div class="section">3</div>
</div>
1。弹性框:
.container {
display: flex;
}
.section {
flex: 1; /*grow*/
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
flex-direction: column;
}
}
2。浮动:
.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;
}
}
3。内联块:
.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;
}
}
4。 CSS table:
.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;
}
}
5。 CSS 网格:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /*fraction*/
}
.section {
border: 1px solid;
}
@media (max-width: 768px) { /*breakpoint*/
.container {
grid-template-columns: none;
}
}