HTML 和 CSS 的空格和新行

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

Spaces and new lines with HTML and CSS

htmlcss

提问by ajsie

Should I use <br />and &nbsp;in HTML to position elements, or should I just use CSS display:inline with padding/margin for positioning and all styling? what are pros and cons with both?

我应该在 HTML 中使用<br />&nbsp;来定位元素,还是应该使用 CSS display:inline with padding/margin 来定位和所有样式?两者的优缺点是什么?

回答by BalusC

Use <br>to represent a linebreak inside a block element and use &nbsp;to represent a non-breaking space inside an inline element. Nothing more. For the remnant just use CSS the smart way with help of under each the display, float, paddingand/or marginproperties.

使用<br>以表示块元件和使用内部断行&nbsp;表示一条内联元件内的非间断的空间。而已。对于剩余的只是使用CSS与下每个帮助聪明的办法displayfloatpadding和/或margin性能。

回答by Robert Harvey

<br />has its uses, but if you find yourself using &nbspa lot, I would consider finding better ways to align things. &nbsp;is just ugly and clunky.

<br />有它的用途,但如果你发现自己使用&nbsp很多,我会考虑找到更好的方法来调整事物。 &nbsp;只是丑陋而笨重。

If it's tabular data, use a table. Your life will be much happier.

如果是表格数据,请使用表格。你的生活会幸福很多。

If it's not tabular data, use css, as BalusC suggests.

如果它不是表格数据,请使用 css,正如 BalusC 建议的那样。

回答by Januz

Ideally you should position everything with css, and only use <br />(line break) and &nbsp;inside <p>s. But this isn't an ideal world ;)

理想情况下,您应该使用 css 定位所有内容,并且只使用<br />(line break) 和&nbsp;inside <p>s。但这不是一个理想的世界;)

回答by prodigitalson

<br />Can really go either way. But if you find yourself using it to adjsut paragraph spacing orsomething like this then you really have to ask yourself "is there a reason why im using breaks instead of applying a class to adjust margins?" &nbsp;on the other rarely if ever makes any kind of sense outside of a paragraph (<p />) and half the time theres not much use for it ther any how as using text-indentis preferable for indenting the first paragraph and much to the chigirn of Editors everywhere im completely opposed to the double space prepending of senetences on the web - as far as im concerned that is a print only thing.

<br />真的可以走哪条路。但是,如果您发现自己使用它来调整段落间距或类似的东西,那么您真的必须问自己“我是否有理由使用中断而不是应用类来调整边距?” &nbsp;另一方面很少,如果让任何一个段落(感之外的<p />),另一半的时间孤单没多大用它疗法任意如何为使用text-indent最好缩进第一款和多少编辑的chigirn到处IM完全反对到网络上 senetences 的双重空间 - 就我而言,这只是印刷品。

回答by CodeWizard

In HTML5 you also have the new http://html5doctor.com/element-index/#w

在 HTML5 中,您还有新的 http://html5doctor.com/element-index/#w

the answer is not black and white, it depends on your content, sometimes it should be
and in some cases the   so the content will be on a single line.

答案不是非黑即白,这取决于您的内容,有时应该如此
,在某些情况下,因此内容将在一行上。

if you want it as block you can use

如果你想要它作为块,你可以使用

回答by RookVenkatesan

Using CSS margin and padding will give you greater flexibility to make adjustments later on.

使用 CSS 边距和填充将为您提供更大的灵活性,以便稍后进行调整。