Build Your Website from Scratch (5): CSS Basics — Your Site's First Real Design

Link a CSS file, learn selectors and the box model, and style the shop page and order form with colors, fonts and your first mobile media query.

Build Your Website from Scratch (5): CSS Basics — Your Site's First Real Design
Link a CSS file, learn selectors and the box model, and style the shop page and order form with colors, fonts and your first mobile media query.

Build Your Website from Scratch (5): CSS Basics — Your Site's First Real Design

Share:
⏱ 7 min read🔄 Last updated: 2026-12-04✍ NOOR 4 TECH Team

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

  1. In the project folder, next to index.html, create a new file named style.css.
  2. In index.html add this line inside <head> after the <title> tag:
    <link rel="stylesheet" href="style.css">
  3. Write a test rule in style.css: body { background: #0b1420; color: #eef2f7; }
  4. 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.
💡 Tip: if nothing changes, the most common cause is a wrong file name or location. Open developer tools (F12), then the Network tab, and refresh: look for 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: p selects every paragraph in the page.
  • The class: .product-card selects every element with class="product-card", the everyday workhorse.
  • The ID: #order selects the element with id="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 */
}
Diagram of the CSS box model: content in the center, surrounded by padding, then border, then margin on the outside
Figure 1: the box model: content, then inner padding, then the border, then the outer margin

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:

Side-by-side comparison: the shop page before CSS with black text on a white background, and after with a dark background, product cards and a styled form in gold and blue
Figure 2: the shop page before and after adding CSS, with the exact same HTML file
The order form after styling: field captions above their fields, wide fields with dark backgrounds and rounded corners, and a prominent gold submit button
Figure 3: the order form after styling: comfortable fields and a clear button

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

  1. Forgetting the semicolon between properties: the browser silently ignores everything after the error in the same rule.
  2. Writing .productcard in CSS while the class in HTML is product-card; they will never match and nobody will warn you.
  3. Using !important to force a style instead of understanding why it lost; it becomes a habit that corrupts the whole project later.
  4. Confusing margin with padding: remember padding is space inside the box around the content, margin is space outside separating the box from others.
  5. Forgetting box-sizing: border-box, so a field overflows its container once padding is added.
⚠️ Warning: the browser shows no error message when it ignores a misspelled CSS property. Open developer tools (F12), then Elements, then Styles, and you will find the bad property struck through with a yellow warning sign.

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

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.

⭐ Featured Recommendation

📈 Double Your Site Traffic & Revenue with Featured Backlinks & SEO

Get a pinned Featured Backlink in our directory or a complete SEO audit by NOOR 4 TECH experts to dominate rankings.

Explore Featured Package ⭐
💼 NOOR 4 TECH Professional Services

Looking for a Technical Partner for Your Next Project?

We deliver enterprise web development, mobile apps, technical SEO, and AI automation.

Author

Ibrahim Moustafa

Electronics and communications engineer, working in IT since 2004. He reviews NOOR 4 TECH articles and tests its tools himself before publication.

Comments (0)

No comments yet. Be the first to comment!

Add Your Comment

Your email will not be published. Comments require review before publishing.