[英]Is it possible to only target content between ::before and ::after when using CSS?
I do not have access to the JavaScript or HTML, only the CSS. 我无权访问JavaScript或HTML,只有CSS。
I am trying to change the text that is already there, to something else. 我正在尝试将已经存在的文本更改为其他内容。
#element::before { content: "This is the new text." }
<div id="element">This is the original text.</div>
Is it possible to target the old text specifically with CSS, to then maybe apply text-indent: -9999px;
是否可以使用CSS专门定位旧文本,然后应用
text-indent: -9999px;
? ?
It is not possible to specifically target the text node of an element; 不可能专门针对元素的文本节点; see Is there a CSS selector for text nodes?
请参阅是否有用于文本节点的CSS选择器? for more information.
欲获得更多信息。
You could achieve the effect you are after in a variety of ways, although as Paulie_D suggests, whether this is a good idea depends on what you are trying to achieve. 尽管正如Paulie_D所建议的那样,您可以通过多种方式获得想要的效果,但这是否是一个好主意取决于您要实现的目标。
#element { visibility: hidden; } #element:before { content:"My new text using visibilty."; visibility: visible; } #element2 { font-size: 0; } #element2:before { content:"My new text using font-size."; font-size: 16px; }
<div id="element">This is the original text.</div> <div id="element2">This is the original text.</div>
The short answer is: no, you cannot use CSS to target the text within the element independently of the content of the pseudo elements. 简短的答案是:不,您不能使用CSS来定位元素内的文本,而与伪元素的内容无关。
However, you can create a work around by pasting the content of the pseudo element over the original content and thus hide it. 但是,您可以通过将伪元素的内容粘贴到原始内容上然后隐藏它来创建解决方案。
Note: This approach uses absolute positioning so it may created issues if the pseudo-element content is excessively long. 注意:此方法使用绝对定位,因此如果伪元素内容过长,可能会产生问题。
Better Approach: An alternative approach is to use the visibility
property to hide the main content of the element but make visible the content from the pseudo-elements (credit to Hidden-Hobbes ). 更好的方法:一种替代方法是使用
visibility
属性隐藏元素的主要内容,但使这些内容在伪元素中可见(贷记为Hidden-Hobbes )。
#element::before { content: "My new text."; display: block; color: black; position: absolute; background-color: yellow; width: 100%; } #element { color: red; position: relative; } #element-alt::before { content: "My new alt-text."; border: 1px dotted blue; visibility: visible; display: block; } #element-alt { border: 1px dotted gray; margin-top: 30px; visibility: hidden; }
<div id="element">This is the original text.</div> <div id="element-alt">This is the original text.</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.