Relevant codepen: http://codepen.io/anon/pen/ocptF/
EDIT: The codepen uses Jade, and thus messes a few things up. I was not aware of this when starting this question. Essentially, I thought CSS attr() would copy over an HTML attribute literally, but that is not the case.
I'd like to use the CSS attr
function to fill in the content
for some pseudoelements. However, it prints out 004
when the HTML attribute is set to \f004
, and 08fa
when \f08fa
.
Relevant lines:
HTML:
<div class="zoomfade" data-fill='\f004' data-unfill='\f08a'></div>
CSS:
.zoomfade:before {
content: attr(data-unfill);
position: absolute;
}
.zoomfade:before {
content: attr(data-fill);
position: absolute;
}
Thanks!
Escape sequences in CSS are only treated specially in CSS syntax. When you specify it in an HTML attribute value then use that value in CSS attr()
, it is taken literally. From the CSS2.1 spec:
attr(X)
This function returns as a string the value of attribute X for the subject of the selector. The string is not parsed by the CSS processor. [...]
Since you're specifying character codes in HTML attribute values, you can either use HTML character references, entity references or the Unicode characters themselves. It's worth noting that the two character codes you have do not appear to be valid, however, so they may not work at all.
EDIT: [...] Essentially, I thought CSS attr() would copy over an HTML attribute literally, but that is not the case.
It copies the attribute value according to the DOM, which may be different from how it is represented in the source, e.g. the source markup, or the script that is generating the element.
For example, if the source is represented in raw HTML markup, then as I mention above, you will need to use HTML character escapes, because HTML is parsed by an HTML parser. If the elements are generated using a JS-based template engine such as Jade, then the character escapes take the form of \u
followed by the hexadecimal code-points. In both cases, the respective parsers will translate the escape sequences into their representative characters, and the characters themselves are what is stored in the DOM as part of the attribute value.
Of course, again there's always the alternative of just using the Unicode characters directly. If your source files are all encoded as UTF-8, you should have no problem using the characters directly.
来源:https://stackoverflow.com/questions/22600464/html-special-characters-in-css-content-using-attr