Day 15. Learning CSS Inheritance, Specificity and Cascade
October 17, 2019
- Every time I have a problem with my CSS, it’s always because I get one of these wrong.
- Almost all property definitions are inheritable in CSS. If you set a
background-coloron the body, all elements will inherit it except that element has been styled otherwise. - Not all CSS properties are inherited. Imagine setting a
borderon thebody, and every child element inherits the border color. - There’s a reference of all inheritable CSS properties, but most times common sense will tell you which ones should be.
- Most times, an understanding of CSS inheritance will be enough. Other times, you’ll need to changes how elements inherit. We can do this with 4 properties;
inherit,initial,unsetand the newrevert. - When we set a property to
inherit, it takes the same value specified in the parent.
<ul>
<li>list 1</li>
</ul>
ul{ border:1px solid red; } li { border: inherit; }The li will have the border specified in the ul.
- Setting a property to
initialrestores back user agent (browser) default styling. unsettakes a property back to its natural value (eitherinheritorinitial) before any styling.- The last value is
revert. It’s somehow difficult to explain. But think of it as a way to revert inheritance one level backwards. For example… - The
background-coloris set toinheritby default (background-colorof parent will apply on children). If we change the value ofbackground-coloron another element