tags:

views:

1115

answers:

4

Hi,

I have a menu div which has a dark background. Inside it, I have several menu item divs with 1px margins on the right and the left. This way I've got separators between them. Obviously these appear on the very left and very right side of the menu which I don't want. Is there a way to accomplish this without inserting 1-pixel divs as separators?

Thank you

edit: sorry, I thought it was descriptive enough. Here is the code:

<div id="menu">
       <a href="#"><div class="menu_item"><img src="imgs/menu/szabalyzat.png" /></div></a>
       <a href="#"><div class="menu_item"><img src="imgs/menu/profil.png" /></div></a>
       <a href="#"><div class="menu_item"><img src="imgs/menu/zenekarok.png" /></div></a>
       <a href="#"><div class="menu_item"><img src="imgs/menu/jelentkezes.png" /></div></a>
       <a href="#"><div class="menu_item"><img src="imgs/menu/esemenynaptar.png" /></div></a>
       <a href="#"><div class="menu_item"><img src="imgs/menu/mmmk_estek.png" /></div></a>
 </div>

IE6 incompatibility is OK (thankfully).

+4  A: 

The simplest way yo achieve it is to mark your first and last elements with custom classes and remove that margins from them.

<ul class="menu">
  <li class="first">One</li>
  <li>Two</li>
  <li>Three</li>
  <li class="last">Four</li>
</ul>
<style>
  .menu li { margin: 0 1px; }
  .menu .first { margin-left: 0; }
  .menu .last { margin-right: 0; }
</style>

You can also try using complex css selectors, like :first-child, but they do not work in older versions of MSIE.

OR, you can use 2px margins on the right side instead and go with only one additional class:

<ul class="menu">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li class="last">Four</li>
</ul>
<style>
  .menu li { margin-right: 2px; }
  .menu .last { margin-right: 0; }
</style>
n1313
I corrected the margin in the first example, `1px 0` puts the margin on the top/bottom, not left/right as it should be. Also "none" is not a valid value AFAIK so I replaced with 0.
DisgruntledGoat
Whoops, my bad. Thanks.
n1313
+1  A: 

If a high percentage of your audience's browsers support CSS3, you can use the :first-child and :last-child pseudo-classes:

div#menu div:first-child {
margin-left: none;
}
div#menu div:last-child {
margin-right: none;
}
Donut
I think first and last-child are actually CSS2. Most browser support them, I think only ie6 does not (again).
Joel Potter
+2  A: 

Can't you have 2px left-margin instead of 1px on each side and then use the css pseudo class :first-child to remove these margin for the first item ?

EDIT: I agree with the fact that you should use border as separator rather than background but in case you do this that way for some good reasons, my answer's still valid :-)

p4bl0
Well, I'm inexperienced with CSS, and didn't think of borders for this purpose. I had the #menu div and this was the firs solution that came into my mind. Do borders have some advantage over this solution?
Tamás Szelei
borders are just a more "clean" way of doing this, since a border is already a kind of separator.
p4bl0
+10  A: 

The following rule will apply to all .menu_item elements that follow another .menu_item element:

.menu_item + .menu_item {
  border-left: 2px solid black;
}
Allain Lalonde
hey! more pretty than my `:first-child` way!
p4bl0
Sadly, does not work in ie6 though.
Joel Potter
Doesn anyone still use IE6? jk
Allain Lalonde
15% of your customers do.
n1313
Microsoft still support it, however even alexa big names are dropping support
Stephen lacy
It won't work in IE... So what? That's not something really important, it's just a small design polishing. As long as the website is usable with every browser it's okay for me. It's not a 20 by 2 px black rectangle that will scare IE6 users... If that was the case most of them would already be dead by heart-attack ^^.
p4bl0
this was sure prettier than :first-child, but it didn't work for me in ff3.5 (it's possible that I did something wrong though).
Tamás Szelei