Selectors decide which elements a rule styles. The box model explains how every element occupies space on the page.

Basic Selectors

The element (type) selector styles all elements of a type: p { }. The class selector, written with a dot, styles all elements with that class: .note { }, used as <p class="note">; a class may be used on many elements, and an element can have several classes. The id selector, written with #, styles the single element with that id: #header { }. The universal selector styles every element. A group selector* styles several selectors with one rule: h1, h2, h3 { }.

Definition: Selector. The part of a CSS rule that chooses which HTML elements the rule applies to, such as an element name, a class, an id or a combination of these.

Combinators and Attribute Selectors

A descendant selector, nav a { }, styles a elements anywhere inside nav. A child selector, ul > li { }, styles only direct children. Adjacent sibling (h1 + p) and general sibling (h1 ~ p) selectors style elements that follow another. Attribute selectors style elements with an attribute or value: input[type="text"] { }.

Pseudo-Classes and Pseudo-Elements

Pseudo-classes select elements in a particular state: a:hover (mouse over), a:visited, input:focus, li:first-child, li:last-child and tr:nth-child(even). Pseudo-elements style part of an element: p::first-letter, p::first-line, and ::before and ::after, which insert content.

The Box Model

Every element is a rectangular box made of four layers, from inside out: the content, the padding (space between content and border), the border, and the margin (space outside the border, separating it from other elements). The total width is width + left and right padding + left and right border + left and right margin. Setting box-sizing: border-box makes width include padding and border, which simplifies layout. Common properties are width, height, padding, border (such as 1px solid black), margin, and margin: auto to centre a block.

Common Properties

Text: color, font-family, font-size, font-weight, text-align, line-height, text-decoration. Background: background-color, background-image. Display and position: display (block, inline, inline-block, flex, grid, none) and position (static, relative, absolute, fixed, sticky).

Table 2.2: CSS selectors

Selector Syntax Selects
Element p All p elements
Class .note Elements with class="note"
id #main The element with id="main"
Descendant nav a a inside nav
Pseudo-class a:hover Links under the mouse

Example 2.2: Box size calculation

Problem: A div has width: 300px; padding: 20px; border: 5px solid; margin: 10px. Find its total width on the page with the default box-sizing, and with box-sizing: border-box.

Solution:

  1. Default (content-box): 300 + 2 × 20 + 2 × 5 + 2 × 10 = 300 + 40 + 10 + 20 = 370px.
  2. border-box: the 300px includes padding and border, so with margins it takes 300 + 20 = 320px.

Result: 370px with content-box, and 320px with border-box.

const box = { width: 300, padding: 20, border: 5, margin: 10 };
const contentBox = box.width + 2 * (box.padding + box.border +
        box.margin);
const borderBox = box.width + 2 * box.margin;
console.log("content-box total width:", contentBox + "px");
console.log("border-box total width:", borderBox + "px");

Output

content-box total width: 370px
border-box total width: 320px

Common mistakes

  • Using the same id on several elements. An id must be unique.
  • Forgetting the dot before a class name or the # before an id in CSS.
  • Forgetting that padding and border add to the width in the default box model.

Key takeaways

  • Element, class (.) and id (#) selectors are the basics.
  • Descendant, child and attribute selectors target elements precisely.
  • :hover and :nth-child are pseudo-classes; ::before is a pseudo-element.
  • The box model is content, padding, border and margin.

Practice questions

  1. How is a class selector written in CSS?
  2. Name the four parts of the box model.
  3. What does a:hover select?

Answers to the practice questions

  1. With a dot before the class name, as in .note.
  2. Content, padding, border and margin.
  3. A link while the mouse pointer is over it.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit II