Html 跨 div 和 span 更改字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7694852/
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
change font color across divs and spans
提问by Uno Mein Ame
is it possible to change font color across several divs and spans with css?
是否可以使用 css 跨多个 div 和跨度更改字体颜色?
For example, I have HTML that goes like this:
例如,我有这样的 HTML:
some text ### some text
<div> some text in div</div>
some text
<div> some text
<span>some text</span>
</div>
some ### more text
Is there a way to change the color of the text to, let's say, red between the ### marks?
有没有办法将文本的颜色更改为 ### 标记之间的红色?
PS. The question is not about scripting, the question about the resulting markup/css itself.
附注。问题不是关于脚本,而是关于结果标记/css 本身的问题。
回答by Zach Rattner
I'm not exactly sure what you're asking, so let's take the opportunity to go over some CSS basics:
我不太确定你在问什么,所以让我们借此机会回顾一些 CSS 基础知识:
This CSS will apply to all div
tags:
此 CSS 将适用于所有div
标签:
div
{
color: red;
}
This CSS will apply to all span
tags inside a div
:
此 CSS 将应用于 a 中的所有span
标签div
:
div span
{
color: red;
}
This CSS will apply to all div
tags and all span
tags:
此 CSS 将适用于所有div
标签和所有span
标签:
div, span
{
color: red;
}
If you want a CSS rule to apply to a certain section of code regardless of the elements in it, then you can put it in a wrapper, like so:
如果您希望 CSS 规则适用于代码的特定部分,而不管其中的元素如何,那么您可以将其放入包装器中,如下所示:
<div class="Wrapper">
<!-- Lots of different tags in here -->
</div>
Then, you can apply this CSS rule to match all tags of any type inside a div
with class Wrapper
:
然后,您可以应用此 CSS 规则来匹配div
with class中任何类型的所有标签Wrapper
:
div.Wrapper *
{
color: red;
}
回答by Mahesh
Always wrap the text like in your example in span
tag. Don't throw the text like that. With that said, in general you can specify in .css file color
property for span
.
始终将文本像在您的示例中一样包装在span
标签中。不要这样扔文本。话虽如此,通常您可以在 .css 文件color
属性中为span
.
span{ color: Red }
But if you want specific span in a div to have a specific color then -
但是,如果您希望 div 中的特定跨度具有特定颜色,那么 -
<div class="someClass">
<span>some text</span>
</div>
In .css,
在.css中,
.someClass { /* properties for the div */ }
.someClass span { color: Blue; /* other font properties */ }