如何从伪元素内容中插入HTML标记:

时间:2015-02-13 13:22:14

标签: html5 css3

如何通过内容插入HTML标记::: before伪元素的属性,我正在尝试< hr>在每个< article>之前标签,到目前为止我有:

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

但这不起作用。是否有其他方法将HTML标记插入伪元素CSS?

谢谢,

2 个答案:

答案 0 :(得分:4)

您不能使用CSS伪元素的content属性注入HTML元素;但是,您可以将它们设置为display: block并且(在您的问题中似乎需要,但不是您的演示/尝试过的代码)使用heightbackground-color来模拟{{1} }}:

<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 automargin-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>之前使用此伪{ - 1}},则可以将选择器更改为:

<hr />

或者,假设<article>元素是兄弟姐妹:

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

答案 1 :(得分:1)

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