Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel some CSS Style

Tags:

html

css

For one project, I've to use a defined stylesheet. I cannot edit it, I've to take it as it comes.

But, there is some styles which aren't adequate for this website(e.g. margin).

I've too create my own CSS file which override some of those properties.

One example, if I've this:

<div id="main-content" class="container">My test text</div>

And the following css instruction in a css file:

div.container{margin:5px}
div#main-content{margin-left:235px; ...}

I would like in MY css to define that the div#main-content hasn't this margin but it's default value.

How could I do it? I don't want to do a margin-left:0px because if I've a global style(saying that all div.container should have a 5px margin, I would like it applies to my previous div. And thoses data could change.

like image 838
J4N Avatar asked Aug 06 '12 11:08

J4N


2 Answers

There's no pure-CSS way to remove a property value declaration without explicitly setting it to another value.

So if you have a situation like this:

 <div id="divA" class="class3 class1"></div>
 <div id="divB" class="class3 class2"></div>

 .class1{margin:2px;}
 .class2{margin:3px;}
 .class3{margin:10px;}

...and you want to cancel the effects of class3 so that divA has margin of 2px, and divB has margin of 5px, your only pure-CSS recourses will involve explicitly referencing each case. I.E. in your overriding stylesheet, you would have to re-assert:

 .class1{margin:2px;}
 .class2{margin:3px;}

...and so on for however many classes/selector contexts may be affected. This is probably unrealistic.

If you're willing to dive into JavaScript, you can actually remove the class-name from the element.

For straight-up JavaScript example of how to do that, see: Remove CSS class from element with JavaScript (no jQuery)

If you want simpler code, and are willing to take on a JavaScript library, jQuery will let you do this in one line like this:

$('divA').removeClass('class3');
like image 80
Faust Avatar answered Nov 16 '22 15:11

Faust


You might add another css class, like .no-left-margin

<div id="main-content" class="container no-left-margin">My test text</div>

and add to your css

.no-left-margin{
 margin-left: 0px !important;
}
like image 3
mkk Avatar answered Nov 16 '22 13:11

mkk