开发者

Generic CSS rule for Class and Specific for ID?

I have a class named containner that has a lot of rules common to a lot of divs but I also want to开发者_StackOverflow get some specific details to those divs. I wonder if I can do something like this:

.containner {
   common rules
}

.containner, #somedivid {
   specific to some div rule
}

Cheers


To select an element inside your container, do this

.containner #somedivid{}

A comma will just style both elements individually, the CSS I posted can be used to be quite specific on a selector, using itself and however many parents it has.

.containner, #somedivid{color: red;}

is the same as

.containner{color: red;}
#somedivid{color: red;}

If you wanted all divs inside .containner to be 100x100px but one red and one blue, you can do something like this:

.containner *{border: 1px solid black;} /* Makes ALL elements inside .containner have a black border */
.containner div{width: 100px; height: 100px;} /* Makes all DIVS inside .containner 100x100px */
.containner div#somedivid{background-color: red;} /* Specific rules with a higher priority selector */
.containner #somedivid{background-color: blue;} /* Specific rules with a lower priority selector */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜