Overriding properties in CSS

后端 未结 6 1726
忘了有多久
忘了有多久 2021-02-07 14:06
#iddiv span {
    display: inline-block;
    width: 190px;
}
.myclass {
    width:10px;
}

Then I have

<
相关标签:
6条回答
  • 2021-02-07 14:26

    Remember to use the keyword, !important, which functions to overwrite parent rules.

    Also you can define your "myclass" in the following way:

    #iddiv span.myclass {
        width:10px;
    }
    
    0 讨论(0)
  • 2021-02-07 14:27

    Because id+selector (#iddiv span) is more specific than a class. Either

    #iddiv span.myclass
    

    or

    #iddiv .myclass
    

    should work for this case.

    Learn more about CSS specificity here or by Googling it.

    0 讨论(0)
  • 2021-02-07 14:27

    CSS applies styles according to the specificity of the selectors

    #iddiv span is more specific than myclass. Changing it to #iddiv .myclass should fix the issue for you.

    Here's an article that goes more in depth about this : http://htmldog.com/guides/cssadvanced/specificity/

    0 讨论(0)
  • 2021-02-07 14:38

    First of all, I'd suggest you properly target your selectors, as others are suggesting.

    But when all else fails, you can use !important.

    0 讨论(0)
  • 2021-02-07 14:44

    It's not working because the first style is more specific.

    To fix it, make sure you target the second span more directly, like this

    #iddiv span.myclass

    http://jsfiddle.net/jasongennaro/5fe9A/

    0 讨论(0)
  • 2021-02-07 14:49

    You could always use the !important flag to override:

    .myclass {
        width: 10px !important;
    }
    
    0 讨论(0)
提交回复
热议问题