PDA

View Full Version : The perils of the universal reset


BoldUlysses
06-20-2008, 02:53 PM
Of course it always happens that as soon as I recommend something as a fix in another thread, I find a flaw in it myself.

Most of my pages so far have been horizontally centered. I tried the auto margins method (http://www.456bereastreet.com/lab/centered/horizontal/), but for some reason it wasn't working for me, so I had to resort to the negative margins method (http://www.bluerobot.com/web/css/center2.html). However, that required some javascript to justify the page should the browser window's horizontal size drop below the container width. And besides the obvious code bloat, it wreaked havoc if anyone would try to print the page.

I discovered today that my problem was the universal reset I had placed at the top of my CSS file:

* {
margin:0px;
padding:0px;
border:0px;
}

It's a great thing to have except that whatever values for those properties in my stylesheet I had set to auto would return to 0. In other words, the code for the auto margins centering method:

#container{
position:relative;
width:700px;
height:100%;
margin:0 auto;
}

would, because of the universal reset, be equivalent to this:

#container{
position:relative;
width:700px;
height:100%;
margin:0 0;
}

So much for centering using that method.

However, after I took the time to "customize" my universal reset, like so:

html, body, p, h1, h2, h3, h4, img, table, tr, td, ul, li, hr, a {
margin:0px;
padding:0px;
border:0px;
}

the auto margin centering method works. Notice the absence of div from the "customized" reset, so that the reset "skips over" my container CSS (form elements are also absent from the reset, but that's another story). Auto can now really be auto for my container margins, no more extraneous code or unneeded javascript. And printing works fine again.

Just thought I'd share that little victory this morning.

oesxyl
06-20-2008, 08:27 PM
is called global reset just because it intent to reset browsers default settings so you can start with your own in all browsers. There is no rule that this must contain margin and padding or only margin and pading, some have fonts settings, usualy this contain what is often specific for browsers and because positioning is usaly a problem, margin and padding fit inside, :)
you can put or get anything in it, I see a global reset on yahoo over 3 k size, but keep in mind that this slow down the browser.

regards

Apostropartheid
06-20-2008, 09:55 PM
The auto property shouldn't behave like this by default for margin-left and margin-right properties.

Otherwise, if both 'margin-left' and 'margin-right' are 'auto', they will be set to equal values. This will center the element inside its parent.
I have found no such problem in all my time using CSS. I'm thinking it's different problem--somehow--in your stylesheet.

rangana
06-21-2008, 03:23 AM
That's quite a good explanation, but I can't reproduce what your problem was.
Though this thread is not a question, I find it rather a learning to know the symptoms and the cure. :thumbsup:

I've made my own markups with what you have:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
* {
margin:0px;
padding:0px;
border:0px;
}
#container{
position:relative;
width:700px;
height:100%;
margin:0 auto;
border:1px solid red;
}
</style>
</head>
<body>
<div id="container">
This is my container.
</div>
</body>
</html>


With these few line of codes, I'm not seeing any odd. Container div is in the center.
I don't think margin:0 auto will turn into margin:0 0;.

Just a thought. The global reset comes earlier than our container div. Whatever margins and padding we set for the container div will be applied in our markups since it will override the earlier rule (*{margin:0;padding:0;}).

Anyway, another proof that I'm confused :confused:

BoldUlysses
06-22-2008, 02:17 PM
That's bizarre. I wish I had saved an earlier version of the page with the original CSS for you all to look over. I'm tempted to say "Well I changed it and that fixed it for me," but if there's another issue with my stylesheet I'd like to know. Will see if I can dig up that earlier version tomorrow at work. Thanks for the input.