class overrule when two classes assigned to one div

前端 未结 5 584
情话喂你
情话喂你 2020-12-13 01:45

I was creating a

tag in which I wanted to apply two classes for a
tag which would be a thumbnail gallery. One class for its
5条回答
  •  青春惊慌失措
    2020-12-13 01:53

    Many classes can be assigned to an element, you just separate them with a space

    Because of the cascade in CSS, the overwriting rules closest the to bottom of the document will be applied to the element.

    So if you have

    .myClass
    {
        background: white;
        color: blue;
    }
    
    .keepOnClassing
    {
        color: red;
    }
    

    The red color will be used, but not the background color as it was not overwritten.

    You must also take into account CSS specificity, if you have a more specific selector, this one will be used:

    .myClass
    {
        background: white;
        color: blue;
    }
    
    div.myClass.keepOnClassing
    {
        background: purple;
        color: red;
    }
    
    .stayClassySanDiego
    {
        background: black;
    }
    

    The second selector here will be used as it is more specific.

    You can take a look at it all here.

提交回复
热议问题