You use CSS by writing a rule and attaching it to HTML. A rule has a selector and one or more declarations. Each declaration is a property and a value: color: #0f766e;.
The selector names the elements. p means every paragraph. .note means every element with class="note". The browser matches the selector, then paints those elements with the declarations.
One shape, three places to put it.
The rule itself stays selector { property: value; }. You place that rule on a style attribute, inside a style element, or in a .css file the page links.
Syntax
How to Write a CSS Rule
A selector, then declarations inside braces. End each declaration with a semicolon:
Both rules select p, so they compete for the same elements.
The second rule comes later in the stylesheet and sets color: #0f766e.
The paragraph is teal. Swap the two rules in Try It Yourself and the color follows whichever rule is last.
Frequently Asked Questions
Write a rule in the form selector { property: value; }. Attach that rule with the style attribute on one element, a style element in head for the current document, or a .css file linked from head. The selector names the HTML the declaration styles.
Put declarations on the element: <p style="color: #2563eb;">. There is no selector. The declarations apply to that element only. A second paragraph with no style attribute keeps the color from the stylesheet, or the browser default.
Place <style> in head and write normal CSS inside it. A rule such as p { color: #b45309; } styles every paragraph in that document. The same rules can move into a .css file later.
Save the rules in a file such as styles.css. In head, add <link rel="stylesheet" href="styles.css">. The browser loads that file and applies its rules. One file can style many HTML pages.
When two rules share the same selector, the browser uses the declaration that comes later in the stylesheet. p { color: #64748b; } followed by p { color: #0f766e; } paints the paragraph teal.