Skip to main content

Command Palette

Search for a command to run...

CSS Basics – Styling the Web

Published
3 min read
CSS Basics – Styling the Web
A

I’m Arjun Saxena, a passionate software developer specializing in web engineering. I believe in writing code that creates real solutions to real problems. I love building efficient, user-friendly applications and constantly push myself to learn new technologies. Beyond coding, I enjoy sharing knowledge and growing together with others in the tech community.

HTML gives structure to a webpage, but CSS makes it beautiful 🎨
Without CSS, every website would look plain and boring.

In this blog, you’ll learn:

  • What CSS is and why it matters

  • The CSS Box Model (content, padding, border, margin)

  • How spacing affects layout

  • CSS specificity (which style wins and why)

  • Difference between class and ID

  • Common beginner mistakes to avoid

No complex design theory — just solid basics.

1️⃣ What is CSS?

Simple Definition:

CSS (Cascading Style Sheets) is used to style and layout web pages.

CSS controls:

  • Colors 🎨

  • Spacing 📦

  • Fonts ✍️

  • Layout 📐

Example: HTML Without vs With CSS

HTML only

<h1>Hello World</h1>
<p>This is a paragraph</p>

With CSS

h1 {
  color: blue;
}

p {
  font-size: 18px;
}

👉 Same content, better appearance.

2️⃣ Understanding the CSS Box Model (Very Important)

Every HTML element is treated as a box in CSS.

CSS Box Model = Content + Padding + Border + Margin

📦 CSS Box Model Diagram

https://blog.hubspot.com/hubfs/image-png-Jan-09-2023-05-33-48-6023-PM.png

ox Model Parts Explained

PartMeaning
ContentActual text or image
PaddingSpace inside the box
BorderWraps padding & content
MarginSpace outside the box

3️⃣ Box Model – Practical Example

<div class="box">Hello CSS</div>
.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 15px;
}

What Happens?

  • Content width = 200px

  • Padding adds space inside

  • Border surrounds content

  • Margin pushes other elements away

👉 Spacing directly affects layout.

4️⃣ Why Box Model Matters?

If you don’t understand the box model:

  • Layouts break ❌

  • Spacing feels confusing ❌

  • Elements don’t align ❌

✔ Box model = predictable layouts
✔ Essential for responsive design

5️⃣ CSS Selectors: Class vs ID

Class Selector (.)

.card {
  background-color: lightgray;
}
<div class="card"></div>
<div class="card"></div>

✔ Reusable
✔ Used multiple times

ID Selector (#)

#header {
  background-color: blue;
}
<div id="header"></div>

✔ Unique
❌ Use only once

Simple Rule:

👉 Class for styling
👉 ID for unique elements

6️⃣ CSS Specificity (Who Wins the Style?)

Sometimes multiple CSS rules apply to the same element.

Specificity decides which rule wins

Specificity Order (Low → High)

  1. Element selector (p)

  2. Class selector (.box)

  3. ID selector (#box)

  4. Inline styles (style="")

Example:

p {
  color: blue;
}

.text {
  color: green;
}

#para {
  color: red;
}
<p id="para" class="text">Hello</p>

👉 Text color = red
Because ID selector wins.

7️⃣ CSS Specificity Algorithm (Simple Table)

SelectorSpecificity Value
Element0-0-1
Class0-1-0
ID1-0-0
Inline styleHighest

✔ Higher value wins
✔ Avoid unnecessary conflicts

8️⃣ Common Beginner Mistakes (Must Avoid)

❌ Overusing inline styles

<p style="color:red">Bad practice</p>

❌ Using IDs everywhere
❌ Not understanding box model
❌ Fighting CSS with !important

Better Practice ✔

.text {
  color: red;
}

✔ Clean
✔ Reusable
✔ Maintainable

9️⃣ How CSS Improves a Web Page (Before & After)

Before:

  • No spacing

  • No colors

  • Hard to read

After:

  • Clear layout

  • Good spacing

  • Better user experience