While working on css3 pseudo classes I came across this beauty, that could be handy just in few cases.
You can read more about attr() specs here http://www.w3.org/TR/css3-values/#attr
Anyway lets talk a little bit more about this function. Key things you should remember are
- In CSS3 attr() can return different types in contrast to CSS2 implementation where it would only return string
- Purpose of attr is data related not presentation related
- It must be used with attribute names such as data-*
There is better way to understand other than when its shown implemented.
Consider this example
We will work with a basic markup and will only define a DIV as shown below
<div id="alt" data-phone="00613999999" data-name="Jaspreet Chahal" data-href="http://jaspreetchahal.org">Look up and down to know more about me</div>
Nothing complicated in our DIV above. The only thing you should note is that I defined few custom attributes such as data-name, data-href, data-phone
Now lets check the CSS side of things
content:"My website is " attr(data-href) ;
border-top:1px solid #0480be;
content:"My name is " attr(data-name) " and you can call me on " attr(data-phone);
border-bottom:1px solid #0000CC;
As you can see that we are using our attr() with content property and we are making use of our custom attributes and using them as fill values for blanks.
See above implementation in action by visiting the example page below
As you can see that CSS is becoming more powerful with every release with functions such as attr() being added/improved. I still sometimes wonder if that is actually an overhead when performance is something that you are looking for.
I hope this helps