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.
The content CSS property replaces an element with a generated value. Objects inserted using the content property are anonymous replaced elements.
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.
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.
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.
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.
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.
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
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:
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.
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
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With