Inherit CSS class

后端 未结 9 1210
既然无缘
既然无缘 2020-12-09 07:45

I have one base .base{} in my CSS file with couple properties. I want to change only color and other properties to stay same. How to inherit this base class inother classe

相关标签:
9条回答
  • 2020-12-09 07:48

    You don't need to add extra two classes (button button-primary), you just use the child class (button-primary) with css and it will apply parent as well as child css class. Here is the link:

    CSS-Inheritance

    Thanks to Jacob Lichner!

    0 讨论(0)
  • 2020-12-09 07:49

    As others have already mentioned, there is no concept of OOP inheritance in CSS. But, i have always used a work around for this.

    Let's say i have two buttons, and except the background image URL, all other attributes are common. This is how i did it.

    /*button specific attributes*/
    .button1 {
        background-image: url("../Images/button1.gif");
    }
    /*button specific attributes*/
    .button2 {
        background-image: url("../Images/button2.gif");
    }
    
    /*These are the shared attributes */
    .button1, .button2 {
        cursor: pointer;
        background-repeat: no-repeat;
        width: 25px;
        height: 20px;
        border: 0;
    }
    

    Hope this helps somebody.

    0 讨论(0)
  • 2020-12-09 07:52

    Something like this:

    .base {
        width:100px;
    }
    
    div.child {
        background-color:red;
        color:blue;
    }
    
    .child {
        background-color:yellow;
    }
    
    <div class="base child">
        hello world
    </div>
    

    The background here will be red, as the css selector is more specific, as we've said it must belong to a div element too!

    see it in action here: jsFiddle

    0 讨论(0)
  • 2020-12-09 07:53

    CSS "classes" are not OOP "classes". The inheritance works the other way around.
    A DOM element can have many classes, either directly or inherited or otherwise associated, which will all be applied in order, overriding earlier defined properties:

    <div class="foo bar">
    
    .foo {
        color: blue;
        width: 200px;
    }
    
    .bar {
        color: red;
    }
    

    The div will be 200px wide and have the color red.

    You override properties of DOM elements with different classes, not properties of CSS classes. CSS "classes" are rulesets, the same way ids or tags can be used as rulesets.

    Note that the order in which the classes are applied depends on the precedence and specificity of the selector, which is a complex enough topic in itself.

    0 讨论(0)
  • 2020-12-09 07:58

    You dont inherit in css, you simply add another class to the element which overrides the values

    .base{    
      color:green;
      ...other props
    }
    
    .basealt{
       color:red;
    }
    
    <span class="base basealt"></span>
    
    0 讨论(0)
  • 2020-12-09 08:01

    You can create another class with the properties you want and add this class to your class attribute:

    .classA
    {
      margin: 0;
      text-align: left;
    }
    
    .classB
    {
      background-color: Gray;
      border: 1px solid black;
    }
    
    <div class="classA classB">My div</div>
    
    0 讨论(0)
提交回复
热议问题