CSS content and attr

By  on  

CSS is becoming more and more powerful but in the sense that it allows us to do the little things easily.  There have been larger features added like transitions, animations, and transforms, but one feature that goes under the radar is generated content.  You saw a bit of this with CSS counters, where we used the counter and counters expressions to set the content of a given element.  There's another expression, attr, that allows for CSS-based content creation as well. Let me show you how attr an content can work together!

Basic content Usage

The content property allows the developer to statically or (somewhat) dynamically set the content of a pseudo-element:

.myDiv:after { content: "I am hardcoded text from the *content* property";  }

Remember to make the element (not the pseudo-element) position: relative if you plan to absolutely position the pseudo-element.

content and attr

In the case that you'd like to use an element attribute as content (this being the dynamic usage of content), you may do so with the attr expression:

/* <div data-line="1"></div> */

div[data-line]:after { 
	content: attr(data-line); /* no quotes around attribute name! */
}

attr is typically used with custom data- attributes since traditional element attributes are good for functionality but not so much or text presentation.

content Concatenation

Concatenating strings is done via simple spacing:

/* <div data-line="1"></div> */

div[data-line]:after { 
	content: "[line " attr(data-line) "]"; 
}

Trying to concatenate via JavaScript "+" or any other symbol will bomb ... not that I found that out the hard way or anything...

The ability to use generated content with an attr expression is quite exciting.  You can see this used within PrismJS' line-highlighting plugin and a line-number plugin I'll release soon.  These generated content tactics make pseudo-elements all the more valuable too!

Cloudinary

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it's damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

Incredible Demos

  • By
    jQuery Chosen Plugin

    Without a doubt, my least favorite form element is the SELECT element.  The element is almost unstylable, looks different across platforms, has had inconsistent value access, and disaster that is the result of multiple=true is, well, a disaster.  Needless to say, whenever a developer goes...

  • By
    Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

Discussion

  1. thanks for this! I first saw the attr() expression from this codepen: http://codepen.io/TrentWalton/pen/bImfn

    Is there anyway to get the value of the inner text? like .innerHTML in JS?

    seems silly to write the content ‘gzip’ twice, when all we want to do is duplicate the content inside a given element.

  2. dj

    Nice, concise tip. Don’t forget to give the HTML code, which your CSS is working against, within the body of the article text – for ease of reading and clarity sake.

  3. Good writeup, David! I’d like to point out that content and attr are also good solutions for translated/internationalized content. The translated content can be defined as an element attribute instead of creating/maintaining translated content in stylesheet(s).

  4. Aaron

    What is the browser support for this?

  5. Also, this is very useful if you have to provide a printable page, this way you don’t lose any links and other information that might be useful.

  6. I love the trick, but with what I tried, copy/paste doesn’t work if the text is added with CSS ‘content’. In a lot of contexts, it makes this technique unusable as copy/paste really is an awesome feature!

  7. Cameron Bright

    very very USEFUL ! solved my problem, I first tried to use multiple pseudo elements but this is better,

  8. César Pacha

    Is it possible to use this pseudo selector with an image and display a content with it’s url? something like this

    .galImg:after{
    content:attr(src);

    }

  9. span:before{
    content: attr(data-text);
    }
    

    I want to replace “data-text”s content by css. data-text placed in php. so if i chenge ‘content: ‘my text’; is not working. How can i change it?

  10. @César Pacha: In theory yes, though doesn’t seem to support use of the :before and :after pseudo selectors:

    http://codepen.io/anon/pen/GgPdXN

  11. Samuel Willems

    @Edward it does! However img doesn’t suport pseudo elements c;

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!