Survey of CSS

Notes

CSS stands for Cascading Style Sheets.

CSS is used to control the appearance of the web page.

Here are some key notions, before we look at some examples:

rules
A rule consists of a “selector” and series of property-value specifications. It indicates that those elements that match the selector should have those properties set to those values.
selectors

Selectors “target” certain elements in the page. For instance a selector could say: “I want to look at the li elements that have a class of todo and are contained inside an element with and id of main.” This would be expressed as #main li.todo.

There are a great many types of things that can be targeted via selectors (both in CSS and in Javascript as we will see when we discuss jQuery). For instance:

properties and values
CSS properties specify the appearance of elements. There is a long list of properties, and each property accepts a specific set of values. Each property-value pair must end with a semicolon.
specificity

Each selector has a specificity value, indicating how “precisely” it is pinpointing the elements. For instance, targetting a specific id is more “specific” than targetting a specific class, which is more “specific” than targetting a specific HTML tag.

If two rules try to apply the same property to an element, the rule with higher specificity wins. If they have equal specificity, the rule that appears later in the file wins.

Browsers also provide their own rules. In general user-provided rules take precedence.

inheritance
Many of the properties are inherited from a parent node to its children. For example specifying the font family in a parent node enforces that setting to all its children as well. This behavior lends cascading style sheets their name.
box model

The box model specifies how different properties specifying dimensions of elements should behave. It consists of content width, padding, border and margins.

layout mode

At any given time, depending on the element and its settings, the browser is in one of four main layout modes: block, inline, table and positioned.

floats
Floats are a difficult concept to work with at first. A “floating” element is taken out of the normal flow and placed along the left or right side of the page, while other elements wrap around it.

Floats are especially important for creating the illusion of columns. For example we can create two columns by floating the first one to the left, giving it a fixed width, and having the second one with a left margin equaling that width.

flexbox and grid
Flexbox and Grid layouts are the modern ways to structure a web-page’s look and feel.

There are too many properties to list here, but we will list the various selectors we can use to target elements:

aTag
An HTML tag when used as a selector targets all elements in the page with that tag.
#anId
Targets only the element with that specific id.
.aClass
Targets all elements with this class.
sel1sel2
Putting selectors right next to each other indicates that they should all apply to an element for it to be targetted. For example div#main targets only a div element with an id of main, and nothing else.
sel1 sel2
A space between two selectors indicates that the second selector should target descendants of elements matched by the first selector. For instance .todo p targets paragraph elements only when they appear within an element with class “todo”.
sel1, sel2
A comma between two selectors indicates that this rule should apply if either selector applies.
sel1 > sel2
Targets elements that match the second selector, that are children (immediate descendants) of elements that match the first selector.
sel1 ~ sel2
Targets elements that match the second selector, that are siblings of elements that match the first selector.
sel1 + sel2
Targets elements that match the second selector, that are adjacent siblings (immediately follow) of elements that match the first selector.
[attr=value]
Attribute selectors target elements with a specific value for a specific attribute. There are a number of variants depending on the relation between the specified value and the element’s value.
::pseudo
There are a few pseudo-elements that can be targetted this way, for instance the first line in a paragraph or the first letter in a paragraph.
:pseudo
These are pseudo-classes and represent specific behavior of the targetted elements. For instance, you can use :hover to indicate something that should happen only when the element is hovered over by the mouse, you can use :visited to highlight links that have already been followed, and so on.

For an example of how a CSS stylesheet might look like, have a look at the CSS file used for our class notes.