31.7.10 Overriding CSS class for selected paragraphs

Paragraphs that have distinct purposes in your document should have distinct format names, even if they share the same print format. However, if your document does contain paragraphs with the same format name that need different CSS classes, you can use Code PI markers to flag those paragraphs, and assign a different class with a macro.

For example, suppose most of your Heading 2 paragraphs are assigned CSS class Heading2, but a few Heading 2 paragraphs need one of three other classes: About, Configuration, or Procedure. You can surround all Heading 2 paragraphs with code to hold the HTML tags and class assignments:

[HtmlStyles]
Heading 2=NoPara CodeBefore CodeAfter

The starting H2 tag assigns a class whose value is computed by macro $UseH2Class:

[StyleCodeBefore]
Heading 2=<H2 class="<$UseH2Class>">

The closing H2 tag follows the paragraph:

[StyleCodeAfter]
Heading 2=</H2>

Macro $UseH2Class checks the value of macro variable $$h2class to determine which class to assign:

[UseH2Class]
<$_if ($$h2class is "A")>About\
  <$_elseif ($$h2class is "C")>Configuration\
  <$_elseif ($$h2class is "P")>Procedure\
  <$_else>Heading2\
  <$_endif>
<$$h2class="H">\

Macro variable $$h2class is initialized (and always reset) to a value that results in assigning the default class, Heading2 (via the $_else clause in macro $UseH2Class):

[MacroVariables]
h2class=H

To set $$h2class for a paragraph that needs a non-default class, you would insert a Code PI marker in the paragraph that precedes each such paragraph. The content of the PI marker would look like this:

<$$h2class="A">

To assign a non-default class to the very first paragraph in a file, you would have to create a chapter-specific configuration file, filename.ini, for that file, with content (for example):

[MacroVariables]
h2class=A

See §42.1.1 Providing configuration files for individual ditamaps.

See also:

§37. Working with macros

§38. Working with processing instructions

§42. Overriding configuration settings

Previous Topic:  31.7.9 Omitting tags from CSS selectors

Next Topic:  31.8 Customizing CSS properties

Parent Topic:  31.7 Assigning CSS classes

Sibling Topics:

31.7.1 Understanding CSS class name restrictions

31.7.2 Mapping paragraph formats to CSS classes

31.7.3 Mapping character formats to tags or span classes

31.7.4 Assigning CSS classes to table formats

31.7.5 Assigning CSS classes to text and table footnotes

31.7.6 Assigning CSS classes based on Unicode character ranges

31.7.7 Using link format names as CSS class names

31.7.8 Using CSS class names as tags for XML

31.7.9 Omitting tags from CSS selectors