6

I want to make a nostyle class in CSS. So that any css written in my style sheet can be overlapped with this class, where i don't want any style. I used the below code, which is not working.

<style type="text/css">
  .nostyle {
          margin: 0;
          padding: 0;
          border: 0;
          outline: 0;
          font-size: 100%;
          vertical-align: baseline;
          background: transparent;
        }   
   </style>

4 Answers 4

4

You are probably suffering from CSS specificity rules where other rules override you class, because they are more specific.

Consider:

<div id="col1">
    <div class="rule1 another-rule">
        Test
    </div>
</div>

<style>
    .rule1 {
        margin: 0;
    }

    #col1 .another-rule {
        margin: 10px;
    }
</style>

In this situation, the margin on the styled DIV is always 10px because the rule carries more weight.

To overcome this, you may use the !important feature of CSS. Try

.nostyle {
      margin: 0 !important;
      padding: 0 !important;
      border: 0 !important;
      outline: 0 !important;
      font-size: 100% !important;
      vertical-align: baseline !important;
      background: transparent !important;
    }   

Here's a great guide to how CSS Specificity works:

CSS Specificity: Things You Should know

3

The styles are probably being overwritten. Add !important after each style to give them a greater weight.

Your css would look like:

.nostyle {
    margin: 0 !important;
    padding: 0 !important;
    ...
}
1

Adding !important is a bad practice. You should have a look into CSS Priorities. Here is a simple list to define priorities:

  1. !important wins.
  2. inline
  3. internal
  4. external

If there are rules at the same level your priorities will be as follows:

  1. The more specific rule wins EG: div span #abc .class
  2. Rule written towards the end wins where rules are equally specific.
5
  • "Adding !important hampers your site's SEO". I've never heard that suggested. Do you have something to link to to back this up? I'll remove my downvote if you can provide a good link. Nov 7, 2013 at 16:49
  • !important hampers the flow of your CSS declarations which is a bad practice and should be avoided unless absolutely necessary. Bad practice -> Hampers SEO. stackoverflow.com/questions/3706819/… Nov 8, 2013 at 5:30
  • While the use of !important may not be a best practice in many situations, it has no direct relation to SEO. Nov 8, 2013 at 8:36
  • No 'direct' relation to SEO. So, what are you implying? And I hope you read the rest of the answer before your downvote. Nov 8, 2013 at 15:32
  • I'm not implying anything; I am stating that the use or abuse of !important has no direct impact on SEO. By 'direct' I mean that there are no practical scenarios that I am aware of where use of !important is penalised by search engines. The biggest problem with the use of !important is that it removes the C from CSS. That's an architectural issue for the dev(s) to wrestle with. A user may have issues in the rare circumstance where they wish to override a style. That said, it's still not an SEO issue. The downvote is for spreading FUD over the use of !important & its SEO impact. Nov 14, 2013 at 14:53
0

you can do that for no classes but your css have to look more like this:

 <style type="text/css"> 
 a,a:hover,div,p,font,table,li,ul,ol {
      margin: 0;
      padding: 0;
      border: 0;
       outline: 0;           font-size: 100%;
      vertical-align: baseline;
       background: transparent;
     }       </style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.