3 CSS

3.1 Basic CSS syntax

HTML describes the structure and content of the DOM. We will use CSS (cascading style sheets) to control the formatting (color, size, font, etc.) of the rendered page.

The basic syntax of CSS is very simple:

The selector tell us which elements on the page will be affected and the property-value pairs determine the effect.

Here’s a simple example. Suppose we want all of our second level headers (h2 elements) to be red. We can do that with

3.2 Connecting HTML and CSS

There are three ways to get our CSS and HTML connected.

3.2.1 <style> ... </style> tags

The simplest method is to include a <style> ... </style> tag in your HTML file.

  1. Add the following at the header portion of your snow person HTML page.

    Expermint with some different colors. The most common ways to specify colors are by name or by 3- or 6-digit HEX codes. A color-picker tools is very useful for generating this codes. Google “css color-picker” to find Google’s color picker. In addition to the HEX code, you will see some other ways of describing color as well (red-green-blue, cyan-magenta-yellow-black, etc.)

3.2.3 Inline styles

Inline styles are the least useful, but for completeness you can also add style information directly in an HTML tag. Here is an example.

3.3 Selectors

3.3.1 Type selectors

The simplest selector is a type selector. These are the selectors we have seen so far – they specify a particular element type (p, h2, li, etc.)

  1. Use type selectors to make the text of all your ordered lists one color and all your unordered lists another color.

3.3.2 Union selectors

Sometimes we want to apply the same formatting to multiple selections. We can create a union selector by separating a list of selectors with commas.

  1. Use the h1, h2, h3, h4, h5, h6 selector to make all the headers red.

3.3.3 Class and ID selectors

What if we want some, but not all, elements of a certain type to be formatted in a particular way? This is where the class and id attributes come in handy. We can select all elements of a particular class, or the unique element with a specific ID using class and ID selectors.

So dot (.) for classes and # for IDs.

    1. Edit your HTML so that your paragraphs have classes and IDs. Make sure you have at least one pragraph with class “special” and at least one that does not have class “special”.

    2. Edit your HTML so that one list item has ID “favorite”

    3. Use class and ID selectors in your CSS file to format the special paragaphs differently from the others and to format your favorite list item differently from the others.

Keep in mind that we will be learning how to programmatically alter the DOM so that the class and/or ID of elements may change over time. Together with CSS, this will let us change how these elements are formatted.

Note: Elements of the DOM may have multiple classes! To select only items that have more than one class, simply concatenate.

3.3.4 Descendant and sibling selectors

We can create more complex selections by remembering that elements in the DOM are related to each other. Suppose, for example, that we want to apply some formatting to the li elements that are in ordered lists (ol) but not to li elements that are in unordered lists ul. That is, we want to select li elements that are descendants of ol elements. We do this by separating the two elements with a space. Children, adjacent (ie, next) siblings and general (subsequent) siblings are indicated with >, +, and ~.

  1. Modify your HTML to include an unordered list inside an item of an ordered list and an unordered list inside an item of an ordered list. Then Modify your CSS file to do the following:

    1. Make all items in ordered lists italic (but not in unordered lists).
    2. Make all but the first item in each ordered list red.
    3. Make all paragraphs that are inside a div have a yellow background (use the background property).

3.4 Properties and values

We have used a number of properties (font-size, color, etc.) and given them appropriate values (sizes, colors, etc.). But how do we know what the properties are called and what values they may be assigned? Answer: Look it up as needed! You won’t be able to remember them all. The ones you use frequently, you won’t be able to forget. For the rest, a Google search or a page like https://developer.mozilla.org/en-US/docs/Web/CSS/Reference can be very useful.

  1. Using the link above to find a property that we have not yet used and use it to add some more formatting to your page.

3.5 Cascading

It may have occurred to you that an element in your DOM might match multiple selectors and that your CSS styles for these selectors might disagree. What happens then?

Here’s what the CSS specification says:

The cascade takes a unordered list of declared values for a given property on a given element, sorts them by their declaration’s precedence, and outputs a single cascaded value.

That’s another way of saying there are (sometime complex) precedence rules to determine which one “wins”.
The short version is that precedence is determined by (in this order):

Here are the attributes that the CSS Cascade algorithm checks, listed in order from highest weight to least weight.

  1. Origin & Importance
  2. Selector Specificity
  3. Order of Appearance
  4. Initial & Inherited Properties (default values)

Roughly this says

  • More specific selectors take precedence over less specific ones.
  • Among equally specific selectors, later ones override earlier ones.
  • Children inherit styles from their parents (in most cases).

You can read more about this at this blog post. This post includes the following handy advice about CSS:

In my experience, if you default to only using class selectors for your custom styles and element selectors for your default styles, it’s way easier to override styles when you actually need to.

When we get to using D3, we will largely follow this mantra and make heavy use of class to select and format elements of our web pages.

3.5.1 Pseudo-classes: selector:keyword

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). This state my rely on information not contained in the DOM. For example, :hover can be used to change an element’s color when the user’s pointer hovers over it.

Similarly, :visited can be used to format links that have been visited differently from those that have not. For a list of available pseudo-classes, see https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes

  1. Use some pseudo-classes to style your page. Start by experimenting with :hover.

3.5.2 Pseudo-elements: selector::keyword

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph.

A list of pseudo-elements can be found at https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements.

  1. Use pseudo-elements to make the first line of each list element larger. Resize your browser to see what happens as text moves from the first line to the second.

  2. Use psuedo-elements to the make the first letter of each paragraph larger and a different color.

3.6 Transformations and Animation

Want to learn how to use CSS to change the size, rotation, and location of elements on your page and make them move? Check out these resources and give it a try:

3.7 Google Fonts

Google provides a large number of free fonts and makes it very easy to use them in our web pages.

  1. Go to fonts.google.com

  2. Select a font or fonts that interest you. (Click on the +)

  3. When you have the fonts you want, click where it says how many families you have selected. You will see the code to paste into the head of your HTML file to make them available and also the font-family names to use in your CSS to use them to style elements of your page.

That’s it. Enjoy!

3.8 Resources