gpt4 book ai didi

html - 3 列布局 HTML/CSS

转载 作者:技术小花猫 更新时间:2023-10-29 11:31:54 25 4
gpt4 key购买 nike

我有以下 HTML 布局:

<div class="container">
<div class="column-center">Column center</div>
<div class="column-left">Column left</div>
<div class="column-right">Column right</div>
</div>

是否有机会仅使用 CSS 在不更改 HTML 的情况下像下面的示例网格那样排列列?

----------------------------------------------------
| | | |
| Column left | Column center | Column right |
| | | |
----------------------------------------------------

最佳答案

应该这样做:

.column-left{ float: left; width: 33.333%; }
.column-right{ float: right; width: 33.333%; }
.column-center{ display: inline-block; width: 33.333%; }

DEMO

编辑

要对更多的列执行此操作,您可以构建一个非常简单的网格系统。例如,像这样的东西应该适用于五列布局:

.column {
float: left;
position: relative;
width: 20%;

/*for demo purposes only */
background: #f2f2f2;
border: 1px solid #e6e6e6;
box-sizing: border-box;
}

.column-offset-1 {
left: 20%;
}
.column-offset-2 {
left: 40%;
}
.column-offset-3 {
left: 60%;
}
.column-offset-4 {
left: 80%;
}

.column-inset-1 {
left: -20%;
}
.column-inset-2 {
left: -40%;
}
.column-inset-3 {
left: -60%;
}
.column-inset-4 {
left: -80%;
}
<div class="container">
<div class="column column-one column-offset-2">Column one</div>
<div class="column column-two column-inset-1">Column two</div>
<div class="column column-three column-offset-1">Column three</div>
<div class="column column-four column-inset-2">Column four</div>
<div class="column column-five">Column five</div>
</div>

或者,如果您足够幸运能够只支持现代浏览器,您可以使用 flexible boxes :

.container {
display: flex;
}

.column {
flex: 1;

/*for demo purposes only */
background: #f2f2f2;
border: 1px solid #e6e6e6;
box-sizing: border-box;
}

.column-one {
order: 3;
}
.column-two {
order: 1;
}
.column-three {
order: 4;
}
.column-four {
order: 2;
}
.column-five {
order: 5;
}
<div class="container">
<div class="column column-one">Column one</div>
<div class="column column-two">Column two</div>
<div class="column column-three">Column three</div>
<div class="column column-four">Column four</div>
<div class="column column-five">Column five</div>
</div>

关于html - 3 列布局 HTML/CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20566660/

25 4 0
文章推荐: css - 如何根据其中的内容自动调整
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com