What Is CSS and What Is It Used For?
CSS (Cascading Style Sheets) is the language that controls how a web page looks and how its content is laid out. HTML defines what the content is (headings, paragraphs, links, images), while CSS defines how that content appears — colors, fonts, spacing, sizing, and where each element sits on the screen. You use CSS any time you want a page to look intentional rather than like a plain document of stacked text.
The three roles: structure, presentation, behavior
A useful way to keep web technologies straight is to separate them by job:
| Technology | Role | Example responsibility |
|---|---|---|
| HTML | Structure / content | "This is a heading, this is a paragraph, this is a link" |
| CSS | Presentation / layout | "Headings are dark blue, paragraphs have 16px spacing, the sidebar sits on the right" |
| JavaScript | Behavior / interactivity | "Clicking this button opens a menu, this form validates before submitting" |
CSS is not a programming language in the sense of loops and logic — it is a declarative style language. You describe the result you want, and the browser figures out how to apply it.
How a CSS rule is written
A CSS rule has two parts: a selector that picks which elements to style, and a declaration block that lists the styles to apply.
/* selector: p | declarations inside { } */
p {
color: #333333;
font-size: 16px;
line-height: 1.5;
margin-bottom: 1em;
}
pis the selector — it targets every<p>element.color,font-size,line-height,margin-bottomare properties.#333333,16px,1.5,1emare the values.- Each property–value pair ends with a semicolon; the whole block is wrapped in
{ }.
The word cascading in the name refers to how the browser resolves conflicts: when multiple rules target the same element, it follows priority rules (origin, specificity, and source order) to decide which one wins. That is why a later rule or a more specific selector can override an earlier one.
What CSS is commonly used for
- Color and typography — text color, background color, font family, size, weight, and line spacing.
- Spacing and sizing — margins, padding, width, height, and gaps between elements.
- Layout — arranging elements in rows, columns, grids, or flexible containers (Flexbox and Grid are the modern layout systems).
- Responsive design — changing the layout based on screen size using media queries, so a page works on both phones and desktops.
- Visual states — hover, focus, and active styles for links and buttons.
- Borders, shadows, and rounded corners — the decorative details that shape a page's look.
Three ways to attach CSS to HTML
-
External stylesheet (most common) — a separate
.cssfile linked from the HTML<head>:<link rel="stylesheet" href="styles.css">This is preferred because one file can style an entire site, and the browser can cache it.
-
Internal stylesheet — a
<style>block inside the HTML<head>. Useful for a single page or quick testing. -
Inline styles — a
styleattribute directly on an element, such as<p style="color: red;">. It applies to that one element only and is hard to maintain at scale, so it is generally reserved for one-off cases.
How CSS fits with the rest of a page
When a browser loads a page, it reads the HTML to build the document structure, then applies the CSS to compute how each element should be displayed, and finally paints the result. JavaScript can run alongside this to change styles dynamically — for example, adding a class that CSS has already defined. This separation is why the same HTML can be restyled completely by swapping stylesheets, and why CSS is one of the three core technologies of the web alongside HTML and JavaScript.