Html 使用 <div> 间隔符是不好的做法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5183731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:05:16  来源:igfitidea点击:

Is using <div> spacers a bad practice?

htmlcss

提问by Emanuil Rusev

Is it a bad practice to use <div>tags to express gaps between elements? If yes - why is it so?

使用<div>标签来表达元素之间的差距是一种不好的做法吗?如果是 - 为什么会这样?

Here's an example:

下面是一个例子:

<div class="panel">
    <!-- Content -->
</div>

<div class="spacer"></div>

<div class="panel">
    <!-- Content -->
</div>

The CSS:

CSS:

div.spacer
{
    font-size: 0; 
    height: 10px;
    line-height: 0;
}

采纳答案by Aaron Digulla

It's bad if you could add a margin to contentinstead. Otherwise, it's the best HTML element for spacers.

如果您可以为其添加边距,那就太糟糕了content。否则,它是间隔符的最佳 HTML 元素。

回答by benhowdle89

Yes, its bad — unnecessary markup. Use margin-top/margin-bottom instead.

是的,它很糟糕——不必要的标记。使用 margin-top/margin-bottom 代替。

回答by Sean Vieira

It is unsemantic and unnecessary.

这是没有语义和不必要的。

By adding:

通过添加:

padding-bottom: 10px;

or

或者

margin-bottom: 10px;

to your div.panelCSS declaration you can save yourself multiple characters (thus reducing bandwidth costs) andhave cleaner code that expresses whatyou mean not howyou want it to look.

你的div.panelCSS声明,你可以节省自己的多个字符(从而降低带宽成本)更简洁的代码表示什么,你的意思是不是怎么样,你希望它看起来。

回答by ChrisBenyamin

It's not a good coding style for web stuff.

对于网络内容来说,这不是一种好的编码风格。

All (block) elements contain everything you need for create paddings/layout. Like the other ppl mentioned: use margins, paddings etc - the box model.

所有(块)元素都包含创建填充/布局所需的一切。就像提到的其他人一样:使用边距、填充等 - 盒子模型。

Too much of markup = more problems positioning something = needless markup.

太多的标记 = 更多定位问题 = 不必要的标记。