Point one style class to another?

前端 未结 10 1355
遇见更好的自我
遇见更好的自我 2021-02-04 23:59

I have a css class like:

.foo {
  background-color: red;
}

then I have a class specified for a list:

.list1 li {
  background-c         


        
相关标签:
10条回答
  • 2021-02-05 00:36

    No. The best you can do with "native CSS" is to use a multiple selector:

    .foo, .list1 li {
       ...
    }
    

    Otherwise there are preprocessors that can help with this such as SASS.

    0 讨论(0)
  • 2021-02-05 00:37

    Inheritance is, as far as I know, not supported in CSS (2.1 at least)

    0 讨论(0)
  • 2021-02-05 00:38

    No you can't but you override it using naming differnt classes for example

    .foo {
      background-color: red;
    }
    .list1 li {
       background-color: tan;
    }
    
    class ="list1 foo"
    
    0 讨论(0)
  • 2021-02-05 00:39

    The above solutions aren't available if you don't have control over how 'foo' was defined.

    So, if a JQuery solution is acceptable, just apply the original class to all instances of the new class/context. In this case:

    $('.list li').addClass('foo')
    
    0 讨论(0)
提交回复
热议问题