Retour aux articles
Wednesday, January 8, 20259 vues1

CSS Box Model: Understanding Margins, Borders, Padding, and Content

Mike Codeur

CSS

If you want to master web design, understanding the CSS Box Model is essential. This model explains how HTML elements are displayed on a webpage and how their dimensions are calculated.

What is the CSS Box Model?

The Box Model defines the structure of each HTML element as a box. Each box consists of four parts:

  1. Content: The area where your text or images are located.
  2. Padding: The space between the content and the border.
  3. Border: The outline surrounding the padding.
  4. Margins: The outer space that separates the element from others.
JSX
+---------------------------+
|        Margin             |
|  +---------------------+  |
|  |       Border         |  |
|  |  +---------------+  |  |
|  |  |   Padding      |  |  |
|  |  | +-----------+  |  |  |
|  |  | |  Content  |  |  |  |
|  |  | +-----------+  |  |  |
|  |  +---------------+  |  |
|  +---------------------+  |
+---------------------------+

Visual Example

Let’s imagine a profile card styled with margins, borders, and padding.

HTML
<div class="card">
  <h3>Jane Doe</h3>
  <p>Web Developer</p>
</div>
CSS
.card {
  margin: 20px; /* Outer space */
  padding: 15px; /* Inner space around the content */
  border: 2px solid #000; /* Border */
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); /* Shadow for decorative effect */
}

Margins, Padding, and Borders: Understanding Their Roles

Margins

Margins are used to create space between elements. They are not affected by background color or borders.

Padding

Padding adds space between the content and the border, improving readability.

Borders

Borders surround the padding and can be styled in various ways: solid, dotted, inset, etc.

Practical Example: Creating a Stylish Card

Here’s how to apply Box Model concepts to create an attractive card.

HTML
<div class="profile-card">
  <h3>John Smith</h3>
  <p>UX/UI Designer</p>
  <button class="btn">Contact</button>
</div>
CSS
.profile-card {
  margin: 30px auto; /* Center the card with margins */
  padding: 20px; /* Space around the content */
  border: 1px solid #ddd; /* Subtle border */
  border-radius: 8px; /* Rounded corners */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Shadow for an elegant effect */
  max-width: 300px;
  text-align: center;
}

.btn {
  margin-top: 10px;
  padding: 10px 20px;
  border: none;
  background-color: #007BFF;
  color: #fff;
  border-radius: 4px;
  cursor: pointer;
}

.btn:hover {
  background-color: #0056b3;
}

Conclusion

The CSS Box Model is the foundation of all web design. By mastering margins, padding, borders, and shadows, you can create professional and responsive designs. Experiment with these concepts to enhance your CSS skills!

Abonnes-toi à la NewsLetter

Apprends les meilleures pratiques pour devenir un développeur web moderne (JavaScript / React / Next).

Gagner sa vie grâce au code
Devenir développeur Freelance
+35 000 développeurs déjà inscrits.

Accès instantané. Aucune carte de crédit requise.

Rejoins +35 000 développeurs déjà inscrits.