A CSS selector is the pattern in front of a declaration block that tells the browser which elements to style. A CSS class selector, written with a dot like .card, matches every element whose class attribute contains that token. An ID selector, written with a hash like #lead-card, matches the element whose id has that value. For everyday styling I reach for classes first, because they are reusable and easier to override later.
The input for this walkthrough is a small HTML document with its CSS in a style block, and the output is a live preview you can judge by eye. You will build the page in the HTML Code Editor, compare a class rule, an ID rule and a two-class rule, then paste the CSS into a formatter to tidy it. Nothing here needs a build step or an account.
Table of Contents

What Is a CSS Selector, and What Does a CSS Class Selector Match?
A selector is the part of a CSS rule that comes before the curly braces. It describes which elements the declarations inside the braces apply to. In the rule below, .card is the selector and padding: 1rem is the declaration.
.card {
padding: 1rem;
}
A CSS class selector starts with a dot and matches elements whose class attribute includes that name. The attribute holds a space-separated list of tokens, so one element can carry several classes and many elements can share the same one. In this markup, .card matches both articles.
<article class="card featured">Featured</article> <article class="card">Regular</article>
MDN documents the syntax in its class selectors reference, and the selector language itself is specified in W3C Selectors Level 4, a Working Draft dated 2026-01-22. Class names in this article stay simple identifiers made of letters and hyphens, because some otherwise permitted HTML values need escaping when you write them in CSS.

CSS Class Selector vs ID Selector: What Actually Differs
Both selectors pick elements by an attribute, so the difference lies in how those attributes are meant to be used. According to the WHATWG HTML Standard, an id value must be nonempty and unique among IDs in its element tree. A class can repeat anywhere on the page. An element can also have both attributes at once, which the sample page later in this article does.
| Question | Class selector | ID selector |
|---|---|---|
| Syntax | .card | #lead-card |
| HTML attribute | class | id |
| Reuse | Many elements, many tokens per element | One element per ID in the tree |
| Specificity | 0-1-0 for .card | 1-0-0 for #lead-card |
| Typical use | Reusable styles and combinations | Fragment links, label references, unique hooks |
An ID has a job beyond styling. It is the target of a fragment link such as <a href="#lead-card">, and forms and scripts refer to it too. That is where an ID earns its place. A CSS ID selector is still valid for styling, so choosing classes for routine styling is a practical recommendation, not a rule of the language.
One misconception is worth correcting. An ID selector is not guaranteed to match exactly one element. If a page repeats an ID, the HTML is nonconforming, but #lead-card can still match every element that carries it. Keeping IDs unique is your job when you write the markup.

CSS Selector for Two Classes on One Element
To style only elements that carry two specific classes, chain the class selectors with no space between them. A CSS selector for two classes such as .card.featured matches an element whose class list contains both card and featured. The order of the tokens in the HTML attribute does not matter, so class="featured card" matches too.
.card {
border: 2px solid gray;
}
.card.featured {
border-color: orange;
}
With the two articles from earlier, the first rule styles both cards and the second changes only the border color of the first. Writing the selector by class this way lets you build a base style and a modifier without inventing a new name for every variation.
A space changes the meaning completely. .card .featured is a descendant selector: it matches a .featured element that sits somewhere inside a .card.
<div class="card"> <p class="featured">Nested</p> </div>
.card .featured {
font-weight: bold;
}
Here the paragraph is matched because it is inside the card. The same rule would match nothing in the earlier markup, where both classes sit on one element. Remember the two forms this way: no space means one element with both classes, and a space means a nested element.
Why a Class Is Usually Easier to Override Than an ID
When several rules set the same property on the same element, the browser uses the cascade to pick a winner. Specificity is one part of that. It is usually written as three numbers: the number of ID selectors, then class-level selectors, then element types. Under otherwise equal cascade conditions, .card scores 0-1-0, .card.featured scores 0-2-0 and #lead-card scores 1-0-0. The ID rule wins even if the class rule appears later in the stylesheet. MDN explains the scoring in its specificity guide.
Specificity is not the whole story. Origin, importance and cascade layers are considered before it, and source order only breaks a tie when the other relevant factors also tie. For ordinary author styles with no layers, the specificity numbers explain most beginner surprises.
This is why I prefer classes for routine styling. A rule written with an ID sets a high bar. To change it later you need another ID rule or something stronger, and each override raises the bar again. With classes you can add a modifier class and keep the stylesheet readable. That is a matter of maintenance, not validity, since both selectors are fine CSS.
If a rule seems to lose for a reason the numbers do not explain, specificity can behave unexpectedly with newer selector syntax. The article on a CSS rule inside where() that does not apply shows one such case and how to diagnose it.
Test Each Rule Live in the HTML Code Editor
The HTML Code Editor has syntax highlighting, auto-closing tags and a sandboxed live preview of the HTML you enter. It does not validate your markup, so you judge each rule by what the preview shows. Keeping the CSS in a style block in the same document makes the test self-contained.
- Open the HTML Code Editor and replace its contents with the document below.
- Look at the preview. Both articles have a gray border, and only the first has an orange one.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selector test</title>
<style>
.card { padding: 1rem; border: 2px solid gray; }
.card.featured { border-color: orange; }
</style>
</head>
<body>
<article class="card featured" id="lead-card">Featured</article>
<article class="card">Regular</article>
</body>
</html>
Now compare a class rule with an ID rule. Add these two rules inside the style block, with the ID rule first on purpose.
#lead-card { color: maroon; }
.card { color: navy; }
The first article’s text turns maroon and the second stays navy. The class rule comes later but still loses, because 1-0-0 outranks 0-1-0.
Next, delete the #lead-card rule and add color: maroon; to the .card.featured rule instead. The first article is still maroon, because 0-2-0 outranks 0-1-0 and no ID was needed. This is the habit I would build: reach the element with a combination of classes rather than an ID.
The CSS Editor offers property auto-completion and a live style preview, but its preview uses a sample page that you do not control. It is handy for trying property values, but your own class and ID names will only show an effect if that sample page happens to contain matching elements. Use it for experiments, and use the HTML Code Editor when the test depends on your own markup. For more on its strengths and limits, read the guide to the visual CSS editor and its live preview.
Tidy the Result With the CSS Beautifier
After a few experiments the stylesheet is often cramped or unevenly indented. The CSS Beautifier takes CSS in and gives formatted CSS back. Paste only the CSS, not the enclosing <style> tags, click the beautify control, then copy or download the output.
Suppose you pasted this compact input:
.card{padding:1rem;border:2px solid gray}.card.featured{border-color:orange;color:maroon}
The formatted result should look something like the following, though exact spacing can differ:
.card {
padding: 1rem;
border: 2px solid gray;
}
.card.featured {
border-color: orange;
color: maroon;
}
Formatting changes layout only. It does not validate the CSS, repair a typo or make a selector match an element it did not match before. To verify the result, paste the formatted CSS back into the style block and confirm that the preview looks the same as before.
If you later want smaller files for a live site, the CSS Minifier removes comments and unnecessary whitespace. Keep your readable source, because minified output is hard to edit, and check the preview again after minifying.
Troubleshooting a CSS Selector by Class That Does Not Match
When a rule does nothing, check the selector against the markup before blaming the browser. These checks cover the usual beginner mistakes:
- Dot versus hash. A class needs
.and an ID needs#. Writing#cardforclass="card"matches nothing. - Spaces in the wrong place. In HTML, two classes are separated by whitespace inside one attribute. In CSS,
.card.featuredhas no space, while.card .featuredmeans a descendant. - Reused IDs. Each ID should appear once in the document. Duplicates make the HTML nonconforming and can make a script or fragment link pick an unexpected element.
- Name mismatches. Class and ID values are case-sensitive in standards mode, so
Cardandcardare different names. Keep names to simple identifiers. - A stronger rule elsewhere. If the selector matches but the style does not show, another rule may have higher specificity. Compare the scores from earlier.
If your class and ID attributes vanish, the problem may be the editor rather than your CSS. The homepage editor is a Quill-based rich-text editor, and applying changes from its source view can drop attributes it does not model. The article on why the WYSIWYG source view removes class and ID attributes explains this and a safer workflow. When custom attributes must survive, edit the markup in the HTML Code Editor instead.
Neither editor checks that your selector is correct. The preview is your evidence, so change one rule at a time and watch what moves.
Frequently asked questions
What is a CSS selector?
A CSS selector is the pattern before a declaration block that matches the elements the declarations apply to. In .card { padding: 1rem; }, .card is the selector. Selectors can match by element type, class, ID, attribute, position and more.
What is the difference between a CSS class selector and a CSS ID selector?
A class selector uses a dot and can match many elements, and an element can carry several classes. An ID selector uses a hash and is meant for a single element, because an id value must be unique among IDs in its element tree. An ID selector also has higher specificity than a class selector.
How do I write a CSS selector by class for an element with two classes?
Chain the class names with no space between them, for example .card.featured. It matches an element that has both tokens in its class attribute, in any order. Adding a space, as in .card .featured, instead selects a nested descendant.
Can I use the same ID on more than one element?
No. The HTML standard requires an ID to be unique among IDs in its element tree, so duplicates make the document nonconforming. An ID selector can still match every element that repeats the value, which is why duplicates cause confusing behavior. Use a class when several elements need the same style.
Is a CSS ID selector wrong for styling?
No, it is valid CSS and works. The practical downside is specificity: an ID rule is harder to override than a class rule, so stylesheets that lean on IDs tend to need stronger and stronger selectors. I would use IDs for unique references like fragment links and classes for routine styling.
Why does my ID rule win even though my class rule comes later?
Under otherwise equal cascade conditions, an ID selector scores 1-0-0 and a single class scores 0-1-0, so the ID rule wins regardless of order. Source order only decides when the other cascade factors tie. Chain more classes or remove the ID rule to get the result you want.
Does the CSS Beautifier fix a selector that does not match?
No. Formatting improves indentation and spacing only. It does not validate the CSS, repair mistakes or change which elements a selector matches. After beautifying, check the live preview again to confirm that the page looks the same.
Next steps
A CSS class selector is the default tool for reusable styles, and a two-class selector such as .card.featured gives you precise variations without extra names. Keep IDs for jobs that need a unique reference, like fragment links, and remember that their higher specificity makes later overrides harder. The practical next step is to paste the sample document into the HTML Code Editor, change one rule at a time, and watch which selector wins. When the stylesheet looks the way you want, format it and keep that readable copy as your source.
Related tools and resources on HTML Editor Online:
- Visual CSS Editor Browser Live Preview
- CSS Where Specificity Rule Not Applying
- WYSIWYG Source View Removes Class and Id Attributes