Make pages beautiful: selectors, colours, fonts, the box model, backgrounds, effects and positioning.
Every example opens in a live browser preview. Click ✎ Edit on any preview, or press L, to open the Code Lab.
style.css file usually styles the whole websitestyle.css.This is the same kind of HTML you wrote in Part 1, plus about 10 lines of CSS.
property: value; is one instruction. A rule can have many.; or }, using = instead of :, spaces in values like 32 px./* This is a CSS comment */ – different from HTML comments.| Method | Good for | Rating |
|---|---|---|
| Inline | Quick test, emails | ★ |
| Internal | A single page, demos | ★★ |
| External | Real websites (many pages) | ★★★ |
href, that the file ends in .css (not .css.txt), and press Ctrl+F5 to refresh.| Selector | Syntax | Example |
|---|---|---|
| Element | p | All paragraphs |
| Class | .name | class="name" |
| ID | #name | id="name" |
| Group | h1, h2 | comma = "and also" |
| Descendant | nav a | space = "inside" |
class="offer". Make all p text size 18px. Use a group selector h1, #special to make both italic with font-style: italic;..important class: dark red text, yellow backgroundstyle.css in the same folder.badge (padding, white text, rounded).pass green, .fail red, .pending orange backgroundclass="badge pass".student for all rows (grey border-left, padding)id="topper"#topper with gold background and bold texth1, h2, h3: Georgia font, colour #0b7285footer are white* to remove default marginModule 8: Colours, Units & Typography – choose beautiful colours and fonts.
Q1. Which selector targets class="price"?
Q2. Best way to style a 20-page website?
Q3. What does nav a select?
| Format | Example | Notes |
|---|---|---|
| Name | tomato | 140+ names; quick but limited |
| HEX | #e34c26 | Most common. #RRGGBB in base 16 |
| Short HEX | #f00 | Same as #ff0000 |
| RGB | rgb(227, 76, 38) | Red, Green, Blue: 0-255 each |
| RGBA | rgba(0, 0, 0, 0.5) | a = alpha (transparency) 0 to 1 |
| HSL | hsl(14, 77%, 52%) | Hue (0-360 on a wheel), Saturation, Lightness |
Screens make every colour by mixing red, green and blue light. 0,0,0 = black; 255,255,255 = white.
| Unit | Relative to | Use for |
|---|---|---|
px | Fixed screen pixels | Borders, small details |
% | Parent element | Widths of boxes |
rem | Root (html) font size | Font sizes, spacing |
em | Parent font size | Padding inside buttons |
vw / vh | 1% of window width / height | Full-screen sections |
font-family: 'Poppins', Arial, sans-serif; means: use Poppins; if not available use Arial; otherwise any sans-serif font. Always end with a generic family.<link> into your headtext-shadow: x y blur colour; – x right, y down, blur softness.Long lines tire the eyes. Use max-width: 65ch on paragraphs.
line-height: 1.5 to 1.7 for paragraphsline-height: 1.6; (no unit) means 1.6 x the font size, and it scales automatically.html { font-size } to 20px – every rem size grows. Make h1 colour hsl(200, 80%, 40%). Add text-shadow to the Hindi heading..c1 to .c4border-bottommargin: autohtml { font-size: 16px; }text-shadowModule 9: The Box Model – padding, borders and margins around every element.
Q1. Which value is 50% transparent black?
Q2. html font-size is 16px. What is 2rem?
Q3. Why end a font stack with sans-serif?
| Layer | Property | Think of it as |
|---|---|---|
| Content | width, height | The photo in a frame |
| Padding | padding | The white mount around the photo |
| Border | border | The wooden frame |
| Margin | margin | Space between frames on the wall |
| You write | Means | Clockwise |
|---|---|---|
padding: 10px; | All four sides 10px | ↻ all |
padding: 10px 20px; | top/bottom 10, left/right 20 | vertical | horizontal |
padding: 5px 10px 15px; | top 5, left/right 10, bottom 15 | |
padding: 1px 2px 3px 4px; | top, right, bottom, left | Clock: 12, 3, 6, 9 |
margin-top: 30px; | Only one side | -top -right -bottom -left |
border-box, the width you write is the width you get. Almost every modern website and framework (Bootstrap, Tailwind) uses it.Drag the yellow slider to change the window width.
margin: 0 auto = top/bottom 0, left/right shared equallymax-width instead of width lets the box shrink on phonestext-align: center centres the text inside; margin auto centres the box.container like this, usually 1100-1200px wide.| Property | Example |
|---|---|
border | 2px solid #ccc |
border-style | solid, dashed, dotted, double, none |
border-left | Only one side |
border-radius | 10px, 50% (circle), 20px 0 |
outline | Like border but does not change size; used for keyboard focus |
outline on links/buttons without adding another focus style. Keyboard users need to see where they are.| Value | What happens |
|---|---|
| visible | Spills out (default) |
| hidden | Cut off |
| scroll | Always shows scrollbars |
| auto | Scrollbar only if needed (best) |
border-radius: 16px). Add a dashed top border. Remove box-sizing – did the card get wider?* { box-sizing: border-box; }margin: 20px auto.container: max-width 600px, margin auto, white background.plan boxes, inline-block, width 180px.best: thicker coloured borderoverflow: auto so a scrollbar appears only when neededoverflow-wrap: anywherebox-sizing: border-box so they matchModule 10: Backgrounds, Gradients & Effects – banners, shadows and image filters.
Q1. Which layer shows the element's background colour?
Q2. padding: 10px 20px means:
Q3. How do you centre a block box with max-width 600px?
background-size: cover; background-position: center; fills the box with no gaps and crops the edges.center / cover = position / size in one line| Type | Syntax |
|---|---|
| Linear | linear-gradient(angle, c1, c2) |
| Radial | radial-gradient(circle, c1, c2) |
| Conic | conic-gradient(c1, c2, ...) |
| Hard stops | red 50%, blue 50% = sharp line |
grayscale(100%), turning colourful on :hover (Module 11).| Value | Result |
|---|---|
| fill | Stretched (faces look squashed) |
| cover | Fills, crops edges (best for avatars, cards) |
| contain | Whole image visible, may leave gaps (products) |
images/saree.jpg after the gradient with a comma. Try radial-gradient..btn: padding, white bold text, no border, radius 30px.b1 orange-to-red gradient.b2 purple-to-blue gradient at 135deg.b3 outline style (transparent background, gradient border via box-shadow inset)object-fit: cover + border-radius: 50%Module 11: Advanced Selectors & the Cascade – hover effects, zebra tables and who wins when rules fight.
Q1. Background fills the whole box and crops the photo:
Q2. Which shadow is the softest/lightest?
Q3. Make a rectangular photo a circle without squashing it:
| Symbol | Name | Family meaning |
|---|---|---|
A B (space) | Descendant | Children, grandchildren... |
A > B | Child | Only own children |
A + B | Adjacent sibling | Younger brother right next to you |
A ~ B | General sibling | All younger brothers/sisters |
a[href$=".pdf"]::after { content: " (PDF)"; }| Pseudo-class | When it applies |
|---|---|
:hover | Mouse is over it |
:focus | Clicked/tabbed into (inputs, links) |
:active | Being pressed |
:visited | Link already opened |
:checked | Checkbox/radio ticked |
:disabled | Disabled input |
:valid / :invalid | Passes / fails HTML validation |
:has(...) | Parent contains something (new, powerful) |
:nth-child(an+b) – n counts 0, 1, 2, 3... So 3n = 3, 6, 9 and 2n+1 = 1, 3, 5 (odd).| Pseudo-element | Selects |
|---|---|
::before / ::after | Fake content before/after (needs content) |
::first-letter | First letter (drop caps) |
::first-line | First line |
::placeholder | Placeholder text |
::selection | Text the user highlights |
:hover). Pseudo-elements use two (::before).!important beats normal rules (use rarely!)| Selector | Score (a,b,c) |
|---|---|
p | 0, 0, 1 |
.note | 0, 1, 0 |
p.note | 0, 1, 1 |
#tip | 1, 0, 0 |
nav ul li a:hover | 0, 1, 4 |
style="..." | beats all selectors |
#main div ul li a are hard to override later.font-family and color once on body and the whole page gets them.| Keyword | Means |
|---|---|
inherit | Copy the parent's value |
initial | Browser default for that property |
unset | inherit if inheritable, otherwise initial |
color: red !important; beats everything. It is a "last resort" – overusing it creates a war of !importants.odd to 3n. Make the active link's hover darker. Add li:last-child { font-weight: bold; }. Replace the tick with "\2605 " (a star).tbody tr:nth-child(even)tr:hovertd:first-childnav a:hover change background and text colour.active shows the current page:focus-visible outline for keyboard usersnav a + a to add a left border between links:focus blue border + soft glow:invalid red border only after typing (:not(:placeholder-shown)):valid green borderclass="req".req::after adds " *" in red::beforea[href$=".pdf"]::after::first-letter)::first-line)p:first-of-type::before::selection colourModule 12: Display, Position & Z-index – badges, sticky headers and floating buttons.
Q1. Which selects every 2nd table row?
Q2. Which has higher specificity?
Q3. ::after needs which property to show anything?
| Value | New line? | Width/height work? |
|---|---|---|
| block | Yes | Yes |
| inline | No | No (ignored) |
| inline-block | No | Yes – best of both |
| none | Removed completely | – |
| Takes space? | Clickable? | Screen readers? | |
|---|---|---|---|
display: none | No | No | Hidden |
visibility: hidden | Yes (gap) | No | Hidden |
opacity: 0 | Yes (gap) | Yes! | Read aloud |
| Value | Behaviour |
|---|---|
| static | Normal flow. top/left ignored |
| relative | Moves from its normal spot; gap stays |
| absolute | Leaves the flow; placed inside nearest positioned parent |
| fixed | Glued to the window, even when scrolling |
| sticky | Normal until you scroll past, then sticks |
float: left / right pushes the image to one side; text flows around itclear: both makes an element start below floats| Pattern | CSS |
|---|---|
| Sticky header | position: sticky; top: 0; z-index: 10; |
| Cart badge | parent relative, badge absolute; top; right |
| Caption on photo | parent relative, caption absolute; bottom: 0 |
| Floating chat button | position: fixed; right; bottom |
position: relative from .card – where does the ribbon go? Move the ribbon to the top-right. Add a heart icon ♡ in the bottom-right corner with absolute..cart with position: relative and inline-blockposition: sticky; top: 0https://wa.me/91... with the WhatsApp imageposition: fixed; right: 18px; bottom: 18pxfigure with position relative, inline-block, width 220pxul is absolute, top: 100%, hidden with display: noneli:hover > ul shows it with display: blockPart 3: Flexbox – the modern, easy way to line things up in rows and columns.
Q1. Which value keeps an element in the window corner even when scrolling?
Q2. A badge must sit on the corner of its card. The card needs:
Q3. width does nothing on a span. Why?
You have finished Part 2 of the Complete HTML + CSS Course.
Keep all your practice files in D:\my-website. You will reuse them in the capstone projects.