Create a pipe separator through CSS

Tuesday, September 9, 2008

You'll often see a series of links distributed horizontally separated by a pipe "|", the graphic symbol dell'asticella vertical. Also, if you have a good writing HTML, you realize that this series of links - apparently horizontal - are nothing but bullets UL or OL It follows that instead of writing:

HTML:
  1. <p>
  2. "#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"> Link n </ a>

It would be better to use:

HTML:
  1. > Link 1 < / a>< / li> <li> <a href = "#1"> Link 1 </ a> </ li>
  2. > Link 2 < / a>< / li> <li> <a href = "#2"> Link 2 </ a> </ li>
  3. > Link n < / a>< / li> <li> <a href = "#n"> Link n </ a> </ li>

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

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

Obtaining:

We can see two problems: the first link has the padding on the left and the last link has the pipe to his right, which is not nice to see! So here may be to use :first-child :last-child of CSS2 to format graphically our list. Using the following stylesheet to the HTML code shown above:

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

Thereby obtain a better result:

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

Variant to float

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

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

Same result, at least in FireFox :)

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

4 comments to "Create a pipe separator through CSS"

  1. getAvatar 1.0
    09 set, 2008 Giulio

    Why, according to your point of view, the syntax

      <a href = "# 1" ..... 

    is worse

      <a href = "....? 

    In terms of coding, I see no obvious advantage, or rather, I see, but still relatively negligible and integrable. He's missed something?

  2. getAvatar 1.0
    09 set, 2008 Giovambattista Fazioli:

    @ Julius: quality of code you tend to write the HTML but neglect the graphic end. It follows that a "list" of links, being just a list, should be listed as UL LI Then, displayed horizontally or vertically, it should be - precisely - 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 tag sequence A separated by pipe, they will be so visible even in the absence of the style sheet. Using instead a list UL LI page will still look nice and readable without the style sheet.

  3. getAvatar 1.0
    June 12, 2009 sXe:

    Interesting trick. However trying the latter option (replace the float with display: inline), the layout of the menu changes slightly. Or the padding is not equal to the right and left, as is the case with the float.

    S.

  4. getAvatar 1.0
    June 15, 2009 Giovambattista Fazioli:

    @ sXe: obviously the behavior float is not identical to the display:inline although much depends on the browser used. 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

TAG XHTML PERMISSIONS: <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 [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL