CSS3 Syntax and Three Ways to Add CSS: Rules, Selectors, Properties, Values, Comments and Basic Selectors
Anatomy of a CSS Rule
css
p {
color: #333;
font-size: 1.125rem;
}
| Part | Example | Meaning |
|---|---|---|
| Selector | p | Which elements to style |
| Declaration | color: #333; | One style instruction |
| Property | color | What to change |
| Value | #333 | What to change it to |
A rule can have many declarations. Each ends with a semicolon. The rule is wrapped in curly braces.
Three Ways to Add CSS
| Way | Where | When to use |
|---|---|---|
| External | A .css file linked with link | Best for almost all sites. One file styles many pages |
| Internal | A style element in the head | A quick demo or single page |
| Inline | A style="..." attribute | Rarely. Hard to maintain and hard to override |
html
<!-- External -->
<link rel="stylesheet" href="css/style.css">
<!-- Internal -->
<style>
h1 { color: teal; }
</style>
<!-- Inline -->
<p style="color: crimson;">Red text</p>
Comments
css
/* This is a CSS comment */
body { margin: 0; } /* so is this */
Basic Selectors
css
/* Element selector */
h2 { color: #0b5fff; }
/* Class selector: elements with class="note" */
.note { background: #fff7d6; padding: 1rem; }
/* Id selector: one element with id="hero" */
#hero { min-height: 60vh; }
/* Grouping: same style for several selectors */
h1, h2, h3 { font-family: Georgia, serif; }
/* Universal selector: everything */
* { box-sizing: border-box; }
html
<section id="hero">
<h2>Welcome</h2>
<p class="note">New here? Start with the basics.</p>
</section>
Classes first Use classes for most styling. Ids must be unique and are harder to override. Name classes by purpose, like
.btn or .card, not by color like .red.
Common Mistakes
- Missing semicolon or curly brace: the next rule may stop working
- Misspelled property names: the browser silently ignores them
- A wrong file path in
link - Forgetting the dot before a class name or the hash before an id
Practice
- Create
style.cssand stylebody,h1andp - Make a
.noteclass and use it on two paragraphs - Use DevTools to turn a declaration off and on
Conclusion
A CSS rule has a selector and declarations. Use an external stylesheet, and style with classes.
Quick Summary: Rule anatomy; three ways to add CSS; comments; basic selectors; mistakes.

