Hostgator coupons for up to 99% off hosting for Dynamic Drive visitors!
Advanced Search Usage Terms Submit Contact
Dynamic Drive CSS Library

CSS Library: Other: Here

CSS Generated Content

Author: Dynamic Drive

CSS2 supports the insertion of generated content, which is content that's specified using your style sheet and applied dynamically to the page according to your CSS definition. This is very useful for further decorating an element with rich content that should not be part of the page's actual markup. CSS Generated Content is accomplished using the ":before" and ":after" pseudo-classes, which denotes whether the content should be added before or after the element in question. You can insert regular "string" content or an embedded object (ie: an image). CSS2's Generated Content is supported in Mozilla/Firefox and Opera 7+, and IE7+

In our first example, we'll automatically add the decorative text ":: " in front of all H3 headers using CSS generated content, and make it red as well:

Dynamic Drive CSS Library

If you're using a capable browser, you should see the effect.

In the next example, you'll see how to include an image as part of the generated content:

The CSS:


Code Info

Rate this code:

Date Posted: 05/02/2006

Revision History: None

Usage Terms: Click here

Your Comments (34)

Got a question or need help customizing this CSS code? Post it in the CSS Forums. If you have a comment or suggestion instead, post it in the comments section below.

Comment Pages 1 of 4 pages  1 2 3 >  Last »

Not supported by IE
Posted by MtD on 05/10, 01:29 PM
How about IE 7 ?? Does it supported??
Posted by Kuswanto on 05/22, 08:53 AM
Couldn't you do something like
a[href*=".zip"]:after {} 
Posted by computerdude33 on 05/24, 03:41 PM
To Kuswanto:

CSS2's Generated Content is supported in Mozilla/Firefox and Opera 7+, and IE7+
Posted by computerdude33 on 05/25, 11:45 AM
You never mention the browser Safari for Mac, which supports most CSS code.
Posted by Andrea Bornstein on 05/28, 04:01 AM
umm, i can't get this. what's so special on this function?

you're defining some class at the link and some attributes for h3 in the css.

instead of using this :before and :after-thingies, i'd prefer using a simple background-image including some padding-tags for positioning. at least this would work correct with most browsers.

why this way, where's the difference?

i really can't understand this...
Posted by sph on 05/28, 12:38 PM
Hi Sph:
Well, there's nothing "special" about generated content in CSS except the concept itself. The idea is to use CSS Generated Content to dynamically display decorative content that are part of an element's style rather than substance. Using a background image is a lot more tedious in many cases, plus there are instances where you just can't duplicate the same effect using a background image.

See the following tutorial on Generated Content to see what's possible:
Posted by ddadmin on 05/28, 02:39 PM
okay, now it's getting clear :)

it's not only an "add-the-image"-thing, eh? ;)

thanx for the link!
Posted by sph on 05/28, 05:04 PM
Better if it was supported in IE4+ becuase I do not like using any other type of browser.
Posted by Callum Haywood on 07/15, 09:56 AM
Wow! CSS is getting closer to PHP all the time. The only time I ever used php was to modify an autogenerated page with formating and other stuff (like adding tracking) Now all css needs is a way to find any text on the page and insert stuff :before and :after :)
Posted by Veggiet on 07/25, 06:34 PM

Comment Pages 1 of 4 pages  1 2 3 >  Last »

Commenting is not available in this weblog entry.
Copyright 2006-2015 Dynamic Drive Read our Usage Terms before using any of the CSS codes.