Some of the more advanced pseudo-classes and psuedo-elements available in recent versions of CSS (CSS 2.1 and the proposed CSS 3) are extremely useful when styling XML directly with CSS. Though not available in all browsers, their use is gradually becoming more widespread. Here's a selection of the most useful:
Styling the first of a group of elements - :first-child. Applies a style to an element if it is the first child of its parent. This can
be used to emphasise the first line of a poem, for example, or to provide
different formatting for the first item of a list.
chapter { display: block }
chapter:first-child { color: blue }
Here, the first chapter in the list will be coloured blue [Example: XML | CSS].
Styling the first of a group of lines or letters - :first-line
and :first-letter.
p { font-size: 12pt; line-height: 1.2 }
p:first-letter { font-size: 200%; float:left }
Adding text before/after an element - :before and
:after. The following will add the word 'Warning' to any paragraphs with
class="warning":
p.warning:before { content: "Warning: " }
and this rule will add a number before each h1 element, counting up with roman numerals:
h1:before { content: counter(chapno, upper-roman) ". " }
note here that the chapno is an identifier for the counter; any other numbering within the document would need to be identified by a different name. You can also reset the counters within rules, so that if numbering sections within a chapter, you could reset the numbers at each new chapter.
There are many other new rule types in the newer CSS (2.1) standards, which allow many useful effects to be created. However, there are still limitations to using CSS to style XML, most notably that it's often useful to re-order or filter the document before display. Whilst the newer CSS layout techniques can help with this, there is a better tool for the job in XSLT, which is purpose-designed for this role. We'll begin examining XSLT next week.
|
Browser compatibility |
|---|---|
|
Note that many of the CSS 2.1 functions are only available in a few browsers - Firefox seems the most reliable for this at the moment - though eventually, they should be supported in all browsers. For the purposes of the assignment, I'm quite happy for you to develop CSS which uses the newer features, rather than trying to produce effects that work across all browsers - but don't forget to mention in your report which browsers you used to test the CSS in! |
|
Further reading... |
|---|---|
|
The W3C have produced a guide to adding style to XML. |