Html 使用 <BR /> 有时不好吗?

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

Is it sometimes bad to use <BR />?

htmltagsstyles

提问by pencilCake

Is it sometimes bad to use <BR/>tags?

有时使用<BR/>标签是不好的吗?

I ask because some of the first advice my development team gave me was this: Don't use <BR/>; instead, use styles. But why? Are there negative outcomes when using <BR/>tags?

我之所以这么问是因为我的开发团队给我的第一个建议是:不要使用<BR/>; 相反,使用样式。但为什么?使用<BR/>标签时是否有负面结果?

回答by Gareth

The main reason for not using <br>is that it's not semantic. If you want two items in different visual blocks, you probably want them in different logical blocks.

不使用的主要原因<br>是它不是语义的。如果您希望两个项目位于不同的可视块中,您可能希望它们位于不同的逻辑块中。

In most cases this means just using different elements, for example <p>Stuff</p><p>Other stuff</p>, and then using CSS to space the blocks out properly.

在大多数情况下,这意味着只需使用不同的元素,例如<p>Stuff</p><p>Other stuff</p>,然后使用 CSS 将块正确隔开。

There are cases where <br>is semantically valid, i.e. cases where the line break is partof the data you're sending. This is really only limited to 2 use cases - poetry and mailing addresses.

有些情况在<br>语义上是有效的,即换行符是您发送的数据的一部分。这实际上仅限于 2 个用例 - 诗歌和邮寄地址。

回答by iamkoa

I think your development team is refering to <br />in place of margin spacing. To make empty space between elements, use padding / margin styling via CSS.

我认为您的开发团队指的是<br />代替边距间距。要在元素之间留出空间,请通过 CSS 使用填充/边距样式。

Bad use of <br />:

错误使用<br />

<div>
   Content
</div>
<br />
<br />
<br />
<br />
<div>
     More content...
</div>

Good use of <br />:

善用<br />

<style>
     div {
          margin-top:10px;
     }
</style>

<div>
   Content<br />
   Line break
</div>

<div>
     More content...
</div>

回答by fengb

Generally, <br/>is an indication of poor semantic HTML. The most common case is using <br/>to declare paragraph separations, which there are much better ways to do it semantically. See Bed and BReakfast.

通常,<br/>表示 HTML 语义较差。最常见的情况是使用<br/>声明段落分隔,在语义上有更好的方法来做到这一点。见床和早餐

There are occasions where it is the proper tag to use, but it is abused often enough that people adopt a "do not use" mentality as to force better semantic thinking.

有时它是合适的标签使用,但它经常被滥用,以至于人们采取“不使用”的心态来强制更好的语义思维。

回答by Pekka

What was meant by your team was probably not to use <br>s to split between paragraphs.

您的团队的意思可能是不使用 <br> 来分割段落。

<p>I am a paragraph</p>
<p>I am a second paragraph</p>

is the better way to do that, because you can then easily adjust the spaces between paragraphs through CSS. Other than that, I can not think of anything speaking agains line breaks as such.

是更好的方法,因为您可以通过 CSS 轻松调整段落之间的空格。除此之外,我想不出有什么可以再次换行。

回答by Dieter G

Same concept applies to why we don't use tables for layout - use tables for tables and CSS for layout.

同样的概念适用于为什么我们不使用表格进行布局 - 使用表格进行表格和 CSS 进行布局。

Use <br/>for break lines in a block of text and CSS if you want to affect the layout.

<br/>如果您想影响布局,请用于文本块和 CSS 中的断行。

回答by Martin Beckett

Specifying the layout directly makes it difficult adapting the site for different page sizes or fonts for example.

例如,直接指定布局会使站点难以适应不同的页面大小或字体。

回答by BlissC

I will generally always set appropriate margins and padding on elements using CSS - it's a lot less messy than loads of <br />s all over the place apart from being more semantically correct.

我通常会始终使用 CSS 在元素上设置适当的边距和填充 -<br />除了在语义上更正确之外,它比到处都是s 的负载要少得多。

Probably the only time I would use a <br />in preference to the margins and padding set by CSS, even if it's not strictly technically correct, is if it was an isolated incident where slightly more space was needed. If I'd got quite a large stylesheet and it didn't seem worth setting up an additional style just for that one occurence, I mayuse a <br />as a one-off.

可能我唯一一次使用 a<br />优先于 CSS 设置的边距和填充,即使它在技术上不是严格正确的,也是如果它是一个需要稍微更多空间的孤立事件。如果我有一个相当大的样式表并且似乎不值得为那一次设置额外的样式,我可能会使用 a<br />作为一次性的。

Like most things, <br />s aren't a bad thing providing they're used correctly.

像大多数事情一样,<br />只要正确使用 s 并不是一件坏事。

回答by Zoe

<br />should be used for line breaks only, and not to apply style to a page. For example, if you need extra space between paragraphs, give them a class and apply the extra padding to the paragraphs. Don't spread out your paragraphs with <br /><br ><br />

<br />应该仅用于换行符,而不是将样式应用于页面。例如,如果您需要段落之间的额外空间,请给它们一个类并将额外的填充应用于段落。不要展开你的段落<br /><br ><br />

回答by ZJR

Don't use threeor more consecutive <br>s, that's a signal you're using them for stylistic purposes and no, you shouldn't.

不要使用三个或更多连续的<br>s,这是您将它们用于风格目的的信号,不,您不应该。

Some would say a single <br>is enough and instead of two you should use <p></p>, but there are situations (e.g. screenplays) in which you want to introduce a longer pause without implying a change of topic or a new period starting, like a paragraph usually does.

有些人会说一个<br>就足够了,而不是两个,您应该使用<p></p>,但是在某些情况下(例如剧本),您希望引入更长的停顿而不暗示主题的变化或新的时期开始,就像段落通常那样。

回答by BStruthers

I try to write my markup in a way that it's easily readable with CSS disabled. If you're just using BRs to add spacing, it's better to use margins and padding.

我尝试以一种在禁用 CSS 的情况下易于阅读的方式编写我的标记。如果您只是使用 BR 来添加间距,则最好使用边距和填充。