我的问题(s)

这些方法是专业网页设计师的首选吗? 在绘制网站时,这些方法中有任何一种是浏览器首选的吗? 这只是个人喜好吗? 我还遗漏了其他技巧吗?

注意:以上问题是关于设计多栏布局的


浮:左;

http://jsfiddle.net/CDe6a/

这是我在创建列布局时经常使用的方法,它似乎工作得很好。但是父节点本身会崩溃,所以你只需要记住清除:both;之后。我刚刚发现的另一个缺点是无法垂直对齐文本。

显示:内联;

这似乎纠正了父节点折叠的问题,但增加了空白。

http://jsfiddle.net/CDe6a/1/

从html中删除空白似乎是解决这个问题的最简单的方法,但如果你真的对你的html很挑剔,这是不可取的。

http://jsfiddle.net/CDe6a/2/

显示:inline-block;

看起来就像display:inline;。

http://jsfiddle.net/CDe6a/3/

显示:表格单元;

http://jsfiddle.net/CDe6a/4/

完美的工作。

我的想法:

我确定我遗漏了很多东西,比如某些会破坏布局的异常,但是,display:table-cell;似乎工作得最好,我想我会开始替换float:left;因为我似乎总是搞砸清楚:两者;。我在网上读过很多关于这方面的文章和博客,但没有一篇给我一个明确的答案,告诉我在布局我的网站时应该使用什么。


当前回答

我认为这取决于你需要它做什么:

If you need it just to get 3 columns layout, I'd suggest to do it with float. If you need it for menu, you can use inline-block. For the whitespace problem, you can use few tricks as described by Chris Coyier here http://css-tricks.com/fighting-the-space-between-inline-block-elements/. If you need to make a multiple choice option, which the width needs to spread evenly inside a specified box, then I'd prefer display: table. This will not work correctly in some browsers, so it depends on your browser support.

最后,最好的方法是使用flexbox。这个规范已经改变了几次,所以它还不稳定。但一旦最终确定,这将是我认为最好的方法。

其他回答

我更喜欢内联块,但浮动仍然是将HTML元素组合在一起的有用方法,特别是当我们有元素应该坚持在左边和一个在右边时,浮动工作得更好,写更少的行,而内联块在许多其他情况下工作得很好。

I usually use float: left; and add overflow: auto; to solve the collapsing parent problem (as to why this works, overflow: auto will expand the parent instead of adding scrollbars if you do not give it explicit height, overflow: hidden works as well). Most of the vertical alignment needs I had are for one-line of text in menu bars, which can be solved using line-height property. If I really need to vertical align a block element, I'd set an explicit height on the parent and the vertically aligned item, position absolute, top 50%, and negative margin.

我不使用display: table-cell的原因是,当你的条目超过了站点的宽度时,它会溢出。Table-cell将强制用户水平滚动,而float将包装溢出菜单,使其在不需要水平滚动的情况下仍然可用。

关于float: left和overflow: auto最好的一点是,它可以一直工作到IE6,而不需要任何修改,甚至可能更久。

为了补充Spudley的答案,如果你知道列的宽度,也可以使用绝对位置和边距。

对我来说,选择方法时的主要问题是是否需要列填充整个高度(等高),其中table-cell是最简单的方法(如果您不太关心旧的浏览器)。

我认为这取决于你需要它做什么:

If you need it just to get 3 columns layout, I'd suggest to do it with float. If you need it for menu, you can use inline-block. For the whitespace problem, you can use few tricks as described by Chris Coyier here http://css-tricks.com/fighting-the-space-between-inline-block-elements/. If you need to make a multiple choice option, which the width needs to spread evenly inside a specified box, then I'd prefer display: table. This will not work correctly in some browsers, so it depends on your browser support.

最后,最好的方法是使用flexbox。这个规范已经改变了几次,所以它还不稳定。但一旦最终确定,这将是我认为最好的方法。

我更喜欢内联块,尽管浮动也很有用。旧的ie不能正确渲染表格单元格(inline-block也不行,但是有缩放:1;*显示:内联黑客,我经常使用)。如果你的子节点的高度小于父节点的高度,浮动会把它们带到顶部,而内联块有时会搞砸。

大多数情况下,浏览器会正确地解释所有内容,当然,除非它是IE。你总是要检查以确保IE不会糟糕——例如,表格单元格的概念。

在所有现实中,是的,这归结为个人偏好。

一种可以用来消除空白的技术是将字体大小设置为0给父元素,然后将字体大小返回给子元素,尽管这很麻烦,而且很恶心。