Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS inheritance and overriding it

Tags:

css

I'm new to CSS. Ive created a Drupal site and playing with the theme.

I have some breadcrumb stuff that I would like to theme. If I go into Firebug and turn off the CSS properties

background
border-color
border-style

in the below code

.breadcrumbs .inner {
    background: none repeat scroll 0 0 #EFEFEF;
    border-color: #929292 #E2E2E2 #FFFFFF;
    border-style: solid;
    border-width: 1px;
    color: #8E8E8E;
}

I get the text looking exactly how I want it.

If I now go into my style.css which is inheriting the code and post

.breadcrumbs .inner {
    border-width: 1px;
    color: #8E8E8E;
}

The formatting I don't want is retained. If I specify .breadcrumbs .inner in the style.css does that not set it up again and override what was specified higher up the cascade?

If that is not the case how do I stop the inheritance without changing the other style sheet?

Thanks,

Andrew

Additional Info

Here is what I have at the moment

enter image description here

This is what I want to have enter image description here

like image 999
Andrew Avatar asked Jan 27 '11 22:01

Andrew


People also ask

Can CSS be overridden?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.

What is CSS style overriding?

CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. Overriding: Overriding in CSS means that you are providing any style property to an element for which you have already provided a style.

Does CSS support inheritance?

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.


2 Answers

If for example you have these css attached to your html document

<link rel="stylesheet" type="text/css" href="css/default.css">
<link rel="stylesheet" type="text/css" href="css/posts.css">

if you have the same class say .color defined in the both default.css and posts.css files, you would imagine that that last css file will be used, but it can be ignored if you write it like so:

// default.css
.color {
   color: blue !important;
}

// posts.css
.color {
   color: green;
}
// In this example the colour would be blue.

Using !important on inherited style forces inherited style to be used, if you want to override the inherited style then you can simply add !important to post.css

// default.css
.color {
   color: blue !important;
}

// posts.css
.color {
   color: green !important;
}
// In this example the colour would be green.

Now both are viewed as important and the last one will be used.

like image 167
Kingsley Ijomah Avatar answered Oct 14 '22 22:10

Kingsley Ijomah


If you specify the CSS styles for same classes twice the resulting style is a union of the attributes defined in both classes. To remove the previous styles you have to redefine the attributes.

like image 40
Chandu Avatar answered Oct 14 '22 22:10

Chandu