是否有用于文本节点的 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:17:11  来源:igfitidea点击:

Is there a CSS selector for text nodes?

csscss-selectors

提问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 spanor 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 paddingas 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 spanor divaround the textis not an option

如果您只想隐藏文本(而不是隐藏其他同级元素)并且添加 aspandiv围绕text不是一个选项

ONLY THEN

只有那时

A workaround that may work for you is to simply set the colorfor parentto white(or whatever background-colorparent has). Having same text coloras the background-colorwill make text invisible.

可能为你工作的一种解决方法是简单地设置colorparentwhite(或其他background-color家长有)。有相同的文字color作为background-color将使文本不可见。