I am having a two level nested div and I want to apply the div holding a class "c" with the same width of div with class a. If it is parent then I guess inherit will do the job. But what to be done in this case?
HTML CODE:
<div class="a">
<div class="b">
<div class="c">
</div>
</div>
</div>
CSS CODE
.a{
width:600px;
}
.b{
width:80%;
}
.c{
//needs to inherit width property set for class a without directly mentioning it as 600px
}
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.
CSS properties such as height, width, border, margin, padding, etc. are not inherited. We can enable inheritance on noninheritable CSS properties by using the inherit value. What is CSS inherit? When you set inherit on a CSS property, the property takes the value from the element’s parent.
These CSS rulesets are inherited from their parent selectors. The child element will naturally inherit a CSS property with its value from the parent element if the CSS property is not specified. We only set div#div1 to have a text color of red, yet the CSS rule was applied to its two child div elements: div#div1Child and div#div2Child.
If the property is inherited, then you know that the value will remain the same for every child element in the document. The best way to use this is to set your basic styles on a very high-level element, like the BODY. If you set your font-family on the body property, then, thanks to inheritance, the entire document will keep that same font-family.
You can add multiple classes to the grandchild div to include
the properties of a to c.
Also setting 80% for .b
will result in 80% of 600px = 480px
for .c
You need to modify it to 100%.
.a {
width: 600px;
}
.b {
width: 100%;
}
<div class="a">
<div class="b">
<div class="a c">
I am 600px wide.
</div>
</div>
</div>
Unfortunately you cannot do this in pure CSS, by default the child element never inherits the width
, you need to specify it, yes, by default it will span 100% if you use a block level element like a div
or p
tag but inorder to inherit(re use) the property of grand parent you can use CSS pre processors like LESS or SASS..
So if you want to do this in SASS we can do something like
.a {
width: 600px;
}
.b {
width: 80%;
}
.c {
@extend .a;
//This is still not an inheritance, you are simply pulling in all the
//properties defined in .a to this class
}
So here it will pick all the properties from .a
to .c
. But here's a catch, when you use .
notation in SASS, it will literally print the .a
block as well, so if you want to only use it for @extend
purpose, you can write this selector instead
%a {
width: 600px;
}
.c {
@extend %a;
}
Here SASS won't print .a
anymore but only .c
.You can refer the official docs for more over SASS @extend
You can also define variables in SASS so defining one with $base-width: 600px;
and re using it across can make sense as well. If you still want to stick with traditional CSS than I would recommend you declaring multiple classes just like Manoj suggested but I would not do this as if you are declaring any properties in .a
and same in .c
things will start getting messy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With