tags:

views:

218

answers:

8

Does css "content" property break the rule of content and separation because css is for presentation not to generation content?

By the way What are other good uses of css "Content" property? i've seen this only in clear fix hack.

A: 

One of interesting use cases would be localization of a User Interface.

Sergey Ilinsky
+5  A: 

Does css "content" property break the rule of content and separation because css is for presentation not to generation content?

Good point. I'd say it does if it's used for actual data.

The quirksmode page on content shows the limitations pretty well. You can't add any kind of styled content at the moment - it will work with too few browsers. You can add only character data.

The author of the quirksmode airs an interesting opinion:

I feel that we shouldn't use the content declaration at all. It adds content to the page, and CSS is meant for adding presentation to the page, and not content. Therefore I feel that you should use JavaScript if you want to dynamically generate content. CSS is the wrong tool for this job.

I agree with this in general, but sometimes there may be cases where you don't want to rely on JavaScript to do the job. The comma example shown by Martin is a case where I find using content justified (although I personally would be feeling better if the commas would already be served coming from server side - it's what I personally would stick to.)

Also, keep in mind that adding commas and quotes through the content property may look bad when your content is viewed from elsewhere - for example in a search results page.

I'd say use it only sparingly, if you really need it.

Pekka
so should we use this in a limited way?
metal-gear-solid
@jitendra I think yes. I edited the answer a bit.
Pekka
we are getting very good examples in answer , should i convert this question into community wiki bcoz it would be difficult for me to choose one answer
metal-gear-solid
@jitendra I had the same thought, probably a good idea.
Pekka
Would i loose all current rep of this question and would not get any rep if i choose this as a community wiki
metal-gear-solid
@Jitendra nobody will get any reputation anymore from the point you make it CW. Any reputation already gained will stay in place.
Pekka
ok i marked this as a wiki bcoz all answers are good
metal-gear-solid
@Pekka - What is the benefit to collect rep?
metal-gear-solid
Way to forego rep for the benefit of the people. You're a hero, sir.
dclowd9901
@jitendra: Reputation is good for having the dough to run +550 bounty questions once in a while. They are great fun. :)
Pekka
A: 

A common use I see for it is with :before and :after tags to format quotations with some sort of stylized quote box. It can be a quick and easy way to get in stylized elements that you would otherwise have build images out of.

blockquote:before, blockquote:after {
    content: '"';
}

I think this is an okay use for it, because it doesn't really break rules of content and style separation. My feeling is that if it is part of the design of the page, rather than the content, it's probably okay for content:

dclowd9901
Why do you declare the property `content` twice?
codescape
Just a dumb typo :\
dclowd9901
+3  A: 

CSS is presentational data. Any content that's only presentation-related is fine in a CSS file. For instance, suppose I want to put « and » around my <h1> tags; that's purely presentational. You could do it with the :before and :after selectors.

It should also be noted that content can also display images:

content: url('my/image.png');

I'd like to add, on a side note, that I'd consider the use of the content property to override already existing content an extremely bad practice.

zneak
Images work in Opera only though, don't they? The quirksmode test page isn't working for me in Firefox 3.6.
Pekka
zneak
Odd. I get `Your browser does not support content.` (Windows 7). Works on Chrome though.
Pekka
@Pekka: At least it seems to work with `:before` and `:after`. I just tested the quirksmode page and it fails as well; however on my local tests using the aforementioned selectors, everything goes well. (Mac OS here.)
zneak
@zneak may be something with the quirksmode page in that case. Strange!
Pekka
@Pekka: The quirksmode page uses `content` to override already-existing contents. Perhaps Firefox doesn't like it.
zneak
@zneak ah ok, that could be the reason.
Pekka
+3  A: 

One popular place that this shows up is in WordPress' default theme

.entry ul li:before, #sidebar ul ul li:before {
    content:"» ";
}

alt text

Justin Johnson
+1 This use I find perfectly fine.
Pekka
What's the downvote for?
Justin Johnson
+3  A: 

It's good for structured content. I wrote a number of test cases for the W3C's next print CSS rules and the one that seemed cool to me was being able to put "Chapter " and things like that into certain elements, especially when paired with counters. A simplistic example would be something like:

li.chapter:before {content: "Chapter" counter(chapter) ": ";}

None of that's print-specific and it's all presentation information. If you don't want your chapters to be preceded with the word "Chapter", take it out of the CSS. Controlling that in a stylesheet means your print version could have different chapter headings from your screen version your mobile could be different again, without having to have any knowledge of the viewer's device inside your application logic.

Tom
+1  A: 

I'm using it to display accesskey in admin panel menu

.menu a[accesskey]:after { content:' [' attr(accesskey) ']'; }
dev-null-dweller
A: 

I just want to add to what has already been said.

With Cascading Style Sheets you can apply styles to a lot of types of documents.

The common use case is to apply CSS to HTML pages. In this case, the general idea is to use the content property only for aesthetic purposes.

Another use case is instead to apply CSS to XML documents. In this case the document usually does not contain elements for page structure (div, h1, etc...). So, in this scenario, by using the content CSS property more frequently, you can better define the page and the relations between elements and data.

For example you could prepend a description paragraph before a table, or appending the email address after the name of a person. Note that in HTML pages these page-structure elements should be part of the HTML document itself while they are usually omitted in a XML document and so they can be added using the content CSS property.

Andrea Zilio