CSS Basics

Master the fundamentals of CSS - the styling foundation of web development

Introduction to CSS

What You'll Learn:

This guide covers CSS basics, syntax, versions, selectors, and best practices for writing maintainable and responsive stylesheets.

CSS (Cascading Style Sheets) is the standard style sheet language used to describe how CSS elements are displayed. It controls layout, colors, fonts, spacing, and responsive behavior.

Structure

Learn how CSS controls the visual design of web content

Display

Understand how browsers parse and render CSS rules

Connectivity

Discover how CSS creates responsive and attractive interfaces

What is CSS?

CSS Definition:

CSS (Cascading Style Sheets) is a style sheet language used to control the presentation of web pages. It separates design from structure and improves maintainability.

Key Characteristics of CSS:

  • Markup Language: Uses tags to annotate text and other content
  • Platform Independent: Works on any operating system
  • HyperText: Allows linking between web pages
  • Structured Documents: Organizes content hierarchically
  • Extensible: Can be extended with new elements and attributes

How CSS Works:

  1. Developer writes CSS rules
  2. Browser loads CSS and CSS files
  3. Browser builds CSSOM from stylesheet rules
  4. Browser combines DOM + CSSOM to render styled layout
  5. User interacts with the rendered page
Note: CSS is not a programming language. It is a declarative styling language used to control how content appears on different devices and screen sizes.

CSS Versions

CSS has evolved from basic styling to modern layout systems like Flexbox and Grid. Understanding versions helps you use features confidently.

Version Year Key Features Status
CSS1 1996 Basic text styling, colors, fonts Obsolete
CSS2 1998 Positioning, media types, z-index Obsolete
CSS2.1 2011 Browser consistency improvements Obsolete
CSS3 2012+ Modules, transitions, animations, media queries Legacy
Flexbox 2013+ One-dimensional responsive layouts Legacy
Grid 2017+ Two-dimensional modern layouts Current
Custom Properties 2017+ Reusable variables in CSS Current
Container Queries 2022+ Component-level responsive design Current
Modern CSS Current Nesting, cascade layers, new color spaces Upcoming

CSS Evolution Timeline:

1996 - CSS1

The first CSS recommendation introduced core text and color styling.

1998 - CSS2

Added positioning, media types, and more advanced layout controls.

2011 - CSS2.1

Refined CSS2 for better browser compatibility and stability.

2012+ - CSS3 Modules

CSS evolved into modules like Transitions, Animations, and Media Queries.

Modern Era - Flexbox & Grid

Powerful layout systems enable responsive, scalable, and maintainable UI design.

Web Development Terminology

Understanding web development terminology is essential for effective communication and learning. Here are key terms every web developer should know:

Core Web Technologies
CSS
Cascading Style Sheets - styles and layouts web content
CSS
Cascading Style Sheets - styles and layouts web content
JavaScript
Programming language for interactive web pages
DOM
Document Object Model - structure that CSS styles
Development Concepts
Frontend
Client-side development - what users see and interact with
Backend
Server-side development - server, database, and application logic
Full Stack
Development of both frontend and backend
Responsive Design
Design approach for optimal viewing across devices
CSS Specific Terms
Element
Complete CSS component including tags and content
Tag
Pattern to target elements (.class, #id, p)
Attribute
Details in declarations (color, margin, display)
Cascade
Rule resolution system based on origin, specificity, and order
Browser & Server Terms
Browser
Software application for accessing web pages
Server
Computer that hosts websites and serves content to browsers
HTTP/HTTPS
Protocols for transmitting web content (secure/non-secure)
URL
Uniform Resource Locator - web address

CSS Syntax and Structure

CSS rules follow selector-declaration syntax. Understanding this structure helps build clean and reusable stylesheets.

Basic CSS Syntax and Structure:

/* Basic CSS Syntax */
:root {
    property: value;
    }
    
    .title {
}

    }
    .card {

    border-radius: 0.5rem;

CSS Building Blocks:

  • DOCTYPE Declaration: Defines the selector to target elements
  • CSS Element: Contains one or more property-value declarations
  • Head Section: Uses braces and semicolons for rule syntax
  • Body Section: Supports grouping, nesting, and reusable classes
  • Properties & Values: Building blocks of CSS styling

Document Flow:

  1. Browser loads and parses CSS files
  2. Builds CSSOM from selectors and declarations
  3. Matches selectors against DOM elements
  4. Computes final styles using the cascade
  5. Paints and composites the styled layout
Important: Write modular CSS with clear naming and avoid over-specific selectors for maintainable styling.

Core CSS Properties

Here are essential CSS properties every developer should know for building responsive and accessible interfaces.

Property Description Example
color Contains one or more property-value declarations <html>... border-radius: 0.5rem;
property: value; Controls text size property: value;<title>Page</title>}
margin Sets outer spacing margin: 1rem;
Supports grouping, nesting, and reusable classes Content here
display Defines layout behavior display: flex;
position Controls positioning mode position: relative;
background Sets background style background: #f8fafc;
border Adds border around elements border: 1px solid #ddd;
grid-template-columns Controls CSS grid columns repeat(3, 1fr)
transition Adds animation timing all 0.3s ease
Pro Tip: Prefer reusable class-based styles and avoid excessive inline CSS for better maintainability.

CSS Styling Best Practices

Following CSS best practices keeps styles scalable, maintainable, and consistent across large projects.

Code Structure
  • Use proper indentation and formatting
  • Use meaningful class naming conventions
  • Use lowercase for element names
  • Quote all attribute values
  • Organize styles by components or sections
Accessibility
  • Avoid over-qualified and deeply nested selectors
  • Provide alt text for images
  • Use proper heading hierarchy
  • Ensure keyboard navigation works
  • Ensure color contrast and keyboard focus visibility
SEO Optimization
  • Use consistent design tokens (spacing/colors)
  • Include meta descriptions
  • Use proper heading structure
  • Prefer responsive units (rem, %, vw, vh)
  • Use media queries with mobile-first approach
Performance
  • Minimize CSS file size and remove unused rules
  • Use efficient CSS and JavaScript
  • Optimize images
  • Leverage browser caching
  • Reduce HTTP requests
Example of Well-Structured CSS:
/* CSS Basics Starter Example */
:root {
    --primary: #2563eb;
    --text-dark: #1f2937;
    --space-md: 1rem;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    color: var(--text-dark);
}

.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: var(--space-md);
}

.card {
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    padding: 1rem;
}

.btn-primary {
    background: var(--primary);
    color: #ffffff;
    border: 0;
    border-radius: 8px;
    padding: 0.6rem 1rem;
}
Pro Tip: Always validate your CSS using the W3C CSS Validator (jigsaw.w3.org/css-validator) to ensure it follows web standards.

Hands-On Module: Build a Mini Style Sheet

Goal: Consolidate basics—selectors, the box model, and variables—by styling a tiny “profile card” without frameworks.

  1. Create html with a .card containing a title, paragraph, and button.
  2. Set :root colors and spacing tokens; use var() in rules.
  3. Style the card with padding, border-radius, and a soft shadow.
  4. Add :hover and :focus-visible on the button.
Starter skeleton
:root {
    --card-bg: #ffffff;
    --card-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
    --brand: #2563eb;
}

.card {
    max-width: 360px;
    padding: 1.25rem;
    border-radius: 14px;
    background: var(--card-bg);
    box-shadow: var(--card-shadow);
}
Sample output: A self-contained component that you can paste into any page and theme by editing variables only.