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:
- Developer writes CSS rules
- Browser loads CSS and CSS files
- Browser builds CSSOM from stylesheet rules
- Browser combines DOM + CSSOM to render styled layout
- User interacts with the rendered page
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:
- Browser loads and parses CSS files
- Builds CSSOM from selectors and declarations
- Matches selectors against DOM elements
- Computes final styles using the cascade
- Paints and composites the styled layout
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;
}
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.
- Create
htmlwith a.cardcontaining a title, paragraph, and button. - Set
:rootcolors and spacing tokens; usevar()in rules. - Style the card with padding, border-radius, and a soft shadow.
- Add
:hoverand:focus-visibleon 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);
}