如何在 HTML 中留出空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12144968/
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
How to leave space in HTML
提问by techie007
I do know that HTML is insensitive to space. But what can I use to make empty spaces between words, or phrases. I have tried <p></p>
such kind of tags, but the HTML automatically omit it.
我知道 HTML 对空间不敏感。但是我可以用什么来在单词或短语之间留出空格。我尝试过<p></p>
这种标签,但 HTML 自动忽略了它。
Can somebody give me example codes?
有人可以给我示例代码吗?
回答by techie007
You can use
, aka a Non-Breaking Space.
It is essentially a standard space, the primary difference being that a browser should not break (or wrap) a line of text at the point that this
occupies.
它本质上是一个标准空间,主要区别在于浏览器不应该在它
占据的位置中断(或换行)一行文本。
回答by Joe S
This will give you the space you're looking for.
这将为您提供您正在寻找的空间。
回答by Sampo Sarrala - codidact.org
As others already answered, $nbsp;
will output no-break space character.
Here is w3 docs for  
and others.
正如其他人已经回答的那样,$nbsp;
将输出不间断空格字符。这里是w3 文档 
和其他人。
However there is other ways to do it and nowdays i would prefer using CSS stylesheets. There is also w3c tutorials for beginners.
然而,还有其他方法可以做到这一点,现在我更喜欢使用CSS 样式表。也有适合初学者的 w3c 教程。
With CSS you can do it like this:
使用 CSS,您可以这样做:
<html>
<head>
<title>CSS test</title>
<style type="text/css">
p { word-spacing: 40px; }
</style>
</head>
<body>
<p>Hello World! Enough space between words, what do you think about it?</p>
</body>
</html>
回答by Rajesh Surana
The "pre" tag defines preformatted text. It preserves both spaces and line breaks.
“pre”标签定义了预先格式化的文本。它保留了空格和换行符。
<!DOCTYPE html>
<html>
<body>
<pre>This paragraph will be pre-formatted.
I hope this helps!
All spaces will be shown as it is in the original file.
</pre>
</body>
</html>
回答by Jukka K. Korpela
“Insensitive to space” is an oversimplification. A more accurate description is that consecutive whitespace characters (spaces, tabs, newlines) are equivalent to a single space, in normal content.
“对空间不敏感”是一种过于简单化的说法。更准确的描述是,在正常内容中,连续的空白字符(空格、制表符、换行符)等效于单个空格。
You make empty spaces between words using space characters: “hello world”. I you want more space, you should consider what you are doing, since in normal text content, that does not make sense. For spacing elements, use CSS margin
properties.
您可以使用空格字符在单词之间留出空格:“hello world”。我想要更多空间,你应该考虑你在做什么,因为在正常的文本内容中,这没有意义。对于间距元素,请使用 CSSmargin
属性。
To get useful example codes, you need to describe a specific problem, like markup and a description of desired rendering.
要获得有用的示例代码,您需要描述特定问题,例如标记和所需渲染的描述。
回答by Raktim Biswas
回答by codeGen
You can preserve white-space with white-space: pre
CSS property which will preserve white-space inside an element. https://www.w3schools.com/cssref/pr_text_white-space.asp
您可以使用white-space: pre
CSS 属性保留空白,这将保留元素内的空白。https://www.w3schools.com/cssref/pr_text_white-space.asp
回答by Tono Kuriakose
After, or in-between your text, use the
(non-breaking space) extended HTML character.
在文本之后或中间,使用
(不间断空格)扩展 HTML 字符。
EG 1 :
This is an example paragraph.
This is the next line.
EG 1:
这是一个示例段落。
这是下一行。
回答by Rajesh Surana
If you are looking for paragraph indent then you can go for 'text-indent' declaration in CSS.
如果您正在寻找段落缩进,那么您可以在 CSS 中使用“文本缩进”声明。
<!DOCTYPE html>
<html>
<head>
<style>
p { text-indent: 50px; }
</style>
</head>
<body>
<p>This paragraph will be indented by 50px. I hope this helps! Only the first line will be indented.</p>
</body>
</html>
回答by Oleg V. Volkov
Either use literal non-breaking space symbol (yes, you can copy/paste it), HTML entity, or, if you're dealing with big pre-formatted block, use white-space
CSS property.
要么使用文字不间断空格符号(是的,您可以复制/粘贴它)、HTML 实体,或者,如果您正在处理大型预格式化块,请使用white-space
CSS 属性。