---
comments: true
date: 2010-09-27 21:37:07
layout: post
slug: keeping-code-clean-with-content
title: Keeping code clean with content
wordpress_id: 1388
categories:
- Web Development
---
The CSS `content` property is one that has been around for a while. It's not new, nor is it particularly ground-breaking. It is however, at least in my opinion, extremely useful and extremely underused. For those not in the know, `content` sets, well, the content of an element via CSS. That is to say it gets rendered in the page but doesn't appear in the markup. Coupled with the `:before` or `:after` pseudo-elements you can prepend or append content to an element respectively:
/* Add a pilcrow before paragraphs */
p:before{
content:"¶ ";
}
/* Add a bullet after the last paragraph */
p:last-of-type:after{
content:" •";
}
The benefit of this is that things that are technically stylistic that could only really be placed in the markup in order to make an appearance can actually be 'injected' into the page through CSS. This keeps your code free from any stylistic markup that adds non-semantic value, and means that any markup-like elements are added through the stylesheet.
A particular example I have been using lately is simply:
The great thing here, and this is _extremely_ die-hard web standards and semantics, is that an item's title is not _technically_ 'Name:' or 'Age:', rather it is just 'Name' and 'Age'. The colon is, if you are being very anal, just stylistic.
Also, another benefit is data-purity in a database. Imagine, for whatever reason, you're populating that `
/* CSS */
dt:after{
content:":";
}
- =$userNameTitle; ?>:
- =$userName; ?>
- =$userAgeTitle; ?>:
- =$userAge; ?>
Notice the trailing colons on the `h2:after{
content:"\0097"; /* Add an em dash after headings */
}