Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separators between elements without hacks

Tags:

html

css

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.

like image 945
Oliver Avatar asked Oct 11 '11 16:10

Oliver


4 Answers

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.

like image 188
thirtydot Avatar answered Oct 17 '22 21:10

thirtydot


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/

like image 7
sandeep Avatar answered Oct 17 '22 21:10

sandeep


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.

like image 4
Andy Avatar answered Oct 17 '22 21:10

Andy


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;
}
like image 2
Eric Avatar answered Oct 17 '22 22:10

Eric