如何从伪元素内容中插入HTML标记:[复制](How to inserted HTML tag from pseudo-element content: [duplicate])
这个问题在这里已有答案:
如何在内容中插入HTML标记::: before伪元素的属性,我在每个<article>标记之前尝试<hr>,到目前为止我有:
article::before { content: "\00003C br / \00003E"; }
但这不起作用。 是否有其他方法将HTML标记插入伪元素CSS?
谢谢,
This question already has an answer here:
How can an HTML tag be inserted by content: property of the ::before pseudo-element, I am trying to an <hr> before each <article> tag, so far I have:
article::before { content: "\00003C br / \00003E"; }
but this doesn't work. Is there some other way of inserting HTML tags into pseudo-element CSS?
Thanks,
最满意答案
您不能使用CSS伪元素的
content
属性注入HTML元素; 但是,您可以将它们设置为display: block
和(在您的问题中似乎需要,但不是您演示/尝试过的代码)使用height
和background-color
来模拟<hr />
:article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; }
article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; }
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article> <article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</article>
当然,你可以提供一个
width
,并使用auto
作为margin-left
和margin-right
来居中一个'较短'的伪<hr />
:article::before { content: ''; display: block; margin: 1em auto; height: 5px; width: 80%; background-color: #f00; }
article::before { content: ''; display: block; margin: 1em auto; height: 5px; width: 80%; background-color: #f00; }
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article> <article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</article>
如果您不想在第一个
<article>
之前使用此伪<hr />
,则可以将选择器更改为:article:not(:first-child)::before { /* ...CSS... */}
或者,假设
<article>
元素是兄弟姐妹:article + article::before { /* ...CSS... */ }
You can't inject HTML elements using the
content
property of CSS pseudo-elements; you can, however, set them todisplay: block
and (as seems to be required in your question, though not your demonstrated/attempted code) useheight
andbackground-color
to emulate an<hr />
:article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; }
article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; }
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article> <article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</article>
You could, of course, supply a
width
, and useauto
formargin-left
andmargin-right
to centre a 'shorter' pseudo-<hr />
:article::before { content: ''; display: block; margin: 1em auto; height: 5px; width: 80%; background-color: #f00; }
article::before { content: ''; display: block; margin: 1em auto; height: 5px; width: 80%; background-color: #f00; }
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article> <article>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</article>
If you don't want this pseudo-
<hr />
before the first<article>
you could change your selector to:article:not(:first-child)::before { /* ...CSS... */}
Or, assuming the
<article>
elements are siblings:article + article::before { /* ...CSS... */ }
相关问答
更多-
最具体的选择器优先。 这在CSS2.1中提到: 伪元素的行为与CSS中的真实元素一样,除了下面和其他地方描述的例外。 就我所知,就实际的浏览器行为而言,在所有支持:before浏览器上,这种行为是可靠的:before和:after的非替换元素,比如a ,CSS2.1确实为这些伪元素定义了行为,与替换了像img这样的元素。 这是有道理的,因为如果要生成多个这样的伪元素,浏览器将不知道它应该如何将它们放置在格式化结构中。 在以下示例中,通过特性和级联, a.inactive:before将优先,此链接的:bef ...
-
什么是:: content / :: slotted伪元素,它是如何工作的?(What is the ::content/::slotted pseudo-element and how does it work?)[2022-09-22]
::content伪元素在将来的Web Components / Shadow DOM的实现中被替换为::slotted 。 同样,在最新版本的Shadow DOM规范中 ,此伪元素定位的元素已从更改为 。 你可以在这里看到关于这个变化的相关讨论。 目前的浏览器仍然支持 和::content 。 原来的答案: 概要: ::content本质上是一种挖掘ShadowHost更深层次和风格后代的ShadowHost ,通常它们不可用于样式,因为您的CSS不知道在没 ... -
:之后::在Sass中的伪元素选择器[duplicate](:after and :before pseudo-element selectors in Sass [duplicate])[2022-06-27]
使用&符号来指定父选择器 。 SCSS语法: p { margin: 2em auto; > a { color: red; } &:before { content: ""; } &:after { content: "* * *"; } } Use ampersand to specify the parent selector. SCSS syntax: p { margin: 2e ... -
检查你的background 。 这不是有效值。 这是8个字符长,但只能有6个字符。 a { text-decoration: none; display: inline-block; color: #000; overflow: visible; } a::after { content: ''; display: block; height: 8px; width: 98%; background: #8BC8F690; <-- This is not valid. ...
-
每个h1在你的例子是first-of-type ,因为在每一个新的父计数复位div ,所以你需要使用的div:nth-of-type h1::before 。 div h1::before { position: absolute; font-size: inherit; left: 0; } div:nth-of-type(1) h1::before { content: '01'; color: #E82F3A; } div:nth-of-type(2) h ...
-
如果希望伪元素相对于文本水平居中,则将目标元素的display设置为inline-block以便宽度缩小以适合内容。 然后从伪元素和margin: auto删除绝对定位将margin: auto按预期工作。 这里的例子 .target { display: inline-block; } .target:after { content: ''; display: block; margin: auto; border-color: #EAB920 tra ...
-
display:block; position:relative; 对于可以帮助你。 DEMO display:block; position:relative; for can help to you. DEMO
-
使用jQuery [duplicate]修改:after伪元素的CSS(Modify CSS of the :after pseudo-element using jQuery [duplicate])[2022-07-06]
你不能用jquery选择伪元素,例如:before和:after。 但在你的情况下,你可以做一个解决方法,在:after之后使用父li的样式,从而匹配border-color属性 codepen CSS更新: ul li { width: 300px; height: 30px; border: 1px dashed; position: relative; } li:first-of-type.active { border-left-color: green; // ... -
如何从伪元素内容中插入HTML标记:[复制](How to inserted HTML tag from pseudo-element content: [duplicate])[2022-06-30]
您不能使用CSS伪元素的content属性注入HTML元素; 但是,您可以将它们设置为display: block和(在您的问题中似乎需要,但不是您演示/尝试过的代码)使用height和background-color来模拟
: article::before { content: ''; display: block; margin: 1em 0; height: 5px; background-color: #f00; } article::befo ... -
动画的伪元素堆叠(pseudo-element stacking for animations)[2022-09-22]
将其z-index设置为-1 ,你应该很好。 .button { width: 50px; height: 50px; background: lightblue; position: absolute; top: 50px; left: 50px; } i.ripple:before { content: ""; position: absolute; border-radius: 50%; width: 30px; height: 30px; back ...