28

One thing I often want to do when laying out a website is to have some elements next to each other, with separators between them. For instance, if I have three elements, I'd want two separators between them, and nothing at either end.

I achieve this in various ways. For vertical stacking of elements, I sometimes use <hr />. Horizontally, I might do something like:

<div>
    <span class="notend">things</span>
    <span class="notend">stuff</span>
    <span>items</span>
</div>

.notend {
    border-right: solid black 1px;
}

Is there a more semantic way of doing this? I want to have separators between elements without putting styling elements into the HTML part, or using non-semantic classes. I don't mind of this requires hacky CSS, I just want to get stuff to do with styling away from the HTML files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oliver
  • 11,297
  • 18
  • 71
  • 121

6 Answers6

47

Use this:

#menu span + span {
    border-left: solid black 1px;
}

http://jsfiddle.net/thirtydot/QxZ6D/

That will apply border-left to all except the first span.

The adjacent sibling selector (+) is supported in all modern browsers except IE6.


Another way to do it is this, which is sometimes nicer because you can keep all the declarations for the "menu buttons" in one block:

http://jsfiddle.net/thirtydot/QxZ6D/1/

#menu span {
    border-left: solid black 1px;
    /*
    a: bunch;
    of: stuff;
    */
}
#menu span:first-child {
    border-left: 0
}

This has exactly the same level of browser support as the first solution.

Note that if you like this solution, it's better to use :first-child rather than :last-child, because :first-child (from CSS2) is supported in IE7/8 and :last-child (only introduced in CSS3!) isn't.

thirtydot
  • 224,678
  • 48
  • 389
  • 349
9

you can do like this also:

span {position:relative; margin-left:5px}

span:after {
    content:"|";
    position:absolute;
    left:-5px;
}
span:first-child:after {
    content:"";
}

In this method you can also use others separators like / , \ , .

http://jsfiddle.net/sandeep/UNnxE/

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
sandeep
  • 91,313
  • 23
  • 137
  • 155
4

how about something like this in your example:

<div>
    <span>things</span>
    <span>stuff</span>
    <span>items</span>
</div>

div span{
   border-left: solid black 1px;
}
div span:last-child{
   border:none;
}

no need for additional classes.

Andy
  • 29,707
  • 9
  • 41
  • 58
2

I often want to have a series of items with semi-colons between them.

Here's what I do for this:

.semi-list span:not(:last-of-type)::after {
  content: "; ";
 }

<div class="semi-list">
  <span>Item One</span>
  <span>Item Two</span>
  <span>Item Three</span>
</div>

It's a pretty flexible solution.

Ref:

darelf
  • 4,589
  • 2
  • 16
  • 11
2

Well for a start, you can simplify it to this:

<div>
    <span>things</span>
    <span>stuff</span>
    <span class="end">items</span>
</div>
span {
    border-right: solid black 1px;
}
span.end {
    border-right: none;
}

If you're willing to drop some support in older browsers, you can reduce that to this, using the :last-child pseudo-class:

<div>
    <span>things</span>
    <span>stuff</span>
    <span>items</span>
</div>
span {
    border-right: solid black 1px;
}
span:last-child {
    border-right: none;
}
Eric
  • 95,302
  • 53
  • 242
  • 374
  • @Oliver, And in order to achieve "element1 | element2 | element3" you'd probably want to do border-right instead of border-left. Or make the special case class the first element and not the last. – mclark1129 Oct 11 '11 at 16:55
  • Yeah, I notice that now. I tend to get left & right mixed up (very inconvinient when trying to navigate). – Oliver Oct 11 '11 at 17:06
  • @MikeC: Good point! I would have spotted that if I'd actually read the code I was writing. – Eric Oct 11 '11 at 17:46
1

Something like this?

CSS:

#note_list span {
    display:inline-block;
     padding:0 10px;
}
.notend {
    border-right:1px solid #000000;
}

HTML:

<div id="note_list">
  <span class="notend">things</span>
  <span class="notend">stuff</span>
  <span>items</span>
</div>
Biotox
  • 1,563
  • 10
  • 15