在HTML/CSS中做列的最佳方法

mut*_*y91 42 html css

我正在寻找一种显示3列内容的方法.我找到了一种显示环绕列的方法,但我不希望这个页面.我正在寻找一种说法

<column>
<!-- content -->
</column>
Run Code Online (Sandbox Code Playgroud)

3次,并且彼此相邻显示3列.我手边的最好例子是The Verge(http://www.theverge.com/).做这个的最好方式是什么?

AbS*_*on8 40

我建议你使用<table>或CSS.

CSS更适合更灵活.一个例子是:

<!-- of course, you should move the inline CSS style to your stylesheet -->
<!-- main container, width = 70% of page, centered -->
<div id="contentBox" style="margin:0px auto; width:70%">

 <!-- columns divs, float left, no margin so there is no space between column, width=1/3 -->
    <div id="column1" style="float:left; margin:0; width:33%;">
     CONTENT
    </div>

    <div id="column2" style="float:left; margin:0;width:33%;">
     CONTENT
    </div>

    <div id="column3" style="float:left; margin:0;width:33%">
     CONTENT
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

jsFiddle:http://jsfiddle.net/ndhqM/

使用float:left会使3列彼此粘在一起,从中心div"内容框"的左侧进入.

  • 我想补充一点,我发现我的个人格式问题需要Mikey的答案.未来的读者,请看Mikey的答案以及这个答案. (2认同)

Eig*_*ite 22

您应该考虑使用css3,尽管它确实包括使用供应商前缀.

我已经敲了一个快速的小提琴演示,但关键是这个.

<style>
.3col
{
    -webkit-column-count: 3;
    -webkit-column-gap: 10px;
    -moz-column-count: 3;
    -moz-column-gap: 10px;
    column-count:3;
    column-gap:10px;
}
</style>
<div class="3col">
<p>col1</p>
<p>col2</p>
<p>col3</p>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 您可能会发现在[w3schools](http://www.w3schools.com/css3/css3_multiple_columns.asp)上了解浏览器支持很有用; IE10 +.我必须等待更好的浏览器支持 (2认同)

Fra*_*ijk 9

除了3浮动列结构(我也建议),你必须插入一个clearfix来防止在columncontainer之后使用元素的布局问题(保持列容器在流中,可以这么说......).

<div id="contentBox" class="clearfix">
....
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.clearfix { zoom: 1; }
.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
.clearfix:after { clear: both; }
Run Code Online (Sandbox Code Playgroud)

  • 使用clearfix是很常见的,但是如果你有三个包围的包装器,你可以设置那个包装器(在这种情况下为#contentBox)溢出:auto;.这将使#contentBox扩展以容纳三列,无论它们的高度如何,从而将下面的内容向下推.这是一个更清晰的解决方案,在HTML中需要更少的类. (4认同)