How to reuse styles?

前端 未结 9 1452
孤城傲影
孤城傲影 2021-02-12 14:36

Back in the past I learned a lot about CSS but now I can\'t remember how to reuse styles.

Example:

I have some tabs with class tab and I can switch

相关标签:
9条回答
  • 2021-02-12 15:25
    .active, .tab {
       ... full style of both here
    }
    
    .active {
       ... the styles that differ from .tab
    }
    
    0 讨论(0)
  • 2021-02-12 15:28

    You could, and probably should, apply both classes to the element like so:

    <a class="tab active"></a>
    

    If you want a css rule for the specific combination of these two classes, you'd do it like so:

    .tab {
        position: relative;
        top: 0;
        left: 0;
        width: 100%;
        padding: 15px 0 15px 0;
        border: solid thin #CCC;
        text-align: center;
        font-weight: bold;
        margin-bottom: 10px;
        color: #272F42;
        cursor: pointer;
        background-color: white;
    }
    
    .active 
    {
        cursor: default;
        background-color: #FFCF75;
    }
    
    .tab.active /* no space */
    {
        /* styles for elements that are both .tab and .active */
        /* leaving .active reusable for things other than tabs */
        /* and allowing override of both .tab and .active */
    }
    

    This allows you to avoid making unnecessary copies of your style declarations... and gives you the specificity to override either of the individual classes when an element has both.

    0 讨论(0)
  • 2021-02-12 15:33

    You can apply multiple classes to an element. So you can have an element with class = "tab active" and have it contain both specs.

    0 讨论(0)
提交回复
热议问题