Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an element inherit a set of CSS rules for another element?

Tags:

css

Say I have a <div> like this that is going to have all of the same properties with a background image or something like that:

div.someBaseDiv {     margin-top: 3px;     margin-left: auto;     margin-right: auto;     margin-bottom: 0px; } 

And I wanted to inherit from it like this:

div.someBaseDiv someInheritedDiv {     background-image: url("images/worldsource/customBackground.gif");     background-repeat: no-repeat;     width: 950px;     height: 572px; } 

Of course I’m pretty sure this is written wrong, and I’m not afraid to ask for help, so can someone tell me how to make this work and include the HTML markup?

like image 614
Tikhon Avatar asked Jun 16 '11 18:06

Tikhon


People also ask

How do CSS styles for a particular element get inherited?

What is CSS inheritance? CSS rulesets cascade down the CSS hierarchy from parent selectors to their children 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.

How do I inherit one CSS style from another?

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.

Does CSS have inheritance?

In CSS, inheritance controls what happens when no value is specified for a property on an element. CSS properties can be categorized in two types: inherited properties, which by default are set to the computed value of the parent element.


2 Answers

The easiest is to add your someInheritedDiv element to the first rule like this.

div.someBaseDiv, #someInheritedDiv {     margin-top:3px;     margin-left:auto;     margin-right:auto;     margin-bottom:0px; } 

This will tell your #someInheritedDiv to apply the same styles as div.someBaseDiv has. Then you extend this set of styles with more specific to your #someInheritedDiv:

#someInheritedDiv {     background-image:url("images/worldsource/customBackground.gif");     background-repeat:no-repeat;     width:950px;     height:572px; } 

This is how specificity in CSS works.

like image 130
spliter Avatar answered Sep 21 '22 04:09

spliter


Use both classes and combine them like so:

.baseClass {     margin-top:3px;     margin-left:auto;     margin-right:auto;     margin-bottom:0px; }  .baseClass.otherClass /* this means the element has both baseClass and otherClass */ {     background-image:url("images/worldsource/customBackground.gif");     background-repeat:no-repeat;     width:950px;     height:572px; } 

The markup is as follows:

<div class="baseClass otherClass"></div> 

Now, in this fashion you can override baseClass if necessary... and since you don't have to keep adding your new class names to the baseClass definition, it's a bit cleaner.

like image 43
canon Avatar answered Sep 19 '22 04:09

canon