Html 何时使用 <br> 换行符 vs CSS 定位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3937515/
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 04:50:52  来源:igfitidea点击:

When to use <br> line breaks vs CSS positioning?

htmlcssw3csemanticssemantic-markup

提问by brandonjp

I've often wondered about the proper use of a <br>line break. Seems that often they are incorrectly used for positioning or clearing content where CSS should instead be used.

我经常想知道如何正确使用 <br>换行符。似乎它们经常被错误地用于定位或清除应该使用 CSS 的内容。

W3schoools.org says to use <br>for blank lines, but not for creating or separatingparagraphs. Looking over W3C HTML5 spec draft, it's a little clearer that the <br>would be used when content requires a line break such as lines of an address or blank lines in poetry, where intended by the author.

W3schoools.org 说<br>用于空行,但不能用于创建或分隔段落。纵观W3C HTML5 规范草案<br>当内容需要换行符(如地址行或诗歌中的空行)时,将使用它更清楚一点,这是作者的意图。

But I'm still interested in any further clarification or input anyone else. I often find myself opting not to use <br>tags but instead just styling elements with the desired clears, margins, paddings, etc to create the space desired.

但我仍然对任何进一步的澄清或输入其他人感兴趣。我经常发现自己选择不使用<br>标签,而只是使用所需的清除、边距、填充等样式元素来创建所需的空间。

Not that it's supremely important, but here's the example that got me thinking about this where a popular ("authoritative") site used a <br>that I'm not sure is quite semantic. Here I would've just cleared the <a>from it's siblings via CSS:

并不是说它非常重要,但这里的例子让我想到了这个,一个流行的(“权威”)网站使用了一个<br>我不确定的语义。在这里,我将<a>通过 CSS 从它的兄弟姐妹中清除它:

<p>Lorem ipsum dolor sit amet, consectetur tempor laborum.</p>
<br>
<a href="#readmore">more &gt;&gt;</a>

回答by Marko

To me, linebreaks should only be used inside paragraphs to indicate a new line. Adding line-breaks between paragraphs was used back in the day, when HTML looked like Chop Suey and the semantics of the HTML document looked like someone from preschool used Dreamweaver.

对我来说,换行符只应在段落内使用以指示新行。在段落之间添加换行符是在当时使用的,当时 HTML 看起来像 Chop Suey,而 HTML 文档的语义看起来像是学龄前使用 Dreamweaver 的人。

I personally rely on margins and padding for content separation, if I have to use a <br />it means I've done something wrong. I think lines of an address are a perfect example of proper usage and I would stick to onlythose scenarios.

我个人依靠边距和填充来进行内容分离,如果我必须使用 a<br />这意味着我做错了什么。我认为,一个地址线是正确使用的一个完美的例子,我会坚持只有那些场景。

回答by Chris Cox

When the linebreak has semantic meaning within the unstyled document.

当换行符在无样式文档中具有语义含义时。

As someone said, poetry is a good example - conventionally, poetry is written with a linebreak between lines. As are addresses. It does not make sense to mark up a line of a poem or an address with a paragraph element, as these are better matches to the whole address or a stanza of the poem.

正如有人所说,诗歌就是一个很好的例子——按照惯例,诗歌是在行与行之间换行的。地址也是一样。用段落元素标记一首诗的一行或一个地址是没有意义的,因为它们与整个地址或诗的一节更好地匹配。

回答by automagic

I agree with the specification, br should be used to create new lines of text within a paragraph. Semantically it makes, sense- a paragraph is a block of text with some top or bottom margin, whereas br specifies no margin, just a newline a the same line-height / line-spacing.

我同意规范, br 应该用于在段落中创建新的文本行。从语义上讲,它是有意义的 - 一个段落是一个带有一些顶部或底部边距的文本块,而 br 指定没有边距,只是一个换行符,行高/行距相同。

回答by whostolemyhat

I use line breaks when customers may be able to edit things - it's easier if they just use the return key rather than get confused as to why spaces appear around certain elements on the page. This is almost always within text areas though, there's no reason to position anything else using <br />

当客户可以编辑内容时,我会使用换行符 - 如果他们只使用返回键而不是对页面上某些元素周围出现空格的原因感到困惑,那么会更容易。这几乎总是在文本区域内,但没有理由使用<br />

回答by orolo

My opinion:

我的看法:

<br>would be used when content requires a line break such as lines of an address or blank lines in poetry, where intended by the author.

<br>当内容需要换行符时使用,例如地址行或诗歌中的空白行,这是作者想要的。

(With that said, occasionally I use them for separating paragraphs, too) </ br>

(话虽如此,我偶尔也会用它们来分隔段落) </ br>

回答by Trufa

The break tag (that when used alone should be <br />)

中断标记(单独使用时应为<br />

Must be used to break a line not for positioning, specially since you break only single lines.

必须用于断线而不是为了定位,特别是因为您只断线。

It should have the same concept behind as you use the return key.

它应该与您使用返回键具有相同的概念。

Hope it helps.

希望能帮助到你。