我有以下 HTML:
<div class="block-content no-title grey-bg">
如何将此 div 的背景设置为 #333 的颜色?
是否有某种 CSS 选择器可用于仅选择具有 gray-bg 的块内容 div?
你的问题有些奇怪。
这:
如何将此 div 的背景设置为 #333 的颜色?
表示您需要此选择器:([.grey-bg][.no-title][.block-content]
使用方括号中的一个或多个部分)。
这:
是否有某种 CSS 选择器可用于仅选择具有 gray-bg 的块内容 div?
表示你想要这个:.grey-bg.block-content
.
在 css 中,选择器之间的空格(或缺少空格)表示后代或相同的 dom 对象,即
<span class="a b">
<span class="c"></span>
<span class="b"></span>
</span>
//一些css选择器示例
.a.b{} //gets the first span (get all elements which have class .a and .b)
.a .b{} //gets the third (nested in a.b after .c - get all .b that are descendants of .a)
.b{} //gets the first and third (get all .b)
.b>.b{} //gets the third (get all direct descendants of .b which are .b)
.b>.c+.b{} //gets the third (next sibling .b of a .c which is direct descendant of a .b)