【发布时间】:2012-11-15 14:49:46
【问题描述】:
我正在尝试根据它们的祖先(不是父母,特别是)为一些按钮设置主题,所以...我有以下 HTML 结构
<body class="theme-a">
<section class="container">
<form class="theme-b">
<div class="button-group">
<button type="button">Button B1</button>
<button type="button">Button B2</button>
</div>
<div class="button-group">
<button type="button">Button B3</button>
<button type="button">Button B4</button>
</div>
</form>
<form>
<div class="button-group">
<button type="button">Button A1</button>
<button type="button">Button A2</button>
</div>
<div class="button-group">
<button type="button">Button A3</button>
<button type="button">Button A4</button>
</div>
</form>
</section>
</body>
嗯,如您所见,有两个主题.theme-a 和.theme-b
CSS 代码,如下所示:
.theme-a {
background: #999;
}
.theme-b {
background: #555;
}
.theme-a button {
background: #222;
}
.theme-b button {
background: #69C;
}
问题是:如果您切换主题类(A 与 B,B 与 A),您会注意到 A 主题上的按钮(与主题类有较近的祖先,保持远的样式祖先,蓝色背景而不是黑色)。
如何通过根据最近的祖先设置按钮属性的方式实现适当的特异性?
这是来自 JSfiddle 的链接:http://jsfiddle.net/XVaQT/1/
希望我解释清楚:)
谢谢
【问题讨论】:
-
特异性的问题在于它没有最近或最远的祖先或兄弟姐妹的概念。即使组合器在特异性上也没有区别。
E F同样特定于E > F。 -
@BoltClock 直系后代规则不在讨论之列 :) 正如我所说,问题与祖先有关,而不仅仅是与父母有关
标签: css inheritance css-specificity