---
title: Introduction to Cascading Style Sheets
order: 2
layout: page
---
[[themes.css]]
= Introduction to Cascading Style Sheets
((("CSS", "introduction", id="term.themes.css", range="startofrange")))
Cascading Style Sheets or CSS is the basic technique to separate the appearance
of a web page from the content represented in HTML. In this section, we give an
introduction to CSS and look how they are relevant to software development with
Vaadin.
As we can only give a short intruction in this book, we encourage you to refer
to the rich literature on CSS and the many resources available in the web. You
can find the authoratitative specifications of CSS standards from the
link:http://www.w3.org/Style/CSS/[W3C
website]
ifdef::web[]
and other literature, references, and tutorials from the
link:http://www.dmoz.org/Computers/Data_Formats/Style_Sheets/CSS/[Open Directory
Project page on CSS], as well as from other
sources
endif::web[]
.
[[themes.css.basics]]
== Applying CSS to HTML
Let us consider the following HTML document that contains various markup
elements for formatting text. Vaadin UIs work in essentially similar documents,
even though they use somewhat different elements to draw the user interface.
[subs="normal"]
----
<html>
<head>
<title>My Page</title>
<link rel="stylesheet" type="text/css"
href="mystylesheet.css"/>
</head>
<body>
**<p>**This is a paragraph**</p>**
**<p>**This is another paragraph**</p>**
<table>
<tr>
**<td>**This is a table cell**</td>**
**<td>**This is another table cell**</td>**
</tr>
</table>
</body>
</html>
----
The HTML elements that will be styled later by matching CSS rules are emphasized
above.
The [literal]#++++# element in the HTML header defines the used CSS
stylesheet. The definition is automatically generated by Vaadin in the HTML page
that loads the UI of the application. A stylesheet can also be embedded in the
HTML document itself, as is done when optimizing their loading in Vaadin
TouchKit, for example.
[[themes.css.basics]]
== Basic CSS Rules
A stylesheet contains a set of __rules__ that can match the HTML elements in the
page. Each rule consists of one or more __selectors__, separated with commas,
and a __declaration block__ enclosed in curly braces. A declaration block
contains a list of __property__ statements. Each property has a label and a
value, separated with a colon. A property statement ends with a semicolon.
Let us look at an example that matches certain elements in the simple HTML
document given in the previous section:
[source, css]
----
p, td {
color: blue;
}
td {
background: yellow;
font-weight: bold;
}
----
The [literal]#++p++# and [literal]#++td++# are element type selectors that match
with [literal]#++
++# and [literal]#++
++# elements in HTML, respectively.
The first rule matches with both elements, while the second matches only with
[literal]#++
++# elements. Let us assume that you have saved the above style
sheet with the name [filename]#mystylesheet.css# and consider the following HTML
file located in the same folder.
[[figure.themes.basic.1]]
.Simple Styling by Element Type
image::img/themes-css-match-1.png[]
[[themes.css.basics.inheritance]]
=== Style Inheritance in CSS
CSS has __inheritance__ where contained elements inherit the properties of their
parent elements. For example, let us change the above example and define it
instead as follows:
[source, css]
----
table {
color: blue;
background: yellow;
}
----
All elements contained in the [literal]#++
++# element would have the same
properties. For example, the text in the contained [literal]#++
++# elements
would be in blue color.
[[themes.css.basics.element-types]]
=== HTML Element Types
HTML has a number of element types, each of which accepts a specific set of
properties. The [literal]#++
++# elements are generic elements that can be
used to create almost any layout and formatting that can be created with a
specific HTML element type. Vaadin uses [literal]#++
++# elements
extensively to draw the UI, especially in layout components.
((("Google Web Toolkit",
"themeing")))
Matching elements by their type as shown above is, however, rarely if ever used
in style sheets for Vaadin applications. We used it above, because it is the
normal way in regular HTML documents that use the various HTML elements for
formatting text, but it is not applicable in Vaadin UIs that consist mostly of
[literal]#++
++# elements. Instead, you need to match by element class, as
described next.
[[themes.css.matching-by-class]]
== Matching by Element Class
Matching HTML elements by the __class__ attribute is the most common form of
matching in Vaadin stylesheets. It is also possible to match with the
__identifier__ of a unique HTML element.
The class of an HTML element is defined with the [parameter]#class# attribute as
follows:
[subs="normal"]
----
<html>
<body>
**<p class="normal">**This is the first paragraph**</p>**
**<p class="another">**This is the second paragraph**</p>**
<table>
<tr>
**<td class="normal">**This is a table cell**</td>**
**<td class="another">**This is another table cell**</td>**
</tr>
</table>
</body>
</html>
----
The class attributes of HTML elements can be matched in CSS rules with a
selector notation where the class name is written after a period following the
element name. This gives us full control of matching elements by their type and
class.
[source, css]
----
p.normal {color: red;}
p.another {color: blue;}
td.normal {background: pink;}
td.another {background: yellow;}
----
The page would look as shown below:
.Matching HTML Element Type and Class
image::img/themes-css-match-class-2.png[]
We can also match solely by the class by using the universal selector
[literal]#++*++# for the element name, for example [literal]#++*.normal++#. The
universal selector can also be left out altogether so that we use just the class
name following the period, for example [literal]#++.normal++#.
[source, css]
----
.normal {
color: red;
}
.another {
blackground: yellow;
}
----
In this case, the rule will match with all elements of the same class regardless
of the element type. The result is shown in <>. This
example illustrates a technique to make style sheets compatible regardless of
the exact HTML element used in drawing a component.
[[figure.themes.match.class]]
.Matching Only HTML Element Class
image::img/themes-css-match-class-3.png[]
To ensure future compatibility, we recommend that you use only matching based on
the classes and __do not__ match for specific HTML element types in CSS rules,
because Vaadin may change the exact HTML implementation how components are drawn
in the future. For example, Vaadin earlier used [literal]#++
++# element to
draw [classname]#Button# components, but later it was changed to use the
special-purpose [literal]#++