Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit all css properties from parent div to child div

My html,

<div id="parent">
    <div id="child">

    </div>

</div>

css looks like,

#parent{
height:100px;
width:200px;
any-aother-property1:something;
any-aother-property2:something;
any-aother-property3:something;
}

Is there is any way to inherit all the properties to child at once , means can I do like,

$('#child').properties= $('#parent').properties
like image 845
Nishant Nawarkhede Avatar asked Sep 23 '14 14:09

Nishant Nawarkhede


People also ask

How do I inherit CSS from parents?

Only direct child elements can inherit a CSS property from its parent element using the inherit value if the CSS property is set by the element's parent element. This is to ensure that the CSS property to be inherited is an inheritable property. The div1 has a height set to 100px . div1ChildChild .

Are all CSS properties inherited?

Only Certain Properties are Inherited The same is true in CSS; not every CSS property is inherited by default by child elements. In fact, if all properties were inherited, the effect would be similar to having no inheritance at all and you would have to write a lot of CSS to override this behavior.

Which CSS property are inherited?

The inherit CSS keyword causes the element to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand property all . For inherited properties, this reinforces the default behavior, and is only needed to override another rule.

Can CSS classes inherit?

CSS provides five special universal property values for controlling inheritance. Every CSS property accepts these values. Sets the property value applied to a selected element to be the same as that of its parent element. Effectively, this "turns on inheritance".


2 Answers

If you really want to dynamically inherit all CSS properties given to the parent at runtime, you can do the following.

Caution: This overrides the default value for all properties. Generally speaking, it is wise to assume that defaults are correct until proven otherwise. Some properties are inherit by default and some are not for various reasons. So you probably should only override specific properties that need to be changed.

#child {
    all: inherit;
}
like image 196
cambunctious Avatar answered Sep 19 '22 23:09

cambunctious


is this enough?

#parent, #parent > div {
    /* your properties */
}
like image 25
mike nvck Avatar answered Sep 18 '22 23:09

mike nvck