tags:

views:

97

answers:

2

I have a menu list. I'm not using UL/LI, just nested DIV's. There are graphic separators between the menu items. The first item in the list needs to suppress the left padding; the last item needs to suppress the right padding and the graphic separator. Here's the CSS:

.platformItem {
  float: left;
  padding: 0 12px;
  background: url(/includes/themes/02RWO/images/assets/separator.gif) no-repeat top right;
}
.platformItem .first {
  padding-left: 0 !important;
}
.platformItem .last {
  padding-right: 0 !important;
  background-image: none !important;
}

And here's the HTML:

<div id="platformMenu">  
  <div class="platformItem first"><a href="">All</a></div>
  <div class="platformItem"><a href="">Windows</a></div>
  <div class="platformItem"><a href="">Mac</a></div>
  <div class="platformItem"><a href="">Linux</a></div>
  <div class="platformItem last"><a href="">Web</a></div>
  <div class="Clear"></div>
</div>

I was hoping I could do the suppression of certain properties using modifier classes. Is this possible? Is there a better way to do this?

Thx.

+1  A: 

You can use first-child pseudo-selector in modern browsers. last-child isn't supported in IE7 or IE8, though. You can also look at jQuery's enhanced selectors:

$(document).ready(function(){

$("div span:last-child")
    .css({color:"red", fontSize:"80%"})
});
Jon Galloway
Yes, in an ideal world, we'd only be dealing with modern browsers. What's the solution for this barnyard world we live in?
NotoriousWebmaster
Yes, that would work. But I'm _just_ starting this layout: I was hoping to do it in CSS, and not have to resort to JS for some time...Would going with UL/LI bring any benefits?(And, BTW, thx for your help.)
NotoriousWebmaster
The benefit UL/LI brings is to make clearer to yourself and any maintainers that the items are a list; that's the argument for semantics. If you don't care about semantics, you can put *any* block-level item in a DIV, and *any* inline item in a span (and then it's harder to understand the *meaning* of those elements). Other than that, CSS can select the elements whether they're LIs or DIV/DIVs. Agree with John Galloway -- the platformItem classes on the inner divs/list items are unnecessary. For compatibility, you need at least the 'last' class, as :last-child isn't supported in IE.
Val
So the problem with UL/LI is that you need to add a number of properties to strip away the default styling before you can get to what you want. But I'm all for semantics, Val; so I'll keep the UL/LI I just switched to.
NotoriousWebmaster
+1  A: 

Didn't need JS after all. .first and .last aren't downstream from .platformItem, but from #platformMenu. (I should've seen this.) New code:

#platformMenu .first {
  padding-left: 0 !important;
}
#platformMenu .last {
  padding-right: 0 !important;
  background-image: none !important;
}
NotoriousWebmaster
Oh, right. I thought you were trying to get rid of the .first and .last classes, too. You don't need .first if you're targeting IE7+, you can use #platformMeu div:first-child for that.
Jon Galloway
+1 because this is the surer bet in most environments if you have control over the markup.
eyelidlessness
your !important-s are not needed because your specificity is sufficient to override the current declaration specificity.
Carl Camera