Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Various possible uses of the "content: " property in css2/css3

I'm trying to find some uptodate info about various possible uses for content: property in css but only find stuff in the ancients dungeons of the web dating from 2004 orso so I thought I have to ask this in 2011 again:

p:before {
content: url(dingdong.png);
}

p:before {
content: "some text ";
}

I'm very new to both the :before selector as well as the content: property and heard of it accidentally on this question which was answered very creatively by a lovely lady:

How to set Bullet colors in UL/LI html lists via CSS without using any images or span tags

Only to find out that some problems might occur concerning the actual encoding of the content:

li:before{ content: "■"; } How to Encode this Special Character as a Bullit in an Email Stationery?

And so my concrete question is: besides url() and "text", are ther other possibilities?
Thanks very much for your suggestions and ideas.

like image 712
Sam Avatar asked Mar 16 '11 03:03

Sam


People also ask

What is the use of content keyword in the following CSS3 property?

The content CSS property replaces an element with a generated value. Objects inserted using the content property are anonymous replaced elements.

What is content property used for?

The content property in CSS is used to generate the content dynamically (during run time) ie., it replaces the element with generated content value. It is used to generate content ::before & ::after pseudo-element.

What is the use of property in CSS?

The Parts of a CSS Rule That declaration is made up of two pieces — the property and the value. The property is the color piece of this declaration. It dictates which aspect of the selector will be changed visually.

What is content tag in CSS?

CSS has a property called content. It can only be used with the pseudo-elements ::after and ::before. It is written like a pseudo selector (with the colon), but it's called a pseudo-element because it's not actually selecting anything that exists on the page but adding something new to the page.

What is the use of CSS content properties?

The property uses text, numbers, url and many more to modify the content based on requirements inside the HTML page. It works with ::before and ::after pseudo-elements and can be used to add generated content in a webpage. Given below are the examples mentioned: The following examples depicts usage of CSS content properties.

What is CSS3 CSS3 used for?

CSS3 is used with HTML to create and format content structure. It is responsible for colours, font properties, text alignments, background images, graphics, tables, etc. It provides the positioning of various elements with the values being fixed, absolute, and relative.

What is the use of the content property?

The contentproperty is used with the ::beforeand ::afterpseudo-elements to generate content inside an element, otherwise the content won’t be generated and inserted. The content always should be added. The property has the following values: normal none counter attr string open-quote close-quote no-open-quote no-close-quote url

Can JavaScript be used with CSS content?

All modern browsers endorse CSS content with pseudo-elements:after/:before. The important thing here is, JavaScript cannot be used but there are also other methods to accomplish a certain result. Given below is the syntax for CSS content property:


3 Answers

Oh, too many to list. Some of the most common cases are:

  • Special numbering, with the counter() function, along with the counter-reset and counter-increment properties

  • Pure CSS clearfix with:

    .foo:after {
        content: "";
        display: block;
        clear: both;
    }
    
  • Display attributes, eg to print URLs for hyperlinks in a print stylesheet

    a[href]:after {
        content: ' (' attr(href) ') ';
    }
    
  • Add typographic ornaments that shouldn't be in the HTML because they're presentational. For example, in my blog, I've used it for the ornaments between posts or sidebar links.

  • Add icons to hyperlinks, depending on where they point, like

    a[href^="http://twitter.com/"]:before {
        content: url('twitter-icon.png');
    }
    
  • Adding a pointer to make a CSS-only speech bubble:

    .bubble {
        position: relative;
        background: silver;
    }
    
    .bubble:after {
        content: "";
        border:10px solid transparent;
        border-top-color:silver;
        position: absolute;
        bottom:-20px
    }
    

And many, many other.

Just beware: If something is not presentational, it should probably be in your HTML. Users will not be able to select CSS generated content, and screen readers will ignore it.

like image 108
Lea Verou Avatar answered Oct 02 '22 03:10

Lea Verou


You can also use a counter. See http://www.w3schools.com/css/tryit.asp?filename=trycss_content_counter

You can also display a certain attribute of the element selected. See http://jsfiddle.net/EcnM2/

You can also add or remove opening and closing quotes.

w3schools content property list: http://www.w3schools.com/css/pr_gen_content.asp

like image 44
Peter Olson Avatar answered Oct 02 '22 04:10

Peter Olson


Generated content won't be perceived by screen readers so beware of accessibility issues.
content is very useful but there are cases where this text should be in the HTML code because it conveys information and isn't only decorative (a bit like background images in CSS vs informative img with a non-empty alt attribute)

  • :after and content can be used as a clearfix with no extra div
  • :before and :after bring multiple backgrounds (up to 3 w/ the element itself) to browsers that don't understand the CSS3 feature.

EDIT: forgot about Eric Meyer's article in A List Apart about printing the href attribute of links along with their text with the help of content (it was followed by a JS improvement, fyi)

like image 38
FelipeAls Avatar answered Oct 02 '22 05:10

FelipeAls