CSS Pseudo Elements Selectors

What about the Pseudo-element selectors.

  • Pseudo-element selectors let you assign styles to structures that don't necessarily exist in the document.
  • Pseudo-element selectors provide the effect of extra markup elements magically appearing in your document, although you don't actually add any extra markup.

four pseudo-elements:

  • The :first-line pseudo-element


    Applies special styles to the contents of the first formatted line of an element.
    Example of the :first-line pseudo-element selector:
    <html>
      <head>
        <style type="text/css">
          p {width: 400px; text-align: center;}
          p:first-line {color: blue ; }
        </style>
      </head>
      <body>
        <p>This is a somewhat long HTML
          paragraph that will be broken into several
          lines. The first line will be identified
          by a fictional tag sequence. The other lines
          will be treated as ordinary lines in the
          paragraph.</p>
        <p> This is a somewhat long HTML paragraph <br/>
          that will be broken into several lines.
          The first line is fixed by a break (br) element.
          The other lines will be treated as ordinary
          lines in the paragraph.</p>
      </body>
    </html>
    CSS 2.1 allows the following properties used with: first-line pseudo-element selector:
    font properties, color property, background properties, 'word-spacing', 'letter-spacing', 'text-decoration', 'vertical-align', 'text-transform' and 'line-height'.
  • The :first-letter pseudo-element


    Selects the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line.
    Example of the :first-letter pseudo-element selector:
    <html>
      <head>
        <style type="text/css">
          p:first-letter { font-size: 3em; font-weight: normal }
          p:first-line { background-color: lightgrey; }
        </style>
      </head>
      <body>
        <p>The :first-letter pseudo-element may be
        used for "initial caps" and "drop caps",
        which are common typographical effects.</p>
      </body>
    </html>
  • The :before and :after pseudo-elements


    Can be used to insert generated content before and after an element's content.
    Example of :before and :after pseudo-element selectors:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
      <head>
        <style type="text/css">
          td.age:before {content:"is "}
          td.age:after {content:" years old."}
        </style>
      </head>
      <body>
        <table border="0">
          <tr>
            <td>Ricard</td>
            <td class="age">25</td>
          </tr>
          <tr>
            <td>Jack</td>
            <td class="age">31</td>
          </tr>
        </table>
      </body>
    </html>
    Note: For :before and :after to work in IE a <!DOCTYPE> must be declared.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.