Character Styles

The next type of style definition, character styles are styles which apply to the contents of one or more runs of text within a document’s contents. This definition implies that the style can only define character properties (properties which apply to text within a paragraph) because it cannot be applied to paragraphs. Character styles can only be referenced by runs within a document, and they must be referenced by the <rStyle> element within a run’s run properties element.

A character style has two defining type-specific characteristics:

  • The @type attribute on the style has a value of character, which indicates that the following style definition is a character style.

  • The style specifies only character-level properties using the <rPr> element. In this case, the run properties are the set of properties applied to each run which is of this style.

The character style is then applied to runs by referencing the @styleId attribute value for this style in the run properties’ <rStyle> element.

Consider a character style titled "Test Character Style" which defines; font = Courier New, font color = yellow; underline. The resulting style definition would be:

<w:style w:type="character" w:styleId="TestCharacterStyle">
  <w:name w:val="Test Character Style"/>
  <w:priority w:val="99"/>
  <w:qformat/>
  <w:rsid w:val="00E77BF0"/>
  <w:rPr>
    <w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
    <w:color w:val="FFF200"/>
    <w:u w:val="single"/>
  </w:rPr>
</w:style>

Notice that the character properties applied using this style are under the <rPr> element. The document content for a paragraph with a run of this style would be:

<w:p>
  <w:r>
    <w:t xml:space="preserve">The following text is in the </w:t>
  </w:r>
  <w:r>
    <w:rPr>
      <w:rStyle w:val="TestCharacterStyle"/>
    </w:rPr>
    <w:t>character style</w:t>
  </w:r>
  <w:r>
    <w:t>.</w:t>
  </w:r>
</w:p>

The <rStyle> element in the second run links that run with the style definition, inheriting the formatting properties for that run.