This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Styling XML data with CSS


This article shows how you can use CSS to style XML data.

Information: XML data

XML (eXtensible Markup Language) is a general-purpose language for any kind of structured data.

By default, your Mozilla browser displays XML in a format very similar to the original data in the XML file. You see the actual tags that define the data’s structure.

But by linking a CSS stylesheet with the XML document, you can define other ways to display it. To do this, your stylesheet uses rules that map tags in the XML document to the display types used by HTML.


For example, you have data in an XML document uses <INFO> tags. You want the document’s INFO elements to be displayed like HTML paragraphs.

In the document’s stylesheet, you specify how the <INFO> elements are to be displayed:

  display: block;
  margin: 1em 0;

The most common values for the display property are:

|block|Displayed like HTML’s DIV (for headings, paragraphs)|
|inline|Displayed like HTML’s SPAN (for emphasis within text)|

Add your own style rules that specify the font, spacing and other details in the same way as for HTML. Other values of display display the element like a list item, or like a component of a table.

For the full list of display types, see the display property. Using CSS alone, the structure of the display must be the same as the structure of the document. Other technologies can modify the structure of the display—for example, XBL can add content, and JavaScript can modify the DOM.

Action: An XML demonstration

  1. Make a new XML file, doc9.xml. Copy and paste the content from here, making sure that you scroll to get all of it:

    <?xml version="1.0"?>
    <!-- XML demonstration -->
    <?xml-stylesheet type="text/css" href="style9.css"?>
    <!DOCTYPE planet>
  2. Make a new CSS file, style9.css. Copy and paste the content from here, making sure that you scroll to get all of it:

    /*** XML demonstration ***/
    planet:before {
      display: block;
      width: 8em;
      font-weight: bold;
      font-size: 200%;
      content: "Oceans";
      margin: -.75em 0px .25em -.25em;
      padding: .1em .25em;
      background-color: #cdf;
    planet {
      display: block;
      margin: 2em 1em;
      border: 4px solid #cdf;
      padding: 0px 1em;
      background-color: white;
    ocean {
      display: block;
      margin-bottom: 1em;
    name {
      display: block;
      font-weight: bold;
      font-size: 150%;
    area {
      display: block;
    area:before {
      content: "Area: ";
    area:after {
      content: " million km\B2";
    depth {
      display: block;
    depth:before {
      content: "Mean depth: ";
    depth:after {
      content: " m";
  3. Open the document in your browser:

  • The superscript 2 (in “million km²”) a Unicode character, coded as \B2 in the CSS file.
  • The heading, "Oceans", has a negative top margin, moving it up so it is displayed on top of the border.

See also

Exercise question

Change the stylesheet so that it displays the document as a table.


  • This article contains content originally from external sources, including ones licensed under the CC-BY-SA license. cc-by-sa-small-wpd.png

  • Portions of this content copyright 2012 Mozilla Contributors. This article contains work licensed under the Creative Commons Attribution-Sharealike License v2.5 or later. The original work is available at Mozilla Developer Network: Article