Create a pipe separator through CSS

You can often see a number of links distributed horizontally separated by pipe "|", the graphic symbol dell'asticella vertical. . In addition, if there is a good writing HTML, you realize that this list of links - apparently horizontal - are nothing but bulleted lists UL or OL . It follows that instead of writing:

1
2
3
< p >
"#1" > Link 1 < / a > | < a href = "#2" > Link 2 < / a > | < a href = "#n" > Link n < / a > < a href = "# 1"> Link 1 </ a > | < a href = "# 2"> Link 2 </ a > | < a href = "# n"> n Link </ a >
</ p >

It would be better to use:

1
2
3
4
5
< ul >
= "#1" > Link 1 < / a >< / li > < will > < a href = "# 1"> Link 1 </ a > </ them >
= "#2" > Link 2 < / a >< / li > < will > < a href = "# 2"> Link 2 </ a > </ them >
= "#n" > Link n < / a >< / li > < will > < a href = "# n"> n Link </ a > </ them >
</ ul >

But add the pipe as a dirty character code so clean. We can alternatively use a border, using the code:

1
2
3
4
5
6
ul li {
none ; list-style: none;
left ; float: left;
0 12px 0 12px ; padding: 12px 0 0 12px;
1px solid #666 ; border-right: 1px solid # 666;
}

Obtaining:

We note two problems: the first link has the padding on the left and the last link has the pipes to the right, which is not nice to see! del CSS2, per formattare graficamente la nostra lista. This is so that you can use the pseudo-classes :first-child and :last-child of CSS2, graphical format for our list. Using the following style sheet on the first HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ul li {
none ; list-style: none;
left ; float: left;
0 12px 0 12px ; padding: 12px 0 0 12px;
1px solid #666 ; border: 1px solid # 666;
}
/ * Remove padding from the "first child", the first LI * /
ul li: first-child {
0 12px 0 0 ; padding: 0 12px 0 0;
}

/ * Removes the edge "last child", LI * /
ul li: last-child {
0 0 0 12px ; padding: 12px 0 0 0;
none ; border: none;
}

Thus obtaining a better result:

Of course, as often happens, pay attention to compatibility with different browsers, especially Internet Explorer.

Variation of the float

Even better, if you want to avoid the float , use this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
ul li {
none ; list-style: none;
inline ; display: inline;
0 12px 0 12px ; padding: 12px 0 0 12px;
1px solid #666 ; border-right: 1px solid # 666;
}
ul li: first-child {
0 12px 0 0 ; padding: 0 12px 0 0;
}
ul li: last-child {
0 0 0 12px ; padding: 12px 0 0 0;
none ; border: none;
}

Identical results, at least on FireFox :)

4 comments to "Create a pipe separator through CSS"

  1. September 9, 2008 Julius:

    Why, according to your point of view, the syntax

      <A href = "# 1" ..... 

    is worse

      <A href = "....? 

    In terms of coding, I see no obvious benefit, or rather, I see, but still relatively negligible and can be integrated. He's missing something?

  2. September 9, 2008 Giovambattista Fazioli :

    @ Julius: quality of code you tend to write HTML, no matter where the graphic finish. It follows that a "list" of links, being just a list, should be listed as UL LI . Then display it horizontally or vertically, it should be - in fact - the task of CSS. However there is nothing wrong with writing a sequence of links as a series of TAG A . The issue is that eliminating the style sheets as shown in the page? Using a sequence of TAG A , separated by pipes, so they will be visible even in the absence of the style sheet. Using instead a list UL LI page will still look nice and readable without style sheet.

  3. June 12, 2009 SXE:

    Interesting tricks. However, trying the latest solution (replacing the float with display: inline), the layout of the menu changes slightly. That is the padding is not equal to the right and left, as is the case with the float.

    S.

  4. June 15, 2009 Giovambattista Fazioli :

    ; anche se molto dipende dal browser utilizzato. @ SXE: obviously the behavior float is not identical to the display:inline ; although much depends on the browser used. il display:inline è, a mio avviso, migliore rispetto al float che, come ben sai, ha altri problemi di layout… However, acting on the margin and padding on display:inline is, in my opinion, better than the float , as you well know, has other layout problems ...

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL