如何从伪元素内容插入HTML标签: [英] How to inserted HTML tag from pseudo-element content:

查看:1431
本文介绍了如何从伪元素内容插入HTML标签:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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";
}

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

but this doesn't work. Is there some other way of inserting HTML tags into pseudo-element CSS?

谢谢,

推荐答案

您不能使用CSS伪元素的 content 属性注入HTML元素;你可以将它们设置为 display:block 和(似乎在你的问题中需要,虽然不是你的演示/尝试的代码)使用 : p>

You can't inject HTML elements using the content property of CSS pseudo-elements; you can, however, set them to display: block and (as seems to be required in your question, though not your demonstrated/attempted code) use height and background-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>

当然,您可以提供宽度,并使用自动 c> c> 为margin-left margin-right < hr />

You could, of course, supply a width, and use auto for margin-left and margin-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>

如果您不想在第一个之前使用此伪< - code>< hr /> < 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... */}

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

Or, assuming the <article> elements are siblings:

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

这篇关于如何从伪元素内容插入HTML标签:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆