Selectors and properties
CSS (Cascading Style Sheets) controls how HTML looks: colours, fonts, spacing and layout. HTML says what something is; CSS says how it looks.
Anatomy of a CSS rule
css
selector {
property: value;
another-property: value;
}Preview
Three ways to add CSS
html
<!-- 1. External stylesheet: best for real projects (cached and reusable) -->
<link rel="stylesheet" href="styles.css">
<!-- 2. <style> in the head: handy for demos (like this site) -->
<style> p { color: navy; } </style>
<!-- 3. Inline style attribute: avoid; hard to maintain and overrides everything -->
<p style="color: navy">Hi</p>Basic selectors
Preview
An element can have several classes: class="card featured dark".
Combinators: selecting by relationship
Preview
Attribute selectors
Preview
Pseudo-classes: selecting by state or position
Preview
Pseudo-elements: styling part of an element
Preview
Pseudo-classes use one colon (:hover); pseudo-elements use two (::before).
Practice
Style a to-do list using only selectors (no classes on the list items): make every odd item light grey, the first item bold, strike through items whose data-done attribute is "true" ([data-done="true"]), and turn items blue on hover.
Preview
✎ Check your understanding
1.Which selector targets every element with class="card"?
2.What does ul > li select?