← Back to Chapters

Introduction to CSS

? Introduction to CSS

⚡ Quick Overview

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents. It controls the layout, colors, fonts, spacing, and overall visual design of web pages, allowing you to create visually attractive and consistent user interfaces.

With CSS, you separate content (HTML) from presentation (design), making your code cleaner, easier to maintain, and more reusable across multiple pages.

? Key Concepts

CSS relies on a few fundamental building blocks to target and style elements:

  • Selector: Specifies which HTML element(s) the style should apply to (e.g. p, .btn, #header).
  • Property: The visual aspect you want to change (e.g. color, font-size, margin).
  • Value: The setting for the property (e.g. red, 16px, center).
  • Declaration: A single property–value pair (e.g. color: blue;).
  • Rule: A full block of styling for a selector, wrapped in curly braces.
  • Cascading: The way CSS decides which styles “win” when multiple rules target the same element (order, specificity, inheritance).

? CSS Syntax

A standard CSS rule follows a strict structure: a selector followed by a declaration block.

? View CSS Rule Syntax
/* The Selector targets the element */
selector {
  property: value; /* Property: Value pair */
}

Here:

  • selector targets the HTML element(s).
  • property describes what visual feature you want to style.
  • value sets how that feature should look.
  • Each declaration ends with a semicolon ;, and all declarations are wrapped in {}.

? Code Example

Below is a simple CSS snippet that styles the page background, headings, and paragraphs:

? View CSS Example
/* 1. Target the entire body */
body {
  background-color: #f0f0f0; /* Light grey background */
  font-family: Arial, sans-serif;
}
/* 2. Target main headings */
h1 {
  color: #007bff; /* Brand Blue */
  text-align: center;
}
/* 3. Target paragraphs */
p {
  color: #333; /* Dark grey text */
  font-size: 16px;
  line-height: 1.5;
}

? Live Output & Explanation

?️ Styled Output Preview

This is a sample styled box with CSS!

In this example:

  • body gets a light gray background and a clean sans-serif font.
  • h1 text becomes blue and centered, making the page title stand out.
  • p text is set to a comfortable size and line height for better readability.

? Common CSS Properties

Some frequently used properties you'll use in almost every project:

Property Description Example Value
color Text color #333, red
background-color Background color of element #fff, blue
font-family Font typeface Arial, sans-serif
font-size Size of text 16px, 1rem
text-align Horizontal alignment of text left, center
margin Space outside element 10px, 1em, auto
padding Space inside element 5px, 1rem, 0

? Tips & Best Practices

  • Use external CSS files to keep HTML clean and separate content from design.
  • Practice writing selectors for elements, classes, and IDs.
  • Use browser developer tools (Inspector) to inspect and debug CSS styles in real time.
  • Keep your CSS organized with meaningful comments and grouping related styles.
  • Prefer reusable class selectors over styling everything by IDs.

? Try It Yourself

  1. Create a CSS rule to change the background color of the whole page.
  2. Style all <h1> elements to have a blue color and center alignment.
  3. Experiment with changing font sizes and families for paragraphs.
  4. Add different padding and margin values to elements and observe how layout changes.