Skip to content
GitHub

Introduction to CSS

Switch to Zen Mode
  • CSS (Cascading Style Sheets) is the language used to describe the style and layout of webpages.
  • It controls elements’ appearance such as colors, fonts, spacing, and positioning.
  • A CSS stylesheet is a set of rules that define the visual appearance of elements on a webpage.
  • A CSS rule is made up of a selector and a declaration block.
  • Check out the MDN CSS reference

A CSS rule consist of a selector and a declaration block:

selector {
property: value;
}
css

Selectors define which HTML elements the CSS rules apply to.


The declaration block contains one or more CSS declarations (property-value pairs) separated by semicolons (;). Each declaration consists of a property and its associated value.

Example:

selector {
property: value;
}
css
  • Property: Defines what you want to change (e.g., color, font-size, margin).
  • Value: Specifies the value for the property (e.g., blue, 16px, 10px).

A comparison table that highlights the differences between ID selector, type selector, and class selector in CSS:

FeatureID SelectorType SelectorClass Selector
Syntax#idNameelementName.className
Target Element(s)Targets a single element with a specific idTargets all elements of a specific type (e.g., div, p)Targets all elements with a specific class
UniquenessMust be unique per page (only one element can have the same id value)Not unique, applies to all elements of the specified typeNot unique, applies to all elements with the specified class
SpecificityHigh specificity (most specific)Low specificityMedium specificity
Use CaseWhen styling a single, unique elementWhen styling all instances of a specific type of elementWhen styling a group of elements with a shared class
Example#header { color: red; }p { color: blue; }.button { background: green; }
Can Be Applied toOne specific elementAll elements of a specific typeMultiple elements with the same class
ScopeCan only appear once in the documentCan appear multiple times in the documentCan appear multiple times in the document
Usage PriorityHighest priority when appliedLowest priority when appliedMedium priority when applied
  1. External CSS: Linked in the <head> section of an HTML document.
    <head>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    html
  2. Internal CSS: Within style tags in the <head> section of an HTML document.
    <head>
    <style>
    /* CSS rules here */
    </style>
    </head>
    html
  3. Inline CSS: Directly in the opening tag of an element.
    <div style="color: blue;">
    html

Targets an element by its tag name.

p {
color: blue;
}
css

This targets all <p> elements.

Targets elements by their class attribute.

.example {
font-size: 16px;
}
css

This targets all elements with the class “example” (e.g., <div class="example">).

Targets an element by its ID attribute.

#header {
background-color: #333;
}
css

This targets the element with id="header".

Targets all elements on the page.

* {
margin: 0;
padding: 0;
}
css

This applies to every single element.

Targets elements based on the presence or value of an attribute.

input[type="text"] {
border: 1px solid #ccc;
}
css

This targets <input> elements with type="text".

Targets elements that are nested within other elements.

div p {
color: red;
}
css

This targets all <p> elements inside <div> elements.

Targets direct children of an element.

div > p {
color: green;
}
css

This targets <p> elements that are direct children of a <div>.

Targets elements in specific states (e.g., hover, focus).

a:hover {
color: red;
}
css

This targets <a> elements when the user hovers over them.

Targets part of an element (e.g., first letter, first line).

p::first-letter {
font-size: 2em;
}
css

This targets the first letter of every <p> element.