What Is CSS and How Does CSS Work?

Learn What Is CSS and How Does CSS Work? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

CSS (Cascading Style Sheets) is the language used to control the presentation and visual layout of web pages. While HTML provides the structure and meaning of a page, CSS controls how that structure looks and how elements are arranged.

CSS is responsible for things such as colors, typography, spacing, borders, backgrounds, responsive layouts, animations, and many other aspects of a website's visual presentation. Without CSS, an HTML document can still contain useful content and structure, but the page would generally appear much closer to the browser's default rendering.

HTML, CSS and JavaScript

HTML

Defines the structure and meaning of the content.

CSS

Controls presentation, visual styling, and layout.

JavaScript

Adds behavior, logic, and interactivity.

What Does CSS Stand For?

CSS stands for Cascading Style Sheets. Each part of the name describes an important idea.

Cascading

Multiple CSS declarations can compete, and the CSS cascade determines which declaration is ultimately applied.

Style

CSS describes visual presentation and layout rules for elements.

Sheets

CSS rules can be stored in stylesheets and reused across many HTML documents.

Why Is CSS Important?

Modern websites contain much more than plain text. They use navigation bars, cards, buttons, forms, dashboards, tables, responsive layouts, visual states, and complex application interfaces. CSS provides the presentation layer that turns structured HTML into usable interfaces.

CSS also allows developers to separate content from presentation. HTML can describe what an element is, while CSS describes how that element should appear. This separation makes it possible to reuse the same HTML structure with different visual designs.

What Can CSS Do?

Typography

Control font families, size, weight, line height, alignment, and other text presentation.

Colors

Set text colors, backgrounds, borders, gradients, and other visual colors.

Spacing

Control margin, padding, gaps, and other relationships between elements.

Layout

Build layouts with normal flow, Flexbox, Grid, positioning, and responsive techniques.

Responsive Design

Adapt layouts and styles to different viewport sizes and device conditions.

Visual Effects

Add borders, shadows, transforms, transitions, and animations.

How Does CSS Work with HTML?

A browser receives HTML and CSS, parses the documents, determines which CSS rules match each HTML element, calculates the resulting styles, and then renders the page.

HTML Document
+
CSS Rules
↓
Browser Calculates Styles
↓
Layout and Rendering
↓
Styled Web Page

What Does a CSS Rule Look Like?

A basic CSS rule contains a selector followed by one or more declarations.

p {
  color: blue;
  font-size: 18px;
}

In this example:

  • p is the selector.
  • color is a CSS property.
  • blue is the value.
  • font-size is another property.
  • 18px is its value.

What Is a CSS Selector?

A selector identifies the HTML elements to which a set of CSS declarations should apply.

/* Element selector */

p {
  color: #334155;
}

/* Class selector */

.card {
  padding: 20px;
}

/* ID selector */

#header {
  background: #0f172a;
}

/* Attribute selector */

input[type="email"] {
  border: 1px solid #cbd5e1;
}

CSS has many selector types, including element, class, ID, attribute, pseudo-class, pseudo-element, combinator, and functional selectors. Selector behavior becomes especially important when multiple rules target the same element.

What Are CSS Properties and Values?

A CSS property describes what aspect of an element you want to control. The value specifies how that property should behave.

.title {
  color: #f97316;
  font-size: 2rem;
  font-weight: 700;
}

Here, color, font-size, and font-weight are properties, while their corresponding values define the requested presentation.

Three Ways to Add CSS

CSS can be added to HTML in three common ways: inline styles, internal stylesheets, and external stylesheets.

Inline CSS

An inline style is written directly on an HTML element.

<p style="color: orange;">
  Hello CSS
</p>

Inline CSS can be useful for specific cases, but it is generally harder to maintain when many elements require the same styling.

Internal CSS

Internal CSS is written inside a <style> element in an HTML document.

<style>
  p {
    color: orange;
  }
</style>

External CSS

External CSS is stored in a separate stylesheet and connected to the HTML document. This is a common approach for maintainable websites and applications.

<link
  rel="stylesheet"
  href="styles.css"
>

Which CSS Method Should You Prefer?

Method Best Use Maintainability
Inline Very specific situations Low for large projects
Internal Small pages or page-specific styles Moderate
External Reusable application and website styles High

What Does "Cascading" Mean in CSS?

The word cascading describes the process used to resolve competing CSS declarations. More than one rule can apply to the same element, and CSS uses its cascade rules to determine which declaration wins for a particular property.

p {
  color: blue;
}

.article p {
  color: orange;
}

Both rules may match the same paragraph. CSS does not simply choose a rule because it appears later in the stylesheet. Selector specificity and the broader cascade determine which declaration wins.

What Is CSS Inheritance?

Some CSS properties can inherit values from a parent element. For example, text-related properties such as color commonly participate in inheritance.

body {
  color: #334155;
}

.card {
  /* Text inside can inherit
     the body's color unless
     another rule overrides it. */
}

Not every CSS property is inherited. Understanding inheritance helps explain why some styles automatically appear on child elements while others must be declared explicitly.

CSS and the Box Model

CSS treats rendered elements as boxes. The box model describes the relationship between content, padding, border, and margin.


+---------------------------------------+
|                Margin                 |
|   +-------------------------------+   |
|   |             Border            |   |
|   |   +-----------------------+   |   |
|   |   |        Padding        |   |   |
|   |   |   +---------------+   |   |   |
|   |   |   |    Content    |   |   |   |
|   |   |   +---------------+   |   |   |
|   |   +-----------------------+   |   |
|   +-------------------------------+   |
+---------------------------------------+
    

The box model is fundamental to understanding CSS layout and spacing. It affects widths, heights, borders, internal spacing, and external spacing.

CSS for Page Layout

CSS provides several layout mechanisms. Developers commonly use normal document flow, Flexbox, CSS Grid, and positioning depending on the layout problem.

Flexbox

Useful for arranging items primarily along one dimension.

CSS Grid

Useful for rows, columns, and two-dimensional page layouts.

Normal Flow

Lets content naturally participate in the document's layout.

Positioning

Provides specific positioning behavior such as absolute, fixed, and sticky elements.

How Does CSS Make Websites Responsive?

Responsive web design means creating interfaces that adapt to different available screen sizes and viewing conditions. CSS provides several tools for this, including flexible layouts, relative units, media queries, container queries, and responsive sizing functions.

.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 768px) {
  .cards {
    grid-template-columns:
      repeat(2, 1fr);
  }
}

The layout starts with a single column and gains additional columns when enough space is available.

CSS Can Separate Design from Content

One of the strongest advantages of CSS is reuse. The same HTML structure can receive different visual treatments without rewriting the content itself.

Light Theme

The same content can be presented using a light visual system.

Dark Theme

The same semantic structure can use a completely different presentation.

A Simple CSS Example

Here is a basic example that connects HTML with a CSS stylesheet.

<!DOCTYPE html>
<html>
<head>

  <link
    rel="stylesheet"
    href="styles.css"
  >

</head>

<body>

  <h1 class="title">
    Welcome to CSS
  </h1>

  <p class="description">
    CSS controls the visual presentation
    of this page.
  </p>

</body>
</html>

The HTML defines the content and class names, while the CSS stylesheet defines how those elements should look.

body {
  font-family: Arial, sans-serif;
  background: #f8fafc;
  color: #1e293b;
}

.title {
  color: #f97316;
  font-size: 2.5rem;
}

.description {
  max-width: 600px;
  line-height: 1.7;
}

CSS Comments

CSS supports comments that are ignored when the stylesheet is processed. They can be useful for explaining non-obvious decisions or grouping related styles.

/* Main page heading */

.title {
  color: #f97316;
}

/*
  Shared card layout
*/

.card {
  padding: 1.5rem;
}

Where Is CSS Used?

CSS is used across almost every category of web interface. It can style a simple static page or form the presentation layer of a large web application.

Websites

Blogs, landing pages, documentation sites, portfolios, and company websites.

Web Applications

Dashboards, SaaS products, admin panels, editors, and business applications.

Design Systems

Shared component libraries and reusable visual systems.

Responsive Interfaces

Interfaces that adapt between mobile, tablet, desktop, and larger displays.

Interactive Components

Buttons, menus, dialogs, forms, navigation, and state-based components.

Theming

Light and dark themes and other reusable visual variations.

Common CSS Beginner Mistakes

Confusing HTML with CSS

HTML creates the document structure; CSS is responsible for presentation and layout.

Forgetting the Selector

A CSS declaration needs to be associated with elements through a selector or another appropriate rule context.

Writing Invalid Syntax

Missing braces, colons, or semicolons can prevent declarations from being parsed as expected.

Using Too Many Inline Styles

Repeated inline styles make shared design changes harder to maintain.

Ignoring the Cascade

Multiple rules can match the same element. Understanding the cascade prevents random override-based fixes.

How to Start Learning CSS

The best way to learn CSS is to understand the concepts in layers. Start with CSS syntax and selectors, then learn properties and values, followed by the box model, spacing, display, positioning, Flexbox, Grid, responsive design, and more advanced cascade behavior.

CSS Syntax
↓
Selectors & Properties
↓
Box Model & Spacing
↓
Layout
↓
Responsive Design
↓
Advanced CSS

Do not try to memorize hundreds of properties at once. Focus on understanding how CSS thinks about elements, layout, dimensions, the cascade, and relationships between components.

CSS Best Practices for Beginners

Use External Stylesheets

Keep reusable styles separate from page content when appropriate.

Use Meaningful Class Names

Name classes according to their role instead of arbitrary visual details.

Keep Selectors Understandable

Avoid unnecessarily complicated selector chains.

Learn the Box Model

Understand how content, padding, borders, and margins affect layout.

Build Responsive Layouts

Test your layouts across different viewport sizes.

Use DevTools

Inspect elements and experiment with CSS directly in the browser.

Key Takeaways

CSS stands for Cascading Style Sheets and controls the presentation and layout of web documents.

HTML provides structure and meaning, while CSS controls visual presentation.

A CSS rule consists of selectors and declarations containing properties and values.

CSS can control typography, colors, spacing, layout, responsive behavior, and visual effects.

The cascade determines which competing CSS declarations are applied.

CSS inheritance allows certain properties to pass from parent elements to descendants.

The box model explains how content, padding, borders, and margins affect an element's dimensions.

Flexbox, Grid, normal flow, and positioning provide different tools for building layouts.

Responsive CSS allows interfaces to adapt to different viewport sizes and environments.

Learning CSS is easier when you understand the underlying layout concepts instead of memorizing isolated properties.

Code Examples

css
/* A basic CSS rule */

p {
  color: #334155;
  font-size: 18px;
  line-height: 1.6;
}
    
css
/* Common selectors */

/* Element */
p {
  color: #334155;
}

/* Class */
.card {
  padding: 20px;
}

/* ID */
#header {
  background: #0f172a;
}

/* Attribute */
input[type="email"] {
  border: 1px solid #cbd5e1;
}
    
html
<!DOCTYPE html>
<html>
<head>

  <style>

    body {
      margin: 0;
      font-family: Arial, sans-serif;
      background: #f8fafc;
      color: #1e293b;
    }

    .container {
      width: min(
        calc(100% - 2rem),
        900px
      );

      margin-inline: auto;
      padding-block: 3rem;
    }

    .title {
      color: #f97316;
      font-size: 2.5rem;
    }

    .description {
      max-width: 650px;
      line-height: 1.7;
    }

  </style>

</head>

<body>

  <main class="container">

    <h1 class="title">
      Welcome to CSS
    </h1>

    <p class="description">
      CSS controls the visual appearance
      and layout of HTML content.
    </p>

  </main>

</body>
</html>
    
css
/* Responsive CSS */

.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 768px) {
  .cards {
    grid-template-columns:
      repeat(2, 1fr);
  }
}

@media (min-width: 1024px) {
  .cards {
    grid-template-columns:
      repeat(3, 1fr);
  }
}
    
css
/* CSS variables */

:root {
  --primary-color: #f97316;
  --text-color: #1e293b;
  --surface-color: #ffffff;

  --space-md: 1rem;
  --radius-md: 10px;
}

.card {
  padding: var(--space-md);

  color: var(--text-color);
  background: var(--surface-color);

  border-radius: var(--radius-md);
}

.button {
  padding: 0.75rem 1rem;

  background: var(--primary-color);
  color: white;
}
    
html
<!DOCTYPE html>
<html>
<head>

  <link
    rel="stylesheet"
    href="styles.css"
  >

</head>

<body>

  <header class="site-header">
    <h1 class="site-title">
      My Website
    </h1>
  </header>

  <main class="content">

    <article class="card">

      <h2 class="card-title">
        Learning CSS
      </h2>

      <p class="card-text">
        CSS controls how HTML content
        looks and behaves visually.
      </p>

      <button class="button">
        Start Learning
      </button>

    </article>

  </main>

</body>
</html>
    

Pro Tip

Do not learn CSS as a collection of unrelated properties. Build a mental model first: HTML provides the structure, CSS selects elements and applies declarations, the cascade resolves competing rules, the box model controls dimensions and spacing, and layout systems determine how elements interact. Once these concepts are clear, individual CSS properties become much easier to learn.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.