How do I apply a style to all children of an element

后端 未结 2 1555
北恋
北恋 2020-12-04 15:06

I have an element with class=\'myTestClass\'. How do I apply a css style to all children of this elements? I only want to apply the style to the elements childr

2条回答
  •  猫巷女王i
    2020-12-04 15:35

    As commented by David Thomas, descendants of those child elements will (likely) inherit most of the styles assigned to those child elements.

    You need to wrap your .myTestClass inside an element and apply the styles to descendants by adding .wrapper * descendant selector. Then, add .myTestClass > * child selector to apply the style to the elements children, not its grand children. For example like this:

    JSFiddle - DEMO

    .wrapper * {
        color: blue;
        margin: 0 100px; /* Only for demo */
    }
    .myTestClass > * {
        color:red;
        margin: 0 20px;
    }
    Text 0
    Text 1
    Text 1
    Text 1

    Text 2

    Text 2

    Text 1

    Text 0

提交回复
热议问题