3列布局HTML/CSS

eas*_*wee 66 html css

我有以下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>
Run Code Online (Sandbox Code Playgroud)

有没有机会像下面的示例网格那样排列列而不使用CSS 更改HTML

----------------------------------------------------
|              |                   |               |
| Column left  |   Column center   | Column right  |
|              |                   |               |
----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Tur*_*nip 143

这样的事情应该这样做:

.column-left{ float: left; width: 33.333%; }
.column-right{ float: right; width: 33.333%; }
.column-center{ display: inline-block; width: 33.333%; }
Run Code Online (Sandbox Code Playgroud)

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%;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

或者,如果您足够幸运能够仅支持现代浏览器,则可以使用灵活的盒子:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


jan*_*oeh 21

@easwee对于可能有相同问题的其他人来说更少:

如果您不需要IE <10的支持,则可以使用Flexbox.令人兴奋的CSS3属性不幸在几个不同版本中实现,添加供应商前缀,并获得良好的跨浏览器支持突然需要相当多的属性.

使用当前的最终标准,您将完成

.container {
    display: flex;
}

.container div {
    flex: 1;
}

.column_center {
    order: 2;
}
Run Code Online (Sandbox Code Playgroud)

而已.如果你想支持像iOS 6,Safari <6,Firefox 19或IE10这样的旧版本,那么这就开始了

.container {
    display: -webkit-box;      /* OLD - iOS 6-, Safari 3.1-6 */
    display: -moz-box;         /* OLD - Firefox 19- (buggy but mostly works) */
    display: -ms-flexbox;      /* TWEENER - IE 10 */
    display: -webkit-flex;     /* NEW - Chrome */
    display: flex;             /* NEW, Spec - Opera 12.1, Firefox 20+ */
}

.container div {
    -webkit-box-flex: 1;      /* OLD - iOS 6-, Safari 3.1-6 */
    -moz-box-flex: 1;         /* OLD - Firefox 19- */
    -webkit-flex: 1;          /* Chrome */
    -ms-flex: 1;              /* IE 10 */
    flex: 1;                  /* NEW, Spec - Opera 12.1, Firefox 20+ */
}

.column_center {
    -webkit-box-ordinal-group: 2;   /* OLD - iOS 6-, Safari 3.1-6 */
    -moz-box-ordinal-group: 2;      /* OLD - Firefox 19- */
    -ms-flex-order: 2;              /* TWEENER - IE 10 */
    -webkit-order: 2;               /* NEW - Chrome */
    order: 2;                       /* NEW, Spec - Opera 12.1, Firefox 20+ */
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

这是一篇关于Flexbox跨浏览器支持的优秀文章:使用Flexbox:混合新旧

  • +1要使用简单版本,可以使用[-prefix-free](http://leaverou.github.io/prefixfree/). (2认同)

Bip*_*Pal 5

.container{
height:100px;
width:500px;
border:2px dotted #F00;
border-left:none;
border-right:none;
text-align:center;
}
.container div{
display: inline-block;
border-left: 2px dotted #ccc;
vertical-align: middle;
line-height: 100px;
} 
 .column-left{ float: left; width: 32%;  height:100px;}
.column-right{ float: right; width: 32%;  height:100px; border-right: 2px dotted #ccc;}
.column-center{ display: inline-block; width: 33%; height:100px;}

 <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> 
Run Code Online (Sandbox Code Playgroud)

查看此链接http://jsfiddle.net/bipin_kumar/XD8RW/2/


小智 5

CSS:

.container {
    position: relative;
    width: 500px;
}
.container div {
    height: 300px;
}

.column-left {
    width: 33%;
    left: 0;
    background: #00F;
    position: absolute;
}
.column-center {
    width: 34%;
    background: #933;
    margin-left: 33%;
    position: absolute;
}
.column-right {
    width: 33%;
    right: 0;
    position: absolute;
    background: #999;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这是演示:http : //jsfiddle.net/nyitsol/f0dv3q3z/