Create a pipe separator through CSS

Tuesday, September 9, 2008

It often see a number of links distributed horizontally separated by a pipe "|", the graphic symbol dell'asticella vertical. Moreover, if you have good writing HTML, you find that this series of links - apparently horizontal - others are not betting that lists UL or OL The result is that instead of writing:

HTML:
  1. <p>
  2. > 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>
  3. </ p>

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>
  4. </ ul>

But add the pipe as a dirty code so clean. We can use an alternative edge, through the code:

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

Getting:

We can see two problems: the first link has the padding on the left and the last link has the pipe to his right, not what looks good! So that can be used :first-child :last-child of CSS2, graphical format for our list. Using the following style sheet on the HTML code shown before:

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

Thereby obtaining a better result:

Of course, as often happens, attention to the 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. ; list-style: none;
  3. ; display: inline;
  4. 12px; padding: 0 12px 0 12px;
  5. border-right: 1px solid # 666;
  6. )
  7. ul li: first-child (
  8. ; padding: 0 12px 0 0;
  9. )
  10. ul li: last-child (
  11. 0 0 12px; padding: 0 0 0 12px;
  12. ; border: none;
  13. )

Same result, at least on FireFox :)

Related Post

2 comments to: "Create a pipe separator through CSS"

  1. Tuesday 09 September, 2008 at 10:20 a.m.
    Julius said:

    Why, according to your point of view, the syntax

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

    is worse

      <A href = "....? 

    In terms of coding, I do not see any obvious advantage, or rather, I see it, but relatively insignificant and integrated anyway. I am avoiding something?

    Answer Quote
  2. Tuesday 09 September, 2008 at 11:21 a.m.
    Giovambattista Fazioli said:

    @ Julius: quality of code we tend to write the HTML not looking after the final chart appearance. It follows that a "list" of links, being just a list, should be listed as UL LI Then, horizontal or vertical display, it should be - indeed - the task of CSS style sheet. But there is nothing wrong with writing a sequence of Link as a series of TAG A The issue is that eliminating the stylesheets as is the page? Using a sequence of TAG A separated by pipe, they will be so visible even in the absence of style sheet. Using instead a list UL LI page will still look nice and readable even without style sheet.

    Answer Quote

Leave a comment

TAG XHTML <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <a <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Use <pre> to enclose code