CSS for Web Development

0% completed

Previous
Next
Introduction to the Box Model in CSS

The CSS box model is the foundation of layout design. It explains how every HTML element is treated as a box with different areas. These areas include the content, padding, border, and margin. Understanding the box model helps you control spacing, alignment, and how elements interact with each other on the page.

When you work with the box model, you can clearly see how each part of an element affects its size and position. This knowledge is important for building tidy and responsive layouts.

Syntax

Image

Explanation:

  • Content: This is where the element's text or image is displayed.
  • Padding: This area adds space around the content and inside the border.
  • Border: It wraps the padding and content, creating a visible frame.
  • Margin: This outermost area creates space between the element and other elements.

Example 1: Visualizing the Box Model

In this example, we create a simple <div> element to show how the box model works. We add padding, border, and margin to see how each part affects the element's overall size.

HTML

. . . .

Explanation:

  • Content Width: The content of the <div> is set to 200px.
  • Padding: Adding 20px of padding creates space inside the border, around the content.
  • Border: A 5px solid black border is drawn around the padded content.
  • Margin: A margin of 30px adds space outside the border, separating the element from others.
  • Outcome: The final element size includes the content width, padding, border, and margin. This example helps illustrate how each part of the box model contributes to the overall layout.

This lesson introduces you to the CSS box model. Knowing how content, padding, borders, and margins work together gives you a strong foundation for designing clear and organized layouts.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next