是否有用于文本节点的 CSS 选择器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5688712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a CSS selector for text nodes?
提问by Rudie
What I would like to do (not in IE obviously) is:
我想做的(显然不是在 IE 中)是:
p:not(.list):last-child + :text {
margin-bottom: 10px;
}
Which would give a text node a margin. (Is that even possible?) How would I get the text node with CSS?
这会给文本节点一个边距。(这甚至可能吗?)我将如何使用 CSS 获取文本节点?
采纳答案by Jacob
Text nodes cannot have margins or any other style applied to them, so anything you need style applied to must be in an element. If you want some of the text inside of your element to be styled differently, wrap it in a span
or div
, for example.
文本节点不能应用边距或任何其他样式,因此您需要应用样式的任何内容都必须在元素中。例如,如果您希望元素内的某些文本具有不同的样式,请将其包裹在span
或 中div
。
回答by Richard JP Le Guen
You cannot target text nodes with CSS. I'm with you; I wish you could... but you can't :(
您不能使用 CSS 定位文本节点。我和你在一起; 我希望你可以......但你不能:(
If you don't wrap the text node in a <span>
like @Jacob suggests, you could instead give the surrounding element padding
as opposed to margin
:
如果您没有<span>
像@Jacob 建议的那样将文本节点包装起来,则可以改为提供周围的元素padding
,而不是margin
:
HTML
HTML
<p id="theParagraph">The text node!</p>
CSS
CSS
p#theParagraph
{
border: 1px solid red;
padding-bottom: 10px;
}
回答by AbsarAkram
In case you ONLY want to hide the text (while not hiding other sibling elements) and adding a span
or div
around the text
is not an option
如果您只想隐藏文本(而不是隐藏其他同级元素)并且添加 aspan
或div
围绕text
不是一个选项
ONLY THEN
只有那时
A workaround that may work for you is to simply set the color
for parent
to white
(or whatever background-color
parent has). Having same text color
as the background-color
will make text invisible.
可能为你工作的一种解决方法是简单地设置color
了parent
以white
(或其他background-color
家长有)。有相同的文字color
作为background-color
将使文本不可见。