何时使用
换行 vs CSS 定位?

发布于 2024-09-28 05:05:20 字数 850 浏览 12 评论 0原文

我经常想知道如何正确使用
换行符。似乎它们经常被错误地用于定位或清除应该使用 CSS 的内容。

W3schoools.org 表示对空行使用
,但不适用于 创建或分隔段落。查看 W3C HTML5 规范草案,更清楚的是,当内容需要换行符(例如地址行或诗歌中的空行)时,将使用
由作者。

但我仍然对其他人可能有的任何进一步的澄清或意见感兴趣。我经常发现自己选择不使用
标签,而只是使用所需的空白、边距、填充等来设计元素的样式,以创建所需的空间。

并不是说它非常重要,而是这个例子让我思考这个问题,其中一个流行的(“权威”)网站使用了一个我不确定是否完全语义的
。在这里,我只是通过 CSS 从它的同级中清除

<p>Lorem ipsum dolor sit amet, consectetur tempor laborum.</p>
<br>
<a href="#readmore">more &gt;&gt;</a>

I've often wondered about the proper use of a <br> line break. Seems that often they are incorrectly used for positioning or clearing content where CSS should instead be used.

W3schoools.org says to use <br> for blank lines, but not for creating or separating paragraphs. Looking over W3C HTML5 spec draft, it's a little clearer that the <br> would be used when content requires a line break such as lines of an address or blank lines in poetry, where intended by the author.

But I'm still interested in any further clarification or input anyone else may have. I often find myself opting not to use <br> tags but instead just styling elements with the desired clears, margins, paddings, etc. to create the space desired.

Not that it's supremely important, but here's the example that got me thinking about this where a popular ("authoritative") site used a <br> that I'm not sure is quite semantic. Here I would've just cleared the <a> from it's siblings via CSS:

<p>Lorem ipsum dolor sit amet, consectetur tempor laborum.</p>
<br>
<a href="#readmore">more >></a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

荒人说梦 2024-10-05 05:05:20

对我来说,换行符只能在段落内使用来指示新行。在过去,在段落之间添加换行符已经被使用,当时 HTML 看起来像 Chop Suey,HTML 文档的语义看起来就像学前班的人使用 Dreamweaver。

我个人依靠边距和填充来分隔内容,如果我必须使用
这意味着我做错了什么。我认为地址行是正确使用的完美示例,我会坚持这些场景。

To me, linebreaks should only be used inside paragraphs to indicate a new line. Adding line-breaks between paragraphs was used back in the day, when HTML looked like Chop Suey and the semantics of the HTML document looked like someone from preschool used Dreamweaver.

I personally rely on margins and padding for content separation, if I have to use a <br /> it means I've done something wrong. I think lines of an address are a perfect example of proper usage and I would stick to only those scenarios.

听风吹 2024-10-05 05:05:20

当换行符在无样式文档中具有语义含义时。

正如有人所说,诗歌就是一个很好的例子——按照惯例,诗歌是在行与行之间换行的。地址也是如此。用段落元素标记一行诗或一个地址是没有意义的,因为它们与整个地址或诗的一节更好匹配。

When the linebreak has semantic meaning within the unstyled document.

As someone said, poetry is a good example - conventionally, poetry is written with a linebreak between lines. As are addresses. It does not make sense to mark up a line of a poem or an address with a paragraph element, as these are better matches to the whole address or a stanza of the poem.

聽兲甴掵 2024-10-05 05:05:20

我同意该规范, br 应该用于在段落中创建新的文本行。从语义上讲,它是有意义的——段落是带有一些顶部或底部边距的文本块,而 br 指定没有边距,只是一个换行符和相同的行高/行距。

I agree with the specification, br should be used to create new lines of text within a paragraph. Semantically it makes, sense- a paragraph is a block of text with some top or bottom margin, whereas br specifies no margin, just a newline a the same line-height / line-spacing.

梅倚清风 2024-10-05 05:05:20

当客户可以编辑内容时,我会使用换行符 - 如果他们只使用返回键,而不是对页面上某些元素周围出现空格的原因感到困惑,会更容易。不过,这几乎总是在文本区域内,没有理由使用
来放置其他任何内容

I use line breaks when customers may be able to edit things - it's easier if they just use the return key rather than get confused as to why spaces appear around certain elements on the page. This is almost always within text areas though, there's no reason to position anything else using <br />

烟火散人牵绊 2024-10-05 05:05:20

中断标记(单独使用时应为

必须用于中断一行而不是用于定位,特别是因为您仅中断单行。

它应该与您使用返回键具有相同的概念。

希望有帮助。

The break tag (that when used alone should be <br />)

Must be used to break a line not for positioning, specially since you break only single lines.

It should have the same concept behind as you use the return key.

Hope it helps.

缱倦旧时光 2024-10-05 05:05:20

我的意见:

当内容需要时,将使用



换行符,例如地址行
或诗歌中的空白行,其中
作者有意为之。

(话虽如此,我偶尔也会使用它们来分隔段落)

My opinion:

<br> would be used when content requires a
line break such as lines of an address
or blank lines in poetry, where
intended by the author.

(With that said, occasionally I use them for separating paragraphs, too) </ br>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文