简体   繁体   English

如何防止子选择器失败的CSS继承?

[英]How to prevent CSS inheritance where child selectors fail?

I've read many other questions similar to this, but have found nothing covering what seems to be a peculiar issue; 我读过许多其他与此类似的问题,但没有发现任何似乎是一个奇特的问题。 albeit one I can't help thinking more than just I am subject to. 尽管我不但要思考,而且要服从于我。

Considering eg 考虑例如

<div class="magic">
    <dl>
        <dt>Foo</dt>
        <dd class="whatever">Bar
            <ul>
                <li>Baz</li>
            </ul>
        </dd>
    </dl>
</div>

Where the CSS is CSS在哪里

div.magic dd.whatever {
    text-indent: -2em;
}

I find that text-indent is inherited by the <ul> causing display issues . 我发现text-indent<ul>继承,从而导致显示问题 So I change my CSS to 所以我将CSS更改为

div.magic dl > dd.whatever { ...

and the inheritance continues unabated >.< 并且继承继续进行>。<

From what I can work out by reading the specs, the issue is that although the CSS selector does indeed specify that only the immediate child <dd> s of <dl> s within <div class="magic"> will have the text-indent applied, it doesn't explicitly stop the inheritance by other element types . 通过阅读规范可以得出的问题是,尽管CSS选择器确实确实指定了<div class="magic"><dl>的直接子元素<dd> ,但text-indent时,它不会显式停止其他元素类型的继承。

Am I simply dumb or is this impossible to fix without explicit specification that 我只是愚蠢吗?或者如果没有明确的说明,这是不可能解决的

div.magic dl > dd.whatever * {
    text-indent: 0;
}

ie Is the only fix to undo whatever inherited applications are undesired? 是唯一的撤消任何不需要的继承应用程序的解决方案吗?

Any and all assistance will be greatly appreciated; 任何帮助都将不胜感激; however, the example of a <ul> inside a <dd> isn't something I'd necessarily choose to do, but it is something that's cropped up in practice - I'm just trying to fix it (for someone else). 但是,我不一定要选择在<dd>中使用<ul>的示例,而是在实践中出现的问题-我只是在试图解决(针对其他人)。 So I humbly request a lack of responses suggesting that I rewrite the HTML. 因此,我谦虚地要求缺乏回应,表明我重写了HTML。 Cheers :-) 欢呼声:-)

EDIT: JSFIDDLE of the above example. 编辑:以上示例的JSFIDDLE Open your inspector to see what's inherited by what. 打开检查,看看有什么用什么继承。

EDIT 2: Although this was answered very quickly and I accept the answer, I'd be interested to hear what pure CSS tricks and kludges people might suggest to workaround the apparent inability to limit selection to only and strictly the targeted element, and not it's descendants no matter what they may be. 编辑2:尽管很快就回答了这个问题,而且我接受了答案,但我很想听听人们可能会建议采用什么纯CSS技巧和and俩来解决显然无法将选择仅限于目标元素的问题,而不是后裔,无论他们是什么。

Here's hoping CSS4 or CSS5 will provide a few more options. 希望CSS4或CSS5提供更多选择。

Yes, that is the concept of Cascading Style Sheets. 是的,这就是层叠样式表的概念。 Child nodes inherit styles from their parents unless they are overwritten by a more specific rule. 子节点从其父级继承样式,除非它们被更特定的规则覆盖。 These rules can either be defined by you or the browser's default style sheet. 这些规则可以由您定义,也可以由浏览器的默认样式表定义。

Take this example: http://jsfiddle.net/r7R8z/ 以以下示例为例: http : //jsfiddle.net/r7R8z/

div.magic dl > dd.whatever {
    padding-left: 20px;
    font-size: 20px;
}

Baz will be in font-size: 20px but will have a padding-left: 0 since the padding is overwritten by the browser but the font-size isn't. Baz font-size: 20pxpadding-left: 0因为该边框被浏览器覆盖,但font-size未被覆盖。

To answer your question: Yes, you will have to overwrite the text-indent property for all elements that don't have it defined by the Browser by default. 要回答您的问题:是的,默认情况下,您将必须为所有未由浏览器定义的元素覆盖text-indent属性。 Check out this answer for more info. 查看此答案以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM