How to combine class and ID in CSS selector?

2019-01-02 20:20发布

If I have the following div:

<div class="sectionA" id="content">
    Lorem Ipsum...
</div>

Is there a way to define a style that expresses the idea "A div with id='content' AND class='myClass'"?

Or do you have simply go one way or the other as in

<div class="content-sectionA">
    Lorem Ipsum...
</div>

Or

<div id="content-sectionA">
    Lorem Ipsum...
</div>

9条回答
听够珍惜
2楼-- · 2019-01-02 20:41

use: tag.id ; tag#classin the tag variables in your css.

查看更多
看风景的人
3楼-- · 2019-01-02 20:44

Well generally you shouldn't need to classify an element specified by id, because id is always unique, but if you really need to, the following should work:

div#content.sectionA {
    /* ... */
}
查看更多
泪湿衣
4楼-- · 2019-01-02 20:46

You can combine ID and Class in CSS, but IDs are intended to be unique, so adding a class to a CSS selector would over-qualify it.

查看更多
登录 后发表回答