- The problem: a site that works but has no look
- What is CSS and how does it connect to HTML?
- Create style.css and link it
- Selectors: how to pick an element
- Colors and fonts first
- The box model: margin, padding and border
- Style the shop page
- Style the form from part 4
- See the result yourself
- Responsiveness: your first media query
- Validate your code
- Common CSS mistakes
- Wrap-up and what is next
- FAQ
- 🔗 Official sources
CSS is the styling language that turns your page from black text on a white background into a design with colors, fonts and tidy spacing.
In short: you create a style.css file, link it with the link tag, then pick elements with selectors and write their properties.
In this part you style the shop page and the order form you already built, and add your first media query so your site looks right on mobile.
In part 4 we built a fully working order form, but it looked like the browser default: narrow cramped fields with no colors or spacing. Today that changes completely. By the end of this part your page will have an elegant dark background, a clear readable font, eye-catching buttons and fields that are comfortable for the eye and the hand, all in about fifty lines of CSS that you will understand line by line.
1. The problem: a site that works but has no look
HTML builds structure and meaning, but it knows nothing about looks. Without styling, every website looks the same: black Times font on a white background with cramped elements. CSS (Cascading Style Sheets) is the layer that separates presentation from content: the same HTML file can appear in dozens of different designs just by swapping the CSS file.
2. What is CSS and how does it connect to HTML?
A single CSS rule has three parts: a selector that picks the element, a property that says what changes, and a value that says how:
h1 {
color: #f5b43c; /* property: value */
font-size: 32px;
}
There are three ways to add CSS to your page: inside a style tag in the page, as a style attribute on the element itself, or in a separate external file. The right way for any real project is the external file: one file controls the whole site's look, and editing one line in it reflects on every page.
3. Create style.css and link it
- In the project folder, next to
index.html, create a new file namedstyle.css. - In
index.htmladd this line inside<head>after the<title>tag:<link rel="stylesheet" href="style.css"> - Write a test rule in
style.css:body { background: #0b1420; color: #eef2f7; } - Save both files and refresh the page in the browser. If the background turns dark, the link works, and you can remove the test rule or build on it.
style.css; its status should be 200, not 404.4. Selectors: how to pick an element
Three selectors cover everything you need right now:
- The tag name:
pselects every paragraph in the page. - The class:
.product-cardselects every element withclass="product-card", the everyday workhorse. - The ID:
#orderselects the element withid="order", for one unique element on the page.
/* every h2 in the page */
h2 { color: #7fb3ff; }
/* every element with the product-card class */
.product-card {
background: #12203a;
border-radius: 12px;
padding: 20px;
}
/* the order section alone */
#order { margin-top: 40px; }
Notice how CSS comments are written between /* and */, and how the semicolon ; separates properties inside one rule.
5. Colors and fonts first
The biggest visual change with the fewest lines: a comfortable dark background, light text, one accent color for headings and buttons, and a clear font. Pick a simple palette and stick to it:
body {
background: #0b1420;
color: #eef2f7;
font-family: "Cairo", "Segoe UI", Tahoma, sans-serif;
line-height: 1.8;
margin: 0;
}
h1, h2, h3 { color: #f5b43c; }
a { color: #7fb3ff; }
The line-height property adds breathing room between lines for easier reading, and font-family lists fallback fonts: if the visitor's device lacks Cairo, the browser moves to the next one automatically.
6. The box model: margin, padding and border
Every HTML element is a box: the content inside, around it padding (inner spacing between content and frame), then border (the frame itself), then margin (outer spacing separating the box from its neighbors). Most "elements are stuck together" problems are solved with padding, and most "sections are glued" problems with margin:
.product-card {
background: #12203a; /* the box background */
padding: 20px; /* inner space around the content */
border: 1px solid #243b63; /* a subtle frame */
border-radius: 12px; /* rounded corners */
margin-bottom: 16px; /* space before the next box */
}

7. Style the shop page
First add classes to elements in the HTML file: class="product-card" on each product article card we built in part 3. Then write in style.css:
header {
background: #0f1d33;
padding: 24px;
text-align: center;
border-bottom: 3px solid #f5b43c;
}
nav a {
color: #eef2f7;
margin: 0 10px;
text-decoration: none;
}
main {
max-width: 900px;
margin: 24px auto;
padding: 0 16px;
}
footer {
background: #0f1d33;
text-align: center;
padding: 16px;
margin-top: 40px;
}
Notice margin: 24px auto; on main: the auto value for the left and right margins centers the content horizontally, the most famous centering trick in CSS.
8. Style the form from part 4
The form we built in part 4 worked but had cramped narrow fields. These rules make it comfortable and professional:
form {
background: #12203a;
padding: 24px;
border-radius: 14px;
}
label {
display: block;
margin: 14px 0 6px;
font-weight: bold;
}
input, select, textarea {
width: 100%;
padding: 10px;
border: 1px solid #243b63;
border-radius: 8px;
background: #0b1420;
color: #eef2f7;
font-family: inherit;
box-sizing: border-box;
}
input[type="radio"], input[type="checkbox"] {
width: auto; /* no full width for radio and checkbox */
}
button {
background: #f5b43c;
color: #0b1420;
border: none;
padding: 12px 32px;
border-radius: 8px;
font-size: 18px;
font-weight: bold;
cursor: pointer;
margin-top: 16px;
}
button:hover { background: #ffc95e; }
Three touches worth noticing: display: block on label puts each caption on its own line above its field, box-sizing: border-box makes width: 100% include the padding so the field never overflows the form, and button:hover is a special selector that lightens the button when the mouse passes over it.
9. See the result yourself
This is how the page looked before CSS, and how it became after fewer than sixty lines:


10. Responsiveness: your first media query
An @media rule applies styles only when a condition is met, most commonly the screen width. This snippet places the name and phone fields side by side on wide screens only, keeping them stacked on mobile:
@media (min-width: 700px) {
.row {
display: flex;
gap: 16px;
}
.row > div { flex: 1; }
}
The rule we follow in this series: write the mobile styles first (the simplest), then add large-screen enhancements inside @media (min-width: ...). This is the Mobile First approach used in every modern website.
11. Validate your code
To validate CSS itself there is the official W3C tool: CSS Validator. Upload your style.css and confirm it is error-free. One typo in a property name silently disables it with no message on the page, and the validator catches it instantly. Re-check the HTML too after adding classes with the W3C validator.
12. Common CSS mistakes
- Forgetting the semicolon between properties: the browser silently ignores everything after the error in the same rule.
- Writing
.productcardin CSS while the class in HTML isproduct-card; they will never match and nobody will warn you. - Using
!importantto force a style instead of understanding why it lost; it becomes a habit that corrupts the whole project later. - Confusing margin with padding: remember padding is space inside the box around the content, margin is space outside separating the box from others.
- Forgetting
box-sizing: border-box, so a field overflows its container once padding is added.
13. Wrap-up and what is next
Today your site turned from a functional skeleton into a real design: you linked an external CSS file, learned the three core selectors, colored the page and its fonts, understood the box model, styled the shop page and the order form, and wrote your first media query. In part 6 we continue CSS with modern layout: Flexbox and Grid to build a responsive product grid of two and three columns instead of stacked cards.
FAQ
Why is a rule I wrote not applying?
Open developer tools (F12), then Elements, and pick the element. In the Styles panel you will see which rules applied, which were struck through because another rule beat them, and any misspelled property. This panel is the number one CSS debugging tool.
What is the difference between a class and an ID?
A class repeats on any number of elements (all product cards, for example), while an ID belongs to one unique element on the page. Use classes for styling most of the time; the ID's higher precedence makes later design changes harder.
Do I need to memorize all the property names?
No. Memorize the principles (selectors, the box model, linking) and go back to the MDN reference whenever you need a property. Even professionals search for property names and values daily.
Should all styles go in one file?
At this stage, yes: a single style.css is plenty for a site of a few pages. When the project grows you can split it later; the right start is always the simplest one.
🔗 Official sources
- MDN: Learn CSS
- MDN: CSS selectors
- MDN: the box model
- web.dev: Learn CSS (Google's official course)
- W3C CSS Validator
Target keywords and how we picked them: "learn CSS", "CSS tutorial for beginners", "CSS box model" and "link CSS to HTML" all appear in Google autocomplete (real search demand; volumes unmeasured). In Arabic, "تعلم css" and "تنسيق صفحات html" are clear-intent terms suggested by autocomplete for Egyptian users.
Comments (0)
No comments yet. Be the first to comment!
Add Your Comment