Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding properties in CSS

Tags:

css

#iddiv span {
    display: inline-block;
    width: 190px;
}
.myclass {
    width:10px;
}

Then I have

<div id="iddiv">
    <span>hello:</span> 
    <span class="myclass">yeah</span> <br/>
</div>

I would like the first span's width to be 190px, and second's to be 10px. But both are 190px: why it's not overriding the width propoerty?

EDIT: Thanks for your responses. What about unsetting width? I don't want 10px width, just default width as if it was undefined

like image 627
de3 Avatar asked Jun 09 '11 15:06

de3


People also ask

What is CSS rules 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.

How do you override a div in CSS?

To override an attribute that a CSS class defines, simply append a new inline style after the DIV's class definition.

How do I override a CSS style with inline?

The only way to override inline style is by using ! important keyword beside the CSS rule.


2 Answers

You could always use the !important flag to override:

.myclass {
    width: 10px !important;
}
like image 64
Eric Avatar answered Nov 15 '22 20:11

Eric


Because id+selector (#iddiv span) is more specific than a class. Either

#iddiv span.myclass

or

#iddiv .myclass

should work for this case.

Learn more about CSS specificity here or by Googling it.

like image 37
RwwL Avatar answered Nov 15 '22 19:11

RwwL