Path: blob/main/files/en-us/related/imsc/styling/index.md
6581 views
------IMSC offers many options for styling documents, and most of the IMSC styling properties have direct CSS equivalents, making them familiar to web developers. In this guide you'll learn a bit more about IMSC styling including the difference between inline and referential styling, and efficient styling using inheritance and region styling.
Inline styling
The simplest way of styling content elements like <p> or <span> is by specifying one or more style attributes, such as tts:color, on them. For instance, the following
yields:
{{EmbedGHLiveSample("imsc-examples/inline-styles/inline-styles.html", '100%')}}
Referential styling
Inline styling is usually avoided since it generates duplication.
Take for example the following two <span> elements, which have exactly the same style attributes:
In referential styling, styles are defined once and reused throughout the document — in a similar way to how CSS rules can be declared once and then applied to multiple HTML elements via for example, element or class selectors. In IMSC this is achieved by defining a <styling> element inside the document <head>, inside which is placed one or more <style> elements — each one of which defines a set of styles that you can reuse elsewhere. This is illustrated below:
Each <style> element is given an id ("s1" in this example):
which can then be referred to later in the document:
this is equivalent to:
In other words, referencing a <style> element via its id and the style attribute is equivalent to copying the style properties of the <style> element onto the referencing element, as if the style properties had been specified using inline styling.
Style inheritance
If a style property is inheritable, like tts:color, then the style property will apply to all the descendants of an element it is specified on — again, this is similar to CSS and HTML. In the following example, the color "yellow" is applied to the text of both <p> elements because they are descendants of the <body> element.
Specifying a style on an element overrides any style specified on an ancestor, for example in the following snippet, the color of the second <p>'s text would be set to "aqua":
Region styling
Region styling plays a special role in IMSC since a style property specified on a region is inherited by all elements that are selected to the region, starting with the <body> element, as if the <region> element was the parent of the <body> element. For example, in the following example, the text "Hello, I am Mork from Ork" will appear in yellow.
Combining styles
Referential styling can be applied to style elements themselves:
Multiple styles can be also applied simultaneously on an element. For example, in the snippet below the style properties of both styles s1 and s2 are applied to the same <p> element.