Wiping Out Inherited CSS Styles

February 27th, 2009 § 4 comments

Sure, you can use the !important css keyword in your stylesheets to override inherited styles, but what about when you want to simply wipe out or remove a style defined in a parent? Use the auto keyword.

Let’s say you have a style defined in a global.css sheet defining a position for a class called menu:

[code lang="css"] ul.menu { position:absolute; left: 10px; top: 10px; } [/code]

This will position a <ul class=”menu”> element</ul> 10 pixels away from the top left corner of the parent element.  But what if you want to move that menu to the right side of the parent element, but you can’t change the defined style in global.css?

You create a new stylesheet or define an inline style, and override the ul.menu class by specifying

[code lang="css"] ul.menu { right:10px; } [/code]

But this simply stretches the menu to the other corner.   We need to override the left:10px; style with the default style (as if we never specified the left:10px; to begin with).  left:none; won’t work and left:0; won’t either.  The solution is the auto keyword:

[code lang="css"] ul.menu { right:10px; left:auto; /* removes left:10px from parent */ } [/code]

Voila, you’re done!  It’s as if the left:10px was never defined!

Share and Enjoy:
  • Twitter
  • LinkedIn
  • DZone
  • Digg
  • Facebook
  • DotNetKicks
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Google Bookmarks
  • Technorati
  • HackerNews

No related posts.

Tagged

  • Pingback: Wiping Out Inherited CSS Styles | Adventures in HttpContext | Best Web Gallery

  • http://www.codeworksplus.com brad

    That doesn’t do anything for what I am trying to do. I am adding a button to ckeditor and somewhere they strip all formatting for buttons and inputs. I want to ignore that and have a default looking button but no dice.

    If I could even just find what the default values are for a button i can override them.

  • http://www.michaelhamrah.com Michael

    I’m not sure how ckeditor works internally, but you can use firebug to determine the html markup and find out what styles are being applied to the element. You can edit styles on the fly with firebug, so try playing around and see if you can get what you want. Stripping off all styles should get you the default button look. Also, make sure the element you want is an input type=’button’ element. If it’s not, you’re probably not going to get the look you want. If you can get it tweaked in firebug, make sure the css change you make is getting picked up. You want to be as specific as possible with your selector, so use the id of the element if possible, with an auto !important declaration for overrides. If this style is not getting picked up, you can check the inheritance chain in firebug to see what’s overriding it.

  • r3zn1k

    Thank you! This works perfectly!

    default_css: class { top: 500px; }

    customized_css (which will override the default_css): class { top: auto; bottom: 25px; }

What's this?

You are currently reading Wiping Out Inherited CSS Styles at Adventures in HttpContext.

meta