📡 You're offline — showing cached content
New version available!
Quick Access

CSS Basics: Selectors, Properties, and the Box Model

Learn CSS fundamentals — selectors, specificity, the box model (margin, border, padding, content), display types, and the cascade.

EzyCoders Admin April 17, 2026 7 min read 0 views
CSS Basics: Selectors, Properties, and the Box Model
Share: Twitter LinkedIn WhatsApp

What is it?

CSS (Cascading Style Sheets) controls visual presentation of HTML — colours, fonts, layouts, spacing, and animations. CSS rules consist of a selector (what to style) and declarations (how to style it).

Why does it matter?

Without CSS, web pages are plain text. Every design element — colours, fonts, spacing, layouts — comes from CSS. Understanding the box model and cascade prevents the most common layout bugs that frustrate beginners.

Learn CSS fundamentals — selectors, specificity, the box model (margin, border, padding, content), display types, and the cascade.

Real-World Use Cases

  • 🎨 Product card styling - Padding inside the card, border around it, margin between cards — the box model controls every spacing decision.
  • 📱 Responsive layout - CSS display properties and the box model are the foundation of every responsive grid and flexbox layout.
  • 🖼️ Button design - Border-radius rounds corners, padding adds internal spacing, background-color sets the fill — pure CSS.
  • 🗂️ Navigation bar - Horizontal nav uses display:flex, with padding on links and a border-bottom separating nav from content.

CSS Selectors and Specificity

p   { color: #374151; }          /* element — lowest specificity */
.btn { background: #6366f1; }     /* class */
#header { height: 60px; }         /* ID — high specificity */

nav a    { text-decoration: none; }   /* descendant */
nav > a  { font-weight: 600; }        /* direct child */

a:hover  { color: #4f46e5; }         /* pseudo-class */
li:first-child { font-weight: bold; }
input:focus { outline: 2px solid #6366f1; }

p::first-line { font-size: 1.2em; }  /* pseudo-element */
li::before { content: "v "; color: #6366f1; }

input[type="email"] { border-color: #10b981; } /* attribute */

The Box Model

/* Every element: Content, Padding, Border, Margin */
.card {
    width:   300px;
    padding: 20px;          /* space INSIDE the border */
    border:  1px solid #e2e8f0;
    border-radius: 8px;
    margin-bottom: 16px;    /* space OUTSIDE the border */
}

/* ALWAYS add this to every project */
* { box-sizing: border-box; }
/* Makes width INCLUDE padding and border (intuitive!) */

Display Types

/* block: full width, new line (div, p, h1) */
.block { display: block; }

/* inline: stays in text flow, no width/height (span, a) */
.inline { display: inline; }

/* inline-block: inline flow + accepts width/height */
.badge { display: inline-block; padding: 2px 8px; }

/* none: completely removed */
.hidden { display: none; }

/* flex and grid — covered in dedicated posts */
.nav  { display: flex; gap: 16px; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); }

Q: What is the difference between margin and padding?

Padding is the space INSIDE the element between content and border — it has the element background color. Margin is space OUTSIDE the element between its border and neighboring elements — always transparent. Use padding for internal spacing; use margin for spacing between elements.

🧩
Test Your Knowledge
50 questions · Takes ~1500 seconds

Answer these questions to confirm you understood the article. Instant feedback on every answer.

Q1
What is the primary purpose of this topic in DevOps development?
Q2
In a DevOps technical interview, what do interviewers test?
Q3
What is the time/space complexity consideration for this DevOps topic?
Q4
When should a DevOps developer apply this technique?
Q5
What is the most common mistake when using this DevOps pattern?
Q6
Best practice for DevOps concept #6?
Q7
Best practice for DevOps concept #7?
Q8
Best practice for DevOps concept #8?
Q9
Best practice for DevOps concept #9?
Q10
Best practice for DevOps concept #10?
Q11
Best practice for DevOps concept #11?
Q12
Best practice for DevOps concept #12?
Q13
Best practice for DevOps concept #13?
Q14
Best practice for DevOps concept #14?
Q15
Best practice for DevOps concept #15?
Q16
Best practice for DevOps concept #16?
Q17
Best practice for DevOps concept #17?
Q18
Best practice for DevOps concept #18?
Q19
Best practice for DevOps concept #19?
Q20
Best practice for DevOps concept #20?
Q21
Best practice for DevOps concept #21?
Q22
Best practice for DevOps concept #22?
Q23
Best practice for DevOps concept #23?
Q24
Best practice for DevOps concept #24?
Q25
Best practice for DevOps concept #25?
Q26
Best practice for DevOps concept #26?
Q27
Best practice for DevOps concept #27?
Q28
Best practice for DevOps concept #28?
Q29
Best practice for DevOps concept #29?
Q30
Best practice for DevOps concept #30?
Q31
Best practice for DevOps concept #31?
Q32
Best practice for DevOps concept #32?
Q33
Best practice for DevOps concept #33?
Q34
Best practice for DevOps concept #34?
Q35
Best practice for DevOps concept #35?
Q36
Best practice for DevOps concept #36?
Q37
Best practice for DevOps concept #37?
Q38
Best practice for DevOps concept #38?
Q39
Best practice for DevOps concept #39?
Q40
Best practice for DevOps concept #40?
Q41
Best practice for DevOps concept #41?
Q42
Best practice for DevOps concept #42?
Q43
Best practice for DevOps concept #43?
Q44
Best practice for DevOps concept #44?
Q45
Best practice for DevOps concept #45?
Q46
Best practice for DevOps concept #46?
Q47
Best practice for DevOps concept #47?
Q48
Best practice for DevOps concept #48?
Q49
Best practice for DevOps concept #49?
Q50
Best practice for DevOps concept #50?
EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment