CSS Foundations: Mastering Tag Selectors

Learn the most fundamental CSS selector. Target elements by their HTML tag to apply broad, foundational styles to your entire website.

Lesson ProgressStep 1 of 8

Main Page Title

A Secondary Title

This is a paragraph. CSS tag selectors will target this element.

A Tertiary Title

This is another paragraph. It will receive the exact same styles as the first one.

0 EXP

Welcome! Let's style this document. Right now, it has no CSS.

/* This is a CSS stylesheet. It's empty! */

What is a CSS Selector?

Think of CSS selectors as the "pattern" that CSS uses to find (or "select") the HTML elements you want to style. The selector is the bridge between your HTML structure and your CSS rules.

There are many types of selectors (class, ID, attribute), but the most fundamental one is the tag selector. It selects elements based on their HTML tag name, like h1, p, or div.

System Check

What is the primary purpose of a CSS selector?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Syntax Guru

Write a valid CSS rule targeting multiple elements.

🏗️
Rule Constructor

Correctly assemble the components of a CSS rule.

✍️
Syntax Specialist

Prove your mastery of CSS syntax by filling in the blanks.

Mission: Style the Document

Write CSS rules to make the main heading (`h1`) **red** and all paragraphs (`p`) **italic**. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> Awaiting input...

Challenge: Assemble the CSS Rule

A CSS rule has a specific order. Drag the components into the correct syntax from top to bottom.

{
h1
}
color: blue;

Challenge: Complete the Syntax

Fill in the missing parts of the CSS rule to style all paragraphs.


: 1.5;}

Consult A.D.A.

Unlock with Premium

Community Holo-Net

Peer Project Review

Submit your "Styled Document" project for feedback from other Net-Runners.

The Foundation of Style: A Deep Dive into CSS Tag Selectors

In the architecture of a website, HTML provides the structure (the beams and bricks), and CSS provides the style (the paint, furniture, and finishes). The bridge between these two languages is the CSS selector. The most fundamental and widely used of these is the tag selector, also known as a type selector.

Mastering tag selectors is the first and most crucial step in writing clean, maintainable, and efficient CSS. They are the "broad strokes" that define the default look and feel of your entire website before you add finer details.

The Anatomy of a Tag Selector Rule

A CSS rule consists of two main parts: the selector and the declaration block.

h1 {
color: #333;
font-size: 2.5rem;
}
  • Selector (h1): This is the tag selector. It targets every single <h1> element on the page.
  • Declaration Block ({ ... }): The curly braces contain one or more declarations.
  • Declaration (color: #333;): A property-value pair.
  • Property (color): The CSS attribute you want to change.
  • Value (#333): The setting you want to apply.

Powerful Use Cases for Tag Selectors

Tag selectors are perfect for setting up a consistent, global "theme" for your site.

1. Setting a Typographic Baseline with `body`

The most common use for a tag selector is styling the body element. Because most text elements inherit properties like `font-family` and `color` from their parent, styling the `body` sets a default for the entire page.

body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
line-height: 1.6;
color: #4a4a4a;
background-color: #fefefe;
}

2. Styling Headings and Paragraphs (`h1`-`h6`, `p`)

Instead of styling every single heading with a class, you can set base styles for all headings and paragraphs to ensure consistent typography and spacing (vertical rhythm).

h1, h2, h3, h4, h5, h6 {
margin-top: 1.5rem;
margin-bottom: 1rem;
font-weight: 700;
}

p {
margin-bottom: 1rem;
}

Notice the use of grouping (,). This applies the same rule to all six heading levels, which is incredibly efficient.

3. Normalizing Link Styles (`a`)

Tag selectors are perfect for changing the default browser appearance of all links (<a>) to match your site's branding.

a {
color: #007bff;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

The Universal Selector (`*`)

The universal selector is a special type of selector that selects every single element on the page. It's most famously used in a "CSS reset" to change the default `box-sizing` behavior.

Default Behavior

A `div` with `width: 100px` and `padding: 20px` will actually be `140px` wide.

With `border-box`

A `div` with `width: 100px` and `padding: 20px` will remain `100px` wide. The padding is included *inside* the width.

This single rule is arguably the most popular CSS snippet in modern web development, as it makes layout math far more intuitive:

* {
box-sizing: border-box;
}

Tag Selectors and Specificity

This is the most important concept to understand: **tag selectors have the lowest specificity**. This is not a weakness; it is their greatest strength.

Specificity is the algorithm browsers use to decide which CSS rule applies if two or more rules target the same element.

  • Tag Selector (e.g., `p`): Specificity = 0-0-1
  • Class Selector (e.g., `.highlight`): Specificity = 0-1-0
  • ID Selector (e.g., `#main-content`): Specificity = 1-0-0

Because 0-1-0 is "larger" than 0-0-1, a class selector **will always override** a tag selector.

p {
color: black; /* Specificity: 0-0-1 */
}

.featured-paragraph {
color: blue; /* Specificity: 0-1-0 */
}

If you have <p class="featured-paragraph">, it will be **blue**. The class rule wins.

Key Takeaway: Use tag selectors to set the default styles for your entire site. Then, use more specific class selectors to create variations and exceptions. This "base + modifications" strategy is the core of clean, scalable CSS.

CSS Tag Selector Glossary

Selector
A CSS pattern that "selects" HTML elements on a page so they can be styled. The selector is the part of the CSS rule before the opening curly brace {.
Tag Selector (or Type Selector)
The most basic selector. It targets all HTML elements that match a specific tag name. Example: <p> selects all <p> elements.
Declaration Block
The section of a CSS rule enclosed in curly braces { ... }. It contains one or more declarations.
Declaration
A single CSS instruction consisting of a property and a value, separated by a colon ( : ) and ending with a semicolon ( ; ). Example: color: blue;.
Property
The name of the CSS attribute to be modified. Examples: font-family, background-color, margin.
Value
The setting applied to a CSS property. Examples: 1.5rem, red, bold.
Grouping Selectors
The practice of applying a single declaration block to multiple selectors by separating them with a comma ( , ). Example: h1, h2, h3{ color: black; }.
Universal Selector ( * )
A special selector that targets every single element on the HTML page. It has a specificity of 0-0-0.
Specificity
The algorithm used by browsers to determine which CSS rule is the most "specific" and should be applied when multiple rules conflict. Tag selectors have the lowest specificity (0-0-1).
Inheritance
A CSS mechanism where properties (like color and font-family) applied to a parent element are passed down, or "inherited," by their child elements. This is why styling body is so powerful.
CSS Reset
A collection of CSS rules, often using tag and universal selectors, designed to remove or normalize the default styles provided by browsers (e.g., margins on body, padding on ul).

Credibility and Trust

About the Author

Author's Avatar

Todotutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust, maintainable stylesheets for large-scale applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!