Style Sheets
  Home arrow Style Sheets arrow Page 6 - Learn CSS, part 7: Pseudo Elements
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Learn CSS, part 7: Pseudo Elements
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2005-06-27

    Table of Contents:
  • Learn CSS, part 7: Pseudo Elements
  • Using the ::first-line Pseudo Element
  • Using the ::first-letter Pseudo Element
  • Using the ::selection Pseudo Element
  • The Content Property
  • The ::before and ::after Pseudo Elements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Learn CSS, part 7: Pseudo Elements - The ::before and ::after Pseudo Elements


    (Page 6 of 6 )

    These pseudo elements give you the ability to insert text or images before (using ::before) the content of the element and after the content of the element (using ::after), through the use of the content property. They are very useful if you have some string values that are repeated in the HTML code a lot. Instead of sending them many times, increasing the size of the page and using more bandwidth, you can send these string values only once in the CSS file (because browsers will cache the CSS file). Use this in Web pages as much as you can, because doing so will save the page size -- but be careful to use  generated content, because it has not yet been implemented by IE.

    Let's look at an example. In the next CSS code we set the before and after content for any <p> element that has the class QandA (note the power of the combination of element selector, class selector and the pseudo element offered by CSS):

    p.QandA
    {
      background-color: cyan;
      color: red;
      font-family: Arial;
    }
    p.QandA::before
    {
      content: "Question: ";
      font-family: Arial;
      font-size: 1.1em; 
      color: white;  
      background-color: blue;
    }
    p.QandA::after
    {
      content: " ?";
      font-family: Arial;
      font-size: 1.1em;
      color: white;
      background-color: blue;
    }


    And here's the HTML code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <link rel="stylesheet" href="PElements.css" type="text/css">
        <title>CSS Pseudo Elements</title>
      </head>
      <body>
        <p class="QandA">Do you like CSS</p>
      </body>
    </html>

     

    The ::before pseudo element has inserted the value of its content property BEFORE the content of the <p> element, and has applied the styling rules too. The same thing applies to the ::after pseudo element.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hi, I was trying to keep up but do better with a full working example. Thank You
       · Thanks for your comment but I thought that all the readers can create a style sheet...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek