Table of Contents
Break right after this and before this
块级元素能做到
你可以通过伪元素插入换行
你可能会有点奇怪,为何不直接将实际的文本写在伪元素里
利用 table 布局
在线例子
Home Web Front-end HTML Tutorial 【译】在行内元素前注入一个换行 | CSS-Tricks_html/css_WEB-ITnose

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

Jun 21, 2016 am 08:45 AM

原文: 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>
Copy after login

块级元素能做到

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

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

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

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

这很容易:

h1 span::before {  content: "\A";}
Copy after login

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

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

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

这样实际有效果了。然而... 由于 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;}
Copy after login

这样可以!然而...

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

利用 table 布局

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

h1 span {  display: table;}
Copy after login

在线例子

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

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

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

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1245
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles