Html 使用 br 或 div 进行分节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12717565/
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
Using br or div for section break
提问by Rob Horton
I've worked at few places and seen two different methods of doing a section or line break in HTML.
我在几个地方工作过,看到了两种不同的在 HTML 中进行分节或换行的方法。
One way I've seen it done is like this:
我看到它完成的一种方式是这样的:
<div class="placeholder-100pct"> </div>
And the other is just using plain old <br />
.
另一个只是使用普通的 old <br />
。
Any benefit to one over the other or would it be just a matter of style?
对彼此有什么好处还是只是风格问题?
回答by Steven
Use <br/>
when you want a new line in a paragraph, like so:
使用<br/>
时要在一个段落换行,就像这样:
<p>Hi Josh, <br/> How are you?</p>
This might be useful when writing an address:
这在写地址时可能很有用:
<p>John Dough<br/>
1155 Saint. St. #33<br/>
Orlando, FL 32765
</p>
Using a div will automatically give you a new line, but if you want a space between two elements, you can use a margin on the div.
使用 div 会自动给你一个新行,但如果你想要两个元素之间的空间,你可以在 div 上使用边距。
Do notuse <br/>
to get some space between two divs:
不要用于<br/>
在两个 div 之间获得一些空间:
<!-- This is not preferred -->
<div>Hello</div>
<br/>
<div>Something else here</div>
Hope this helps
希望这可以帮助
回答by Quentin
A div is a generic container. A br is a linebreak. Neither is particularly well suited for expressing a section break.
div 是一个通用容器。的BR是线断。两者都不是特别适合表达分节符。
HTML 5 introduces sections. You mark up each section, not the break between them.
HTML 5 引入了部分。您标记每个部分,而不是它们之间的中断。
<section>
<!-- This is a section -->
</section>
<section>
<!-- This is another section -->
</section>
回答by jenjenut233
Use a <br /> when it makes semantic sense to do so. If all you need is a line-break, that's what it's there for. If you're trying to split sections of different types of content, then each section should be in a container of its own. For example, if you have an address where each line of the address would show on a separate line, it would make sense to do:
当这样做具有语义意义时,请使用 <br /> 。如果您需要的只是换行符,那就是它的用途。如果您尝试拆分不同类型内容的部分,则每个部分都应位于其自己的容器中。例如,如果您有一个地址,地址的每一行都显示在单独的行上,那么这样做是有意义的:
<address>
123 Main Street<br />
Anywhere, USA 12345
</address>
回答by raina77ow
One obvious difference is that <br>
is inline element, while <div>
is not.
一个明显的区别是它<br>
是内联元素,而<div>
不是。
So this:
所以这:
<span>Some text broken into <br /> lines</span>
... is valid HTML code, while this:
... 是有效的 HTML 代码,而这个:
<span>Some text broken into <div> </div> lines</span>
... is not, as you cannot place block elements inside inline elements.
... 不是,因为您不能将块元素放置在内联元素中。
回答by Faust
<br>
has the disadvantage of limiting the size of your gap between sections to the line-height applied to or inheritted by the <span>
it sits within.
<br>
具有将部分之间的间隙大小限制为<span>
它所在的应用或继承的行高的缺点。
In other words, with <br>
, the size of the break can only everbe exactly the height of one line of text.
换句话说,对于<br>
,中断的大小只能是一行文本的高度。
Definitely wrap eachyour "sections" in their own tags, and use margins to control spacing, if you want to retain any control over the spacing. The difference is not just in the semantics of markup.
如果您想保留对间距的任何控制,请务必将每个“部分”包装在自己的标签中,并使用边距来控制间距。区别不仅仅在于标记的语义。
回答by Ben Frain
If you are looking to provide a break between two sections of content in the sense of a thematic break, then <hr>
is the element you should use.
如果您希望在主题中断的意义上在两个内容部分之间提供中断,那么<hr>
您应该使用该元素。
The hr element represents a paragraph-level thematic break, e.g., a scene change in a story, or a transition to another topic within a section of a reference book.
hr 元素代表一个段落级别的主题中断,例如,故事中的场景变化,或参考书中某一部分中另一个主题的过渡。
It's also relatively straightforward to style these as needed as they can take whatever size you require. Note however that it is a void element and cannot contain content (although you can of course add a background-image to it as needed).
根据需要设计这些样式也相对简单,因为它们可以采用您需要的任何尺寸。但是请注意,它是一个空元素并且不能包含内容(尽管您当然可以根据需要为其添加背景图像)。