4

I'm trying to dynamically set a FontAwesome glyph inside a span or a button through the CSS property attr(property).

What I would like to have is to set an attribute on the tag

<button glyph="\f005"></button>

and then use it in the CSS file like this

button::before{
     content: attr(glyph)
}

But it looks like it doesn't work and it just display the code I've written in the tag. Is there a way to "render" the code or to make the CSS consider it as an escaped character?

Take a look at this Fiddle for a quick example.

2

2 Answers 2

6

Try setting the value of the glyph attribute to a HTML entity, such as glyph="&#xf005;"

1
  • 1
    Direct and easy, I don't know what I was thinking about, thank you :) Commented Jul 20, 2015 at 14:21
4

First of all, your CSS should be button::before or button::after, not only button. You can use content only in these.

Second, in HTML, you write entities like &XXXX;, not \XXXX, you mixed it up a little. Imagine it that the entity becomes single character and then it is transferred into CSS, not in another way. In HTML, you need to use HTML entities and in CSS, use CSS entities, even though they will travel through both languages in some way.

And third, don't use non-standard attributtes like glyph. They should be prepended with data-.

See http://codepen.io/ondrakoupil/pen/XbBvzV

2
  • Excuse me, the ::before was just a typo, in the Fiddle it was correct. You were correct about the '\' entity instead, I just replaced '\' with &#x as @tenderloin stated and it worked flawlessy. Thank you for your help! Commented Jul 20, 2015 at 14:19
  • Sorry, I didn't look at the fiddle properly. Hope it will help! Commented Jul 20, 2015 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.