Chapter 10. CSS: Sizing and Spacing

In this chapter, we will dive into the essential concepts of sizing and spacing in CSS. These concepts are crucial for controlling the layout and spacing of elements on a web page, making it visually appealing and functional. Understanding the box model, how to adjust width
and height
, and how to manipulate padding
, margin
, and box-sizing
are fundamental to creating professional layouts.
The following topics are covered in this chapter:
CSS Box Model
In this section, we'll explore the CSS box model, which forms the foundation for understanding how elements are sized and spaced on a page. The box model consists of four key areas: content, padding, border, and margin. These areas define how elements are structured and how space is distributed around them. A clear understanding of the box model is crucial for effective layout design.
Width and Height
In this section, we'll cover the width
and height
properties, which define the size of an element. These properties can be set using various units like pixels, percentages, em
, and rem
. We'll also discuss how these properties interact with the box model, especially in how padding, borders, and margins affect an element's total size. By default, the width and height apply to the content box, but we'll explain how to include padding and borders in the overall measurement.
Padding
In this section, we'll examine the padding
property, which adds space between the content of an element and its border. Padding can be applied uniformly to all four sides, or you can specify values for each side individually: padding-top
, padding-right
, padding-bottom
, and padding-left
. Proper use of padding helps create breathing room around content, making layouts more readable and visually appealing.
Margin
In this section, we'll cover the margin
property, which controls the space between elements. Margin is applied to the outermost area surrounding an element, creating visual separation from other elements. Just like padding, margin can be set for all sides or specific sides, offering flexibility in layout design and element positioning.
Margin and Padding for Specific Side
In this section, we'll look into targeting specific sides with the margin
and padding
properties. Instead of applying values to all sides, you can use individual properties like margin-top
, margin-right
, margin-bottom
, and margin-left
(or their padding counterparts). This gives you greater control over spacing, which is especially helpful in more complex layouts.
margin: auto
In this section, we'll demonstrate how to use margin: auto
to center elements horizontally in CSS. This technique works by automatically adjusting the left and right margins, distributing the remaining space evenly on both sides of the element. It's a common method for centering block-level elements like images or containers within a parent element.
Box-Sizing
In this section, we'll cover the box-sizing
property, which dictates how the total width and height of an element are calculated. By default, the width and height are applied to the content box, but when you apply box-sizing: border-box
, the padding and border are included within the width and height. This simplifies layout calculations, as you no longer have to account for padding and borders separately when setting element sizes.
Learn offline for better focus!
A book for this course is available on Amazon.
HTML & CSS Visual Guide
Step By Step Manual for Complete Beginners with Zero Coding Experience to Build Stunning Websites from Scratch
Get the Book Now