Member Avatar for Member #174700

Hi,

I'm not too good with CSS but I managed to create a menu list that I like. It works exactly how I want with IE 6.0, but overlaps with Netscape and FF.

Can anyone help me figure out why?

Here is the CSS:

#left-menu {
	background-color: #181C3A;
	border-bottom-width: 1px;
	border-bottom-style: solid;
	border-bottom-color: #393939;
	list-style-type: none;
	margin: 0;
	padding: 0;
	width: 100%;
}
#left-menu li.main {
	font-family: arial;
	font-weight: bold;
	font-size: 16px;
	margin: 0;
	margin-top: -12px;
	margin-bottom: -18px;
}
#left-menu li.sub {
	font-family: arial;
	font-weight: bold;
	font-size: 12px;
	font-variant: small-caps;
	margin: 0;
	margin-top: -12px;
}
#left-menu .main a { 
	background-color : #181c3a; 
	border-top-width : 1px; 
	border-top-style : solid; 
	border-top-color : #393939; 
	color : #b3c0ce; 
	display : block; 
	padding : 6px 15px; 
	text-decoration : none; 
	margin : 0; 
}
#left-menu .sub a { 
	background-color : #181c3a; 
	color : #b3c0ce; 
	display : block; 
	padding : 4px 25px; 
	text-decoration : none; 
	margin : 0; 
}
#left-menu .main a:hover {
	background-color: #FF9900;
	background-image: url('../graphics/hover.gif');
	background-position: left center;
	background-repeat: no-repeat;
	color: #181C3A;
	padding-bottom: 6px
}
#left-menu .sub a:hover {
	background-color: #FF9900;
	background-image: url('../graphics/hover.gif');
	background-position: left center;
	background-repeat: no-repeat;
	color: #181C3A;
	padding-bottom: 4px
}

The abbreviated HTML is like this:

<li class="main"><a href="../about.htm" target="_self">About 7th Fleet</a></li>
<li class="sub"><a href="../leaders/commander.htm" target="_self">Leaders</a></li>
<li class="sub"><a href="../forces.htm" target="_self">Forces</a></li>

Also, I've attached screenshots of how it looks in IE and Netscape.
Thank you so very much.

Matthew

Dani AI

Generated

posted a common symptom: the menu looks fine in IE6 but list items overlap in Firefox/Netscape. tried position:relative (no effect) and correctly pointed to the negative margins as the root cause. The html>body hack that was added is a browser-specific kludge; a standards-based fix is preferable.

Negative top/bottom margins can make one box intrude into the next under normal (standards) layout rules. Old IE behaved differently (hasLayout/quirks), which hid the problem there. For an authoritative read on margin behavior and collapsing, see MDN: [margin] (https://developer.mozilla.org/en-US/docs/Web/CSS/margin) and [Mastering margin collapsing] (https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing). Also confirm a standards DOCTYPE is present so browsers render in standards mode ([DOCTYPE] (https://developer.mozilla.org/en-US/docs/Glossary/DOCTYPE)).

Practical fixes:

  • Remove all negative vertical margins on the li elements.
  • Put spacing into the clickable element (the anchor) using padding or a fixed line-height, and keep the hover state from changing padding so element height does not jump.
  • Add separators consistently with an adjacent-sibling rule instead of trying to pull items together with negative margins.

Example replacement (illustrative):

#left-menu li { margin: 0; padding: 0; }
#left-menu li + li { border-top: 1px solid #393939; }
#left-menu a { display: block; padding: 8px 16px; line-height: 1.2; }
#left-menu a:hover { /* same padding; only change background/color */ }

Final checklist: remove margin hacks, keep hover states height-neutral, test with a valid DOCTYPE in multiple browsers, and use conditional comments only if legacy IE-specific tweaks are absolutely necessary. This approach avoids fragile, browser-specific behavior and produces consistent results across modern engines.

Recommended Answers

All 5 Replies

Try adding 'position:relative' to the <li> tags.

Member Avatar for Member #174700

Thank you for your quick reply. No, that didn't work. The list didn't change one way or another with that tag.

You can't have negative surrounding styles.

margin-top: -12px;
	margin-bottom: -18px;

These are causing the overlapping.

Why are you using them?

Negative surrounding styles are not really defined, and do different things in IE and FF.

- Since FF obeys the standard and puts surrounding styles outside the size of a box object, a negative surrounding style makes the surrounding style intrude on the contents of the box. This causes the boxes to overlap when rendered, since they are larger than their margins.

- Since IE does not obey the standard, it puts the surrounding styles inside the size of the box object. So negative surrounding styles stick out from the box.

The best thing to do is to not define sizes and surrounding styles on the same tag.

Always design for FF first, since it obeys the standard. Then, all browsers except IE will work. Then adjust your design to also work in IE.

Member Avatar for Member #174700

don't know how smart it is, but I think I figured out a way.
I read http://www.sitepoint.com/article/bro...ific-css-hacks to figure it out.

These are the changes I made:

#left-menu li.main {
 	font-family: arial;
	font-weight: bold;
	font-size: 16px;
	margin: 0;
[B]	margin-top: -12px;
	margin-bottom: -18px;[/B]}
[B]html>body [/B]#left-menu li.main {
 	font-family: arial;
	font-weight: bold;
	font-size: 16px;
	margin: 0;
[B]	margin-top: 0;
	margin-bottom: 0;}[/B]#left-menu li.sub {
	font-family: arial;
	font-weight: bold;
	font-size: 12px;
	font-variant: small-caps;
	margin: 0;
[B]	margin-top: -12px;}[/B]
[B]html>body [/B]#left-menu li.sub {
	font-family: arial;
	font-weight: bold;
	font-size: 12px;
	font-variant: small-caps;
	margin: 0;
[B]	margin-top: 0;}[/B]

And now it looks right, at least using IE 6 and Netscape 7. I can't test on other platforms just yet.

What do you think?

It's a kludge.

Don't use negative surrounding styles. They are not defined, and can do widely different things on different browsers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.