A CSS Rule Is a Target Plus InstructionsCSS stands for Cascading Style Sheets, and it controls how the HTML you wrote actually looks.A rule is a selector saying what to style, then a block in curly braces saying how.Inside the block each declaration is a property, a colon, a value and a semicolon, such as color: #333333;.Property names come from a fixed list the browser knows, so inventing margen gets you nothing at all.A value the browser cannot understand is thrown away in silence, which is why one typo kills a single line with no error message.Lengths need units: px is a fixed pixel, rem is a multiple of the root font size, and % is measured against the parent element.Exampleh1 { color: #1d4ed8; font-size: 2rem; margin-bottom: 12px; } is one selector and three declarations.Colours can be written as a hex code #1d4ed8, a keyword like navy, or rgb(29, 78, 216).Missing the semicolon on a line usually breaks the line after it too, so check the declaration above the one that failed.Selectors Are How You AimAn element selector like p { } hits every paragraph on the page, which is useful for a base style and far too blunt for anything else.A class selector written .card { } styles any element carrying class="card", and you can reuse that class on 50 elements.An id selector written #main-nav { } styles exactly one element, because an id appears once per page.A descendant selector nav a { } means every link inside a nav and leaves links elsewhere alone.Pseudo-classes react to state, so a:hover changes a link under the pointer and a:focus shows where the keyboard has landed.Never delete the focus outline to tidy a design, because keyboard users then have no idea where they are on the page.Comma separated selectors share one block, so h1, h2, h3 { font-family: Georgia, serif; } styles all three in three words.The Cascade Settles Arguments Between RulesWhen two rules set the same property on the same element, the browser picks a winner using specificity.An id beats a class and a class beats a plain element, so #menu wins over .menu, which wins over ul.When specificity ties, the rule written further down the file wins, which is why the order of your stylesheet matters.Some properties inherit, so setting font-family on body passes it to every child unless a child overrides it, while margin and border never inherit.Developer tools show the winning declaration and cross out the losers, which beats guessing every time.Common MistakeAn inline style="color: red" on the element beats almost any rule in your stylesheet.Adding !important overrides everything, and the next fix then needs an even bigger hammer.If you find yourself writing !important twice in one file, the real problem is a selector that is too specific.The Box Model: Everything Is a RectangleDefinitionBox modelThe rule that every HTML element is a rectangle made of four layers: the content, then padding around it, then a border, then margin outside that.Every element is a box with four layers: the content in the middle, padding around it, a border around that, and margin outside.Padding sits inside the border so it takes the background colour, while margin sits outside and stays see through.A box set to width: 300px with 20px padding and a 2px border takes up 344px on screen by default, because width counts only the content.Setting box-sizing: border-box makes width include the padding and border, so 300px really is 300px.Most stylesheets open with * { box-sizing: border-box; } so that arithmetic never bites.Vertical margins between stacked elements collapse into one, so 30px under a heading plus 20px over a paragraph gives a 30px gap, not 50px.Flexbox Handles a Row, Grid Handles the Pagedisplay: flex on a container arranges its direct children along one axis, a row by default.justify-content spreads items along that axis, and space-between pushes the first and last to the edges, which is exactly what a navigation bar wants.align-items lines children up across the other axis, so align-items: center vertically centres a logo next to its menu.gap: 16px puts even spacing between children without hanging a margin on each one.display: grid with grid-template-columns: repeat(3, 1fr) makes three equal columns, where 1fr means one share of the space left over.Use grid for the overall page skeleton and flex inside each region, for a row of buttons or the contents of one card.Hintgrid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) builds a card gallery that reflows on its own with no breakpoints written.Give the container a temporary outline: 1px solid red to see where its edges really are.Flex and grid both apply to the container, not to the children, so check which element you put the display property on.One External Stylesheet Beats Fifty Inline StylesPut every rule in a separate file called styles.css and pull it in from the head with <link rel="stylesheet" href="styles.css">.One stylesheet serves the whole site, so changing the heading colour once changes it across all 20 pages.The browser caches that file after the first page, so every page after it loads faster.Keeping structure in HTML and appearance in CSS is called separation of concerns, and it means a redesign never touches the content.Comments written as /* navigation bar */ break a 400 line file into chunks you can actually find.Custom Properties Keep Your Palette in One PlaceA custom property is a variable you invent, written with two leading hyphens, as in --brand-blue: #1d4ed8;.Declare them once inside :root { } so every element on the page can reach them.Use one with var(), as in background-color: var(--brand-blue);.Storing five colours and three font sizes this way turns a rebrand into an eight line edit instead of a search through the whole file.Name them by job, --text-muted, rather than by appearance, --light-grey, so the name still makes sense after the colour changes.A dark theme can then be one small block that gives the same names different values.Choosing which colours go in the palette, and checking they have enough contrast, is covered in the interface design articles.Active recallA rule with an id selector and a rule with a class selector both set the colour, so which wins and why?Why does a 300px box with 20px padding measure 344px, and what one line fixes it?What is the difference between padding and margin when the element has a background colour?When would you reach for grid rather than flexbox?Give two reasons an external stylesheet beats styling each element inline.