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);
}
CSS Basics MCQ Test
Practice in the same pattern as the dedicated CSS MCQ pages: choose options, check answers, and view your score instantly.
CSS Basics Interview Questions
CSS Basics interview preparation should combine conceptual clarity with practical examples. Use concise definitions plus one real project scenario in each answer.
When answering, explain both what a concept does and when to apply it. Interviewers value practical reasoning over memorized definitions.
Basic Interview Q&A
- What is CSS?CSS is a styling language used to control the visual presentation of HTML documents.
- What is the cascade?The cascade resolves competing style rules using origin, importance, specificity, and source order.
- What is specificity?Specificity is the selector weight used to decide which declaration wins.
- Class vs ID selector?Classes are reusable; IDs are unique and should target one element.
- When to use external CSS?Use external stylesheets for maintainability and reuse across multiple pages.
- What are inline, internal, and external CSS?Inline is element-level, internal is page-level, and external is reusable project-level styling.
- What is inheritance in CSS?Some properties like font and color inherit from parent elements automatically.
- What is the box model?Every element has content, padding, border, and margin areas.
- What is the difference between
display: noneandvisibility: hidden?display: noneremoves layout space, whilevisibility: hiddenkeeps space but hides content. - Why use semantic class names?Semantic names improve readability, reuse, and long-term maintainability.
Advanced Interview Q&A
- How do cascade layers help?
@layerlets you control style priority intentionally across base, components, and utilities. - What are container queries?They apply styles based on the size of a parent container, not just viewport size.
- When do you use
will-change?Use it only for targeted animation optimization; overuse can increase memory cost. - What causes layout shift in CSS?Late-loading fonts/images and dynamic content without reserved space commonly cause CLS issues.
- How do you reduce CSS bundle size?Remove unused rules, split by route/component, and minify in production builds.
- What is critical CSS?It is the minimal above-the-fold CSS inlined to improve first render performance.
- How do you manage large CSS architecture?Use naming conventions and layered structure (for example, BEM plus utility strategy).
- What are paint/composite-friendly animations?Prefer transform and opacity over top/left and width/height for smoother rendering.
- How does specificity debt happen?Overly nested or overqualified selectors force escalation and make overrides difficult.
- How do you design accessible focus states?Use visible
:focus-visiblestyles with sufficient contrast and non-color cues.