Why do browsers treat margin differently?

Hi - I have this in my css:

#header{width:100%;margin-bottom:30px;}

opera, IE & Safari ignore it. They display this (no 30px margin below the menu that should be there)

while FF & chrome correctly display the 30px margin like this:

Is there a way to tell the first three browsers to behave themselves? Or have I done something wrong?

thanks! - Val

Whether you’re aware of it or not, every browser has its own built-in stylesheet that it will use if it doesn’t get overridden by your own CSS. These can be negated by using a CSS reset, such as this one from Eric Meyer - http://meyerweb.com/eric/tools/css/reset/

Given that three browsers, are ignoring what you’ve done, I’d check in the dev tools for each to see what CSS is being applied. I’d suggest that something in there is being seen as having a greater level of specificity than you own code, it’s then a case of working out what that is.

In addition to chrisofarabia:
There are a lot of great CSS-Resets out there and personally I like this one (it is more modern and made by using Eric Meyers) the most, but do not forget:

this is a starting point, not a self-contained black box of no-touchiness.
~ Eric Meyer

2 Likes

I knew I missed something out of my reply.

Most likely you have done something wrong because modern browsers are pretty consistent in the way they handle margins assuming that you aren’t relying on the defaults as mentioned above.

I checked in IE11 and Firefox and they are both observing the margin correctly. If you are talking about an old browser like IE8 (Safari PC is dead destroy it now) then it seems you don’t have the html shiv in place for IE8 to recognise the new html5 elements that you are using (). IE8 will not apply any styling to that element as it knows nothing about it.

You need the html5 shiv and then set the element to display:block.

<!-- the next script is the html5shiv and for media query support in IE8 and under -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

You are still using the old html4 doctype anyway so you should have stuck to using divs or upgrade to the html5 doctype and use the new elements with the shive and a suitable reset.

Hi all - thanks for your feedback. I fixed the problem by removing the margin-bottom from header, and adding it to menu. Then it displays ok across all browsers.

Hi Paul - thank you! You’re right, it doesn’t make sense to use html4 any more. Am I right in understanding your advice is:

(1) change this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

to this:

<!DOCTYPE html>

And somewhere inside <head> add this:

<!-- the next script is the html5shiv and for media query support in IE8 and under -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

I say ā€œsomewhere insideā€ because my header.php begins with doctype and ends with a meta property, then in each individual file it continues with more meta’s (e.g. the og’s unique to that file) and </head> ends in the individual file template, not in the header template.

So my idea is to add the html5shiv to header.php template. Is that ok? It’ll end up looking something like this:

<head>
<meta charset...
etc meta's
<!-- the next script is the html5shiv and for media query support in IE8 and under -->
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!--that's the end of header.php and now begins the individual file.php-->
<link rel="canonical" ...
<meta <meta property="og:url" ... etc bunch of meta og's
<?php wp_head(); ?>
</head>

It’s not a wordperfect page but I call the wp environment so as to use a wp plugin (ajax testimonials).

you should have stuck to using divs

you mean instead of the css tables?

or upgrade to the html5 doctype and use the new elements with the shive and a suitable reset.

(2) Yes I’ll do that! Can you confirm it’s ok to stick the shiv anywhere inside <head>?

(3) What does ā€œa suitable resetā€ mean? Is that the display:block? I’m scared to add that to #header, so that instead of this:

#header{width:100%;}

it becomes:

#header{width:100%;display block;}

The file.php template is:

<header id="header" role="banner"><?php include("$rpath/1cde/head.php"); ?></header>

and head.php has all the css table stuff with logo, fontawesome icons, subscribe form, postit image, and then the non-table #menu. I’m scared to set all that to display:block, in case it messes with tables which have their own display rules.

I feel more comfortable just moving the margin-bottom away from #header and down to #menu. And leaving header’s only rule as width:100%.

I’m really grateful you brought up that doctype. It’s something we all forgot when making the template responsive.

thank you! - Val

Val, if your header.php includes all the <head> section, how do you give each page a unique TITLE?

Hi - no it doesn’t. That’s why I was asking Paul if it’s ok to have the shiv anywhere inside <head>. Header.php has half of the <head> (like stylesheet) then the rest of the <head> is in each individual file.php (like title). So head opens in header.php and closes in indiv-file.php.

The test page is here, although you can’t see from that the division of head into 2 separate files.

http://greensmoothie.com/test/1mst.php

I’ll be interested to see Maestro Paul’s reply, too. This is the order that Boilerplate uses if it helps.

<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="apple-touch-icon" href="apple-touch-icon.png">
        <!-- Place favicon.ico in the root directory -->

        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/main.css">
        <script src="js/vendor/modernizr-2.8.3.min.js"></script>
    </head>

If you want to use the new html5 elements then yes you should use the html5 doctype although it will actually do no harm if you don’t (apart from validation). Of course as you are using a transitional doctype you may have validation errors if you change to the the html5 doctype anyway.

The shiv goes in the head but should follow any stylesheets. I also forgot to give you the media query script that IE8 needs. There are a couple of scripts about to patch media queries but I tend to use this one which should also follow the main css.

If you use modernizr then you don’t need the html5 shiv but you still do need the media query script. Modernizr provides feature detection so if you don’t expect to use it then just go with the html5 shiv as it is much lighter code.

I don’t know what those are! I’m guessing css tables is one… Anyway it just feels right to have the same doctype as those pages on the site that are in wordpress.

This shiv thing now is getting too complicated for me. I’m gonna leave it out altogether. IE8 can go to pot.

I’m grateful you inspired me to dump that old doctype!

thanks million, Val

No CS has nothing to do with html5 at all :smile:

html5 is mark up elements such as the ā€˜header’ element that you are already using and giving you the problem in IE8 as I already mentioned. The other new html5 elements are listed here.

There’s nothing complicated about it. Just include it in the head after the css and forget about it. Same for the media query script.

hahaha - you’ve forgotten there’s only one pea in my brain. I haven’t the faintest idea what that page is saying (let alone what I’m supposed to extract from it & add to my css). It seems to be about IE8 being responsive and I’m sure 99% of mobile phones & tablets couldn’t give a hoot about ie8 and will work just fine without shiv. So now I can go back to protecting my pea (and my header.php) without shiv…

No CS has nothing to do with html5 at all

I just realized what you’re saying to me. You’re saying there’s something new in there that’s html5. There’s only 2 new things I see:

<header id="header"...
<nav id="menu"...

So all I need do is this:

<div id="header"...
<div id="menu"...

and then ie8 will recognize the #header? I want it to see the width:100%.

But if that’s the case, does that mean IE8 is not seeing the dropdown menu & its hamburger? Since it doesn’t recognize <nav?

thanks, Val

Take a look at the CanIUse page on semantic elements - http://caniuse.com/#search=semantic

That covers all the new HTML5 semantic elements - section, article, aside, header, footer, nav, figure, figcaption, time, mark & main

It will also tell you which browsers support them - for IE, that means IE9+ and not IE8

Yes assuming you haven’t applied styles using the html5 tag name,

It won’t recognise styles on nav or header but it will apply styles to inner elements it recognises.

Consider the following simple example in IE8.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
header {
	padding:25px;
	background:red;
	margin-bottom:30px
}
.test {background:blue}
.next {background:green}
</style>
</head>

<body>
<header>
		<div class="test">Testing</div>
</header>
<div class="next">Next</div>
</body>
</html>

In IE8 you will get a blue strip flush against a green strip. In other browsers the blue strip will be inside a padded red background and the green strip will be 30px away from them and not flush.

thanks Paul - that’s really interesting! So it recognizes the ul & li styles but not the #menu! As they say, in Redmond no one can hear you scream…

I don’t want to do that shiv jazz because it will no longer be needed in a year or two & it’s extra data. I’m thinking just to ignore the whole thing. IE updates itself automatically doesn’t it? So who’s likely still to be on 8?

Or do you rather advise I should change <header> and <menu> into <div>? And search through my html to see if there’s any others that don’t begin with div & change them too, e.g. maybe there’s a <footer>

Microsoft Edge will do that, but no version of IE ever has. Version number changes for IE were a major release, and had to be selected/installed by the user (or their admin staff), when they appeared. The only thing beyond that were patches that Microsoft released, though these were usually to address security concerns, rather than feature releases.

Whilst IE8 is now a very small (around 1%) part of what’s recognised out in the wild now, IE9 through IE11, are far more prevalent.

thank you! I’m going to stick to my fancy new <header and <nav tags then and not do the shiv :slight_smile:

I just took a look in IE tester and as Paul said, it’s ignoring the styling for #menu – e.g. text-align:center; is displaying the menu as left-aligned – but it’s picking up all the styles (+ links) for ul & li’s inside the menu.

Even when I change <nav to <div it still displays menu on the left. IE7 is even worse! Looks a real mess. But since only 1% are still using that old junk, I’m going to ignore them.