CSS 嵌套类选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3119411/
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-29 22:17:35  来源:igfitidea点击:

Nested classes selectors

css

提问by Alex

If I have something like this in my HTML

如果我的 HTML 中有这样的东西

    <div id="top">
        <div class="txt">
            <span class="welcome">Welcome on my website</span>
            <span class="links"><a href="Home">Home</a></span>
        </div>
    </div>

How I can select the welcome class in my CSS.

如何在我的 CSS 中选择欢迎类。

I've tried #top.txt.welcome but doesn't work. I've also tried #top.txt span.welcome.

我试过#top.txt.welcome 但没有用。我也试过#top.txt span.welcome。

回答by greg0ire

#top .txtis not #top.txtthe latter means that the matched element has the id AND the class, while the former means that the matched element has the class, and one of its ancestors element has the id

#top .txt不是#top.txt后者意味着匹配的元素具有 id 和类,而前者意味着匹配的元素具有类,并且其祖先元素之一具有 id

回答by Kubain

You can use

您可以使用


span.welcome
#top .welcome
#top div.txt span.welcome
.welcome

回答by Rony

.welcome

#top div.txt .welcome

div#top div.txt span.welcome

it depends on how specific you want to be

这取决于你想成为多么具体

回答by theorise

#top .txt .welcome{}

回答by DONGA AKSHAY

If you want to use that 'welcome' class for perticular span tag than you can use two ways like...

如果您想将那个“欢迎”类用于特定的跨度标签,那么您可以使用两种方法,例如...

.txt span.welcome

.txt span.welcome

or

或者

span.welcome

跨度欢迎

it will works for your CSS class in your code perfectly.

它将完美地适用于您代码中的 CSS 类。

this is the concept of nesting class you can directly refer from internet sources too.

这是嵌套类的概念,您也可以直接从互联网资源中引用。

回答by Jim Lamb

div#top div.txt span.welcome

or

或者

#top div.txt .welcome

or some other variation thereof...

或其他一些变体...