Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to override a user agent CSS stylesheet rule that gives unordered-lists a 1em margin?

Tags:

css

overriding

I'm working on a web app that has a topBar similar to facebook's blue bar at the top. I have an unordered list within the div of that bar to list some items, like Inbox, Notifications, etc. The UL has a 1em margin as defined by the user agent stylesheet of my browser. This is a problem because it's pushing my topBar down 1em. How can I override this to make the border of the ul = 0? I've read that overriding user agent stylesheets is a bad idea so I'm curious to learn what is best to do. Thanks.

EDIT: Here's the CSS file:

body {  margin:0px; padding:0px; }  #topBar{     background-color:#CCCCCC;     height: 50px;     width:100%;     z-index:-1;  }  #mainNav{     margin:0 auto;     width:900px; } #logo{     float:left; }  #mainNav ul li{     float:left;     border:0px;      margin:0;     padding:0;     font-size:10px } 

And the html:

<!DOCTYPE html> <html>     <head>         <title>ff</title>         <%= stylesheet_link_tag :all %>         <%= javascript_include_tag :defaults %>         <%= csrf_meta_tag %>     </head>     <body>         <div id="topBar">             <div id="mainNav">                 <div id="logo"><%=image_tag("livecove.jpg") %></div>                 <ul>                     <li>Inbox</li>                 </ul>             </div>         </div>          <%= yield %>     </body> </html> 
like image 485
John Avatar asked Feb 02 '11 11:02

John


People also ask

How do I override user agent in CSS?

To override the user agent string from Chrome DevTools: Press Command + Shift + P (Mac) or Control + Shift + P (Windows, Linux, ChromeOS) to open the Command Menu.

What is the user agent stylesheet?

User-agent stylesheets User-agent, or browsers, have basic style sheets that give default styles to any document. These style sheets are named user-agent stylesheets. Most browsers use actual stylesheets for this purpose, while others simulate them in code.


2 Answers

If You Are Able to Edit the Offending Stylesheet

If the user-agent stylesheet's style is causing problems for the browser it's supposed to fix, then you could try removing the offending style and testing that to ensure it doesn't have any unexpected adverse effects elsewhere.

If it doesn't, use the modified stylesheet. Fixing browser quirks is what these sheets are for - they fix issues, they aren't supposed to introduce new ones.

If You Are Not Able to Edit the Offending Stylesheet

If you're unable to edit the stylesheet that contains the offending line, you may consider using the !important keyword.

An example:

.override {     border: 1px solid #000 !important; }  .a_class {     border: 2px solid red; } 

And the HTML:

<p class="a_class">content will have 2px red border</p> <p class="override a_class">content will have 1px black border</p> 

Live example

Try to use !important only where you really have to - if you can reorganize your styles such that you don't need it, this would be preferable.

like image 84
Michael Robinson Avatar answered Sep 17 '22 16:09

Michael Robinson


No its not. Use Meyers CSS reset :) http://meyerweb.com/eric/tools/css/reset/

like image 20
benhowdle89 Avatar answered Sep 17 '22 16:09

benhowdle89