Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit css from grandparent tag? [duplicate]

Tags:

css

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
}
like image 888
Kiran Dash Avatar asked Jul 04 '15 07:07

Kiran Dash


People also ask

How do I inherit a CSS property from its parent element?

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.

What is not inherited in CSS?

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.

How are CSS rulesets inherited from parent selectors?

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.

How do you use inheritance in HTML?

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.


2 Answers

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>
like image 137
m4n0 Avatar answered Nov 09 '22 00:11

m4n0


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.

like image 39
Mr. Alien Avatar answered Nov 09 '22 02:11

Mr. Alien