Introducing CSS
This is an introduction to the basics of CSS
Dr Graeme StuartThis is an introduction to the basics of CSS
Dr Graeme StuartCSS allows developers to control the style and layout of a document in the browser.
Spend some time in the CSS Zen Garden for inspiration.
Ideally, we don’t want to include style information within the HTML document itself. So we put all our style information in separate CSS files and use
<link>
elements to link these files into our documents.
|
|
This is very flexible. A common stylesheet can be used across multiple documents. We can also have multiple stylesheets attached to a single document if we want.
A CSS ruleset consists of a [group of] selector[s] and a declaration block (between curly brackets) containing one or more declarations. Where Each declaration consists of a property name, a colon and a value, followed by a semi-colon.
|
|
Typically, each declaration is given it’s own line. One-line formatting is also common but should only be used when there is only one declaration.
|
|
Spaces and newline characters are ignored. However, consistent formatting is expected.
CSS declarations are a way to set CSS properties of elements to given values. The selector determines which elements are targetted by the ruleset.
|
|
In the above examples the selectors are body, p and a respectively.
CSS declarations can have multiple values, typically separated by spaces or commas.
|
|
Some properties are shorthand properties provided for convenience.
|
|
The
border
property is shorthand for settingborder-width
,border-style
andborder-color
simultaneously.
In HTML, class attributes (
class="student"
) can be used to identify similar elements. Class selectors (the dot, e.g..student
) can be used for selecting elements with a specific class attribute. Similarly, id attributes (id="students"
) can be selected with a hash (e.g.#students
).
|
|
|
|
Selectors allow us to specify which elements will be affected by a ruleset. There are several patterns (known as combinators) we can use for selectors which allow us to target specific elements.
Separating elements with spaces (e.g.
p a
) is the descendent combinator.
|
|
Using a greater than symbol (e.g.
p > a
) is the child combinator
|
|
Using a plus symbol (e.g.
h1 + p
) is the next sibling combinator
|
|
Read more about selectors and combinators.
The order of styles is important, but styles are not simply applied in order. Styles with higher specificity take precedence over those with lower specificity.
|
|
The paragraph has a class and an id. The style declarations below all try to set the
color
property. So, there are three potential values, but only one can be applied. Which one is chosen?
|
|
The specificity algorithm counts selector components in each weight category (ID, CLASS, TYPE). If competing declarations have the same value, the order in the file will decide.
Note that styles added as inline style attributes will always be applied.
Essentially there are three types of stylesheets.
Conflicting styles are applied by an algorithm known as the cascade (as in Cascading Style Sheets). The algorithm is fairly complex but for our purposes we can simplify it.
Documentation is usually excellent via MDN. Be sure to seek out the MDN page when searching the web (add “MDN” to your search).
e.g. see the documentation for the CSS border property
There are two main exercises which will introduce some CSS concepts in a practical way.
If you have any questions, now is a good time to ask.
Thanks for listening
Dr Graeme Stuart