【问题标题】:How to inserted HTML tag from pseudo-element content: [duplicate]如何从伪元素内容中插入 HTML 标签:[重复]
【发布时间】:2015-04-14 13:19:04
【问题描述】:

如何通过内容插入 HTML 标记:::before 伪元素的属性,我正在尝试在每个

标记之前添加一个
,到目前为止我有:

article::before {
  content: "\00003C br / \00003E";
}

但这不起作用。有没有其他方法可以将 HTML 标签插入到伪元素 CSS 中?

谢谢,

【问题讨论】:

  • 您是特别想在每篇文章之前引入一个 hr 标签还是只引入一行?
  • 您不能使用 CSS 来添加伪 HTML 标签。见this

标签: html css


【解决方案1】:

您不能使用 CSS 伪元素的 content 属性注入 HTML 元素;但是,您可以将它们设置为 display: block 并且(在您的问题中似乎需要,尽管不是您演示/尝试的代码)使用 heightbackground-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-leftmargin-right 将“较短”的伪&lt;hr /&gt; 居中:

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>

如果您不想在第一个 &lt;article&gt; 之前出现这个伪&lt;hr /&gt;,您可以将选择器更改为:

article:not(:first-child)::before { /* ...CSS... */}

或者,假设 &lt;article&gt; 元素是兄弟元素:

article + article::before { /* ...CSS... */ }

【讨论】:

  • 感谢大卫,效果很好。我发现最接近复制
    标签的实际 CSS 是:article::before { content: '';显示:块;边距:1em 0;高度:1px;背景颜色:暗灰色;边框底部:1px 浅灰色实心;
【解决方案2】:

没有任何方法可以插入带有 CSS 内容的 HTML 标记。 This 答案建议使用 jQuery,如果这是一个选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-27
    • 2019-09-26
    • 2014-12-21
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多