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!

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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    Add Site Screenshots for External Links Using MooTools Tooltips

    Before you send your user to an unknown external website, why not provide them a screenshot of the site via a tooltip so they may preview the upcoming page? Here's how you can do just that using MooTools. The MooTools JavaScript The first step is to grab...

  • By
    Hot Effect: MooTools Drag Opacity

    As you should already know, the best visual features of a website are usually held within the most subtle of details. One simple trick that usually makes a big different is the use of opacity and fading. Another awesome MooTools functionality is...

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!