search
HomeWeb Front-endHTML Tutorial【译】在行内元素前注入一个换行 | CSS-Tricks_html/css_WEB-ITnose

原文: http://www.zcfy.cc/article/517

我遇到了一个小问题,我有一个 span 在 header 中,而我想要在 span 的前面产生一个换行。郑重声明,在 span 前面插入一个
标签当然没问题(而事实上,你还可以显示/隐藏这个标签,这 非常有用 )。但是...不得不用 HTML 去做一个布局相关的事情始终感觉有点奇怪。

因此,让我们来深入探索一下,在这个探索中,我们会多次说到“然而...”。

<h1 id="Break-right-after-this-br-could-go-here-but-can-we-do-it-with-CSS-span-and-before-this-span">  Break right after this  <!-- <br> could go here, but can we do it with CSS? -->  <span>    and before this  </span></h1>

块级元素能做到

不同于 ,我们可以使用一个

,而之所以用 div 可以是因为它是一个块级元素。

但是我们有使用 span 的理由,因为设计上要求我们用 span。在换行之后的文本应该是一个行内/行内块元素,因为我们可能想给它一个背景或者 padding 或者其他什么。

你可以通过伪元素插入换行

这很容易:

h1 span::before {  content: "\A";}

然而... 是一个行内元素。换行不会产生任何效果!就像 真正的 回车换行一样不产生。

我们可以通过样式让空白符生效,来强制让换行有效...

h1.two span::before {  content: "\A";  white-space: pre;}

这样实际有效果了。然而... 由于 padding 和背景存在,它把左 padding 的部分留在了上一行:

我们可以使用 box-decoration-break: clone; 来修复这个“左边缘环绕”的问题,然而... 它会导致上一行产生更大的黑块:(此处不明白的同学可以看 这个例子 ——译者注)

box-decoration-break 对于处理一些问题来说好极了,但不包括这个问题。

如果我们将 span 变成 inline-block,那么换行将会发生在那个块里面,这也不是我们想要的效果:

将伪元素设置成块级元素,让 span 保持行内元素,这样也不行:

你可能会有点奇怪,为何不直接将实际的文本写在伪元素里

这是 Aaron Bushnell 想出的 办法 。这个技巧是让 span 成为块级元素,然后把文本通过伪元素插入进去,以伪元素作为行内元素添加样式。

h1 span {  display: block;}h1 span::before {  content: attr(data-text);  background: black;  padding: 1px 8px;}

这样可以!然而...

我一直是伪元素技巧的簇拥,但是这么用有点危险,因为你可能破坏了可访问性。我认为一些读屏软件会读伪元素,但不是所有的都会,也不是所有的都打算支持。更何况这种方式下你不能拷贝和粘贴所有的文本,尽管这些文本完整地出现在 HTML 文档里。

利用 table 布局

我最喜欢的 方式 是由 Thierry Koblentz 提出的。只需要给 span 设置 display:table; 就行了。当然这不是真正的表格数据,但没关系。通过 CSS 将元素强制用 table 布局以利用 table 布局独特的布局属性,它唯一的问题仅仅是——不语义化。

h1 span {  display: table;}

在线例子

包括使用
的那个例子,那样做也是好的。

例子: 在一个 header 元素中的行内元素前尝试插入一个换行 。

英文原文: https://css-tricks.com/injecting-line-break/

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.