Html 什么时候用 <span> 代替 <p>?

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

When to use <span> instead <p>?

html

提问by Affar

As the question indicates, if I have some text that I want to add in the HTML then when should I use <p>and when should I use <span>?

正如问题所示,如果我想在 HTML 中添加一些文本,那么我<p>应该何时使用以及何时使用<span>

采纳答案by Messy

You should keep in mind, that HTML is intended to DESCRIBE the content it contains.

您应该记住,HTML 旨在描述它所包含的内容。

So, if you wish to convey a paragraph, then do so.

所以,如果你想传达一个段落,那就去做吧。

Your comparison isn't exactly right, though. The more direct comparison would be

不过,您的比较并不完全正确。更直接的比较是

When to use a <div>instead of a <p>?

何时使用 a<div>而不是 a <p>

as both are block level elements.

因为两者都是块级元素。

A <span>is inline, much like an anchor (<a>), <strong>, emphasis (<em>), etc., so bear in mind that by it's default nature in both html and in natural writing, that a paragraph will cause a break before and after itself, like a <div>.

A<span>是内联的,很像锚点 ( <a>)、<strong>、强调 ( <em>) 等,所以请记住,在 html 和自然写作中,它的默认性质是,一个段落将在其自身前后造成中断,就像一个<div>.

Sometimes, when styling things — inline things — a <span>is great to give you something to "hook" the css to, but it is otherwise an empty tag devoid of semantic or stylistic meaning.

有时,当对事物进行样式设置时——内联事物——a<span>很适合给你一些东西来“挂钩”css,但否则它是一个没有语义或文体意义的空标签。

回答by cletus

Semantically, you use <p>tags to indicate paragraphs. <span>is used to apply CSS style and/or class(es) to an arbitrary section of text and inline elements.

在语义上,您使用<p>标签来指示段落。<span>用于将 CSS 样式和/或类应用于文本和内联元素的任意部分。

回答by dxh

The <p>tag is a paragraph, and as such, it is a blockelement (as is, for instance, h1and div), whereas spanis an inline element (as, for instance, band a)

<p>标签是paragraph,正因为如此,它是一个元素(如,例如,h1div),而span是内嵌元素(如,例如,ba

Block elements by default create some whitespace above and below themselves, and nothing can be aligned next to them, unless you set a floatattribute to them.

默认情况下,块元素会在其自身上方和下方创建一些空白,并且它们旁边没有任何东西可以对齐,除非您float为它们设置属性。

Inline elements deal with spans of text inside a paragraph. They typically have no margins, and as such, you cannot, for instance, set a widthto it.

内联元素处理段落内的文本跨度。它们通常没有边距,因此,例如,您不能为其设置 a width

回答by David Glass

A span is an inline formatting element that does NOT have a line feed above or below.

跨度是一种内联格式元素,其上方或下方没有换行符。

A p is a block element that HAS an implied line feed above and below.

p 是一个块元素,它的上方和下方都有一个隐含的换行符。

http://w3schools.com/tags/default.asp

http://w3schools.com/tags/default.asp

回答by Matt Kellogg

Semantically speaking, a p is a paragraph tag and should be used to format a paragraph of text. A span is an inline formatting change that isn't handled semantically.

从语义上讲,ap 是一个段落标记,应该用于格式化一段文本。跨度是一种不按语义处理的内联格式更改。

回答by Matt Kellogg

Span is completely non-semantic. It has no meaning, and serves merely as an element for cosmetic effects.

Span 是完全非语义的。它没有任何意义,仅用作装饰效果的元素。

Paragraphs have semantic meaning - they tell a machine (like a browser or a screen reader) that the content they encapsulate is a block of text, and has the same meaning as a paragraph of text in a book.

段落具有语义含义——它们告诉机器(如浏览器或屏幕阅读器)它们封装的内容是一段文本,与书中的一段文本具有相同的含义。

回答by Felix Kling

A practical explanation: By default, <p> </p>will add line breaks before and after the enclosed text (so it creates a paragraph). <span>does not do this, that is why it is called inline.

一个实用的解释:默认情况下,<p> </p>将在所附文本前后添加换行符(因此它创建一个段落)。<span>不这样做,这就是为什么它被称为inline

回答by statenjason

The p tag denotes a paragraph element. It has margins/padding applied to it. A span is an unstyled inline tag. An important difference is that p is a block element when span is inline, meaning that <p>Hi</p><p>There</p>would appear on different lines when <span>Hi</span><span>There</span>winds up side by side.

p 标签表示一个段落元素。它应用了边距/填充。跨度是无样式的内联标签。一个重要的区别是当 span 是内联时 p 是一个块元素,这意味着<p>Hi</p><p>There</p><span>Hi</span><span>There</span>并排缠绕时它会出现在不同的行上。

回答by suhasini

When we are using normal text at that time we want <p>tag.when we are using normal text with some effects at that time we want <span>tag

当我们使用普通文本时,我们想要<p>标签。当我们使用带有一些效果的普通文本时,我们想要<span>标签

回答by Richard H

<span> is an inline tag, a <p> is a block tag, used for paragraphs. Browsers will render a blank line below a paragraph, whereas <span>s will render on the same line.

<span> 是内联标签, <p> 是块标签,用于段落。浏览器将在段落下方呈现一个空行,而 <span> 将呈现在同一行上。