Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Terse Font Icons
a[data-icon]:before,
span[data-icon]:before {
font-family: 'Icon Font Here';
content: attr(data-icon);
}
/*
This would also work, but other web developers might
yell at you about "performance" which may/not matter:
*/
*[data-icon]:before {
font-family: 'Icon Font Here';
content: attr(data-icon);
}
<!-- Assuming "+" is your font's Add icon -->
<a data-icon="+" href="...">
Add
</a>
<!-- Assuming "D" is your font's Delete icon -->
<span data-icon="D">
Delete
</span>
@ahmedelgabri

This comment has been minimized.

Copy link

@ahmedelgabri ahmedelgabri commented Jan 29, 2012

Why not just use this without the "*"?

[data-icon]:before {
   font-family: 'Icon Font Here';
   content: attr(data-icon);
}
@nathansmith

This comment has been minimized.

Copy link
Owner Author

@nathansmith nathansmith commented Jan 29, 2012

If you want to save one byte by not having it in the stylesheet, go for it.

As far as browsers are concerned, it probably wouldn't matter either way.

I just had the star selector there for proper code highlighting.

Neither TextMate, nor Sublime Text 2, acknowledge attribute selectors without a preceding tag (or *).


TextMate screenshot:
http://cl.ly/1f333U2G3d2O3U0X2E2p

Sublime Text 2 screenshot:
http://cl.ly/1V3G1f3W132h0r3m0R0c

@ahmedelgabri

This comment has been minimized.

Copy link

@ahmedelgabri ahmedelgabri commented Jan 29, 2012

Since browsers parse CSS selectors from right to left, I understand that it won't make a huge difference. Cause in both cases the browser will look for the attribute in all elements anyway.

I know about the highlight issue. I just thought you might have some other reason other than saving bytes or performance, that's why I asked.

@nathansmith

This comment has been minimized.

Copy link
Owner Author

@nathansmith nathansmith commented Jan 29, 2012

Nope, no good reason, really. Just a cut-and-paste from my text editor. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.