Want to override child element css property by parent element

后端 未结 3 2005
长情又很酷
长情又很酷 2021-01-04 02:05

In CSS, "child" class has its o

相关标签:
3条回答
  • 2021-01-04 02:42

    !important will override any inline background color applied, let me know if this works

    .parent > .child {
        background-color: transparent !important;
     }
    
    0 讨论(0)
  • 2021-01-04 02:51

    You could override the CSS using jQuery, although it's inelegant. So, if you have HTML

    <div class="parent">
            <div class="child">
    </div></div>
    

    and CSS:

    .parent {
        width: 100px;
        height: 100px;
        background: red;
    }
    .child {
        width: 50px;
        height: 50px;
        background: blue;
    }
    

    this jQuery will find the parent background color and apply it to the child:

    $('.child').on('click',  function(event) {
        var bg = $(this).parent().css("background-color"); // finds background color of parent
        $(this).css("background-color", bg);  //  applies background color to child
    });
    

    Here's a jsfiddle: link

    0 讨论(0)
  • 2021-01-04 02:52
    .parent .child {
        background-color: inherit !important; // can also use "transparent"
    }
    

    Use !important only if nothing else works.

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