HTML5 Semantic Tags
Over the weekend, I was reading Mark Pilgrim's great book on HTML5 - and when I got to the part about the semantic tags, I thought it might be worth a quick mention.
In case you've missed out on HTML5 in general (and don't want to take the time to read that book I linked above), the idea behind semantic tags is that many sites use div blocks to mark out the same kinds of content, over and over. Content like headers, footers, and nav bars. Changing straight <div> tags to tags like <header>, <footer>, and <nav> is granting these tags semantic meaning, hence the name - semantic tags.
Semantic tags are a great idea. They offer a lot advantages over plain vanilla divs, especially for screen readers... but support in IE is pretty broken... The essential problem is this: unlike all other major browsers, IE doesn't know how to style unknown tags. So the following code won't work:
<style>
.border {
border: solid black;
}
</style>
...
<section class="border">test3</section>
Ah, I hear the more informed folks in the audience say, there exists a library to fix this problem: the HTML5 Shiv. You can use it like so:
<!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]-->
This simple script will allow styles to be placed on unknown tags in IE... So, that's a good start, but there are a few problems with it. For one thing, it relies on JavaScript, so if JavaScript is disabled, your styling will fail catastrophically. Similarly, applying print styles may not work, since JavaScript won't necessarily be run as part of the print process (note: I haven't tested this fully, but that's sure what it looks like in brief testing). There are reports that nesting seems to mess stuff up applying styles correctly, but my testing hasn't found anything broken in this way that isn't already broken in IE's CSS support.
Of course, there is a way around even that: If you are running JSF or some other server side processing on your backend, you could do User Agent detection, and emit <div>'s to IE and the semantic tags to all other browsers. Then, by styling the tags solely with classes and ID's, it should be possible to make something that gets around the client side issues. Here's a section from a component that does just that.
@FacesComponent(value = "navtag")
public class NavTag extends UIComponentBase {
@Override
public void encodeBegin(FacesContext context) throws IOException {
boolean isIE = false;
UIComponent component = getCurrentComponent(context);
String style = (String) component.getAttributes().get("style");
String styleClass = (String) component.getAttributes().get("styleClass");
ResponseWriter responseWriter = context.getResponseWriter();
String ua = context.getExternalContext().getRequestHeaderMap().get("User-Agent");
if (ua != null && ua.contains("MSIE") && !ua.contains("Opera")) {
isIE = true;
}
if (isIE) {
responseWriter.startElement("div", null);
} else {
responseWriter.startElement("nav", null);
}
responseWriter.writeAttribute("id", getClientId(context), "id");
responseWriter.writeAttribute("name", getClientId(context), "clientId");
if (styleClass != null) {
responseWriter.writeAttribute("class", styleClass, "styleClass");
}
if (style != null) {
responseWriter.writeAttribute("style", style, "style");
}
}
Should JSF add these tags to JSF 2.1? I'd love to hear your comments, below...
- Login or register to post comments
- Printer-friendly version
- driscoll's blog
- 6054 reads






Comments
Hey Jim - I think it could
by rogerk - 2010-02-09 11:13
Hey Jim -
I think it could be a possibility with some of the other nice features HTML5 has to offer.
I'd be curious to see what the EG and the overall community has to say about it.
-roger