Forum How do I...?

Working with optional attributes?

_savage
Having a long title like this
<h1 short="Brief title">This is a very long title for this section</h1>
it would occupy two lines in the header of the left pages. That looks kind of ugly, so I added a "short" version of the title as an attribute.

However, since some titles are actually brief enough the "short" attribute is optional and doesn't go with every <h1> element.

My CSS at the moment is this:
h1 {
  font-size: 18pt;
  text-align: center;
  string-set: chaptertitle content(); /* attr(short); */
}
Was I to use the attr() here then, it seems, it would have to become a required attribute.

Is there a way to select an attribute if it exists, and if not, the element's content? Or will I have to provide "short" for every <h1> element?
mikeday
You can use attribute selectors to check for the existence of an attribute, like this:
h1 { string-set: chaptertitle content() }
h1[short] { string-set: chaptertitle attr(short) }
_savage
Thanks Mike :)

Because the .xhtml file (input to Prince) is auto-generated, I can also easily provide the "short" attribute for every <h1> element. Either works, I like your solution better though, as it reduces redundancy in the markup.