This tutorial clarifies the distinction between CSS margins and padding, illustrating their impact on webpage element spacing. We'll explore margin collapsing, the implications of various units in responsive design, and conclude with CSS margin and padding layout techniques.
Key Concepts:
box-sizing: content-box
adds padding and borders to the element's width and height, frequently causing layout issues. Setting box-sizing: border-box
is a common solution.The CSS Box Model Explained:
CSS elements are rectangular boxes composed of:
The following diagram illustrates this structure:
Understanding CSS Box Sizing:
Box sizing is a frequent source of confusion for CSS newcomers. Two 50% width elements might not fit their container due to added padding and borders.
By default (box-sizing: content-box
), padding and borders increase the element's overall width. To simplify layout, set box-sizing: border-box
so padding and borders are included within the specified width. CSS resets often apply border-box
globally:
html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; }
Experiment with interactive demos to solidify your understanding.
Setting Padding in CSS:
Control padding using padding-top
, padding-right
, padding-bottom
, padding-left
, or the shorthand padding
property:
/* All sides */ padding: 20px; /* Vertical | Horizontal */ padding: 2em 4em; /* Top | Horizontal | Bottom */ padding: 1em 20px 2em; /* Top | Right | Bottom | Left */ padding: 10px 10% 2em 15%;
Setting Margin in CSS:
Similar to padding, use margin-top
, margin-right
, margin-bottom
, margin-left
, or the shorthand margin
property:
/* All sides */ margin: 10px; /* Vertical | Horizontal */ margin: 2em 4em; /* Top | Horizontal | Bottom */ margin: 2em auto 2em; /* Top | Right | Bottom | Left */ margin: 10px 10% 2em 15%;
Margins create space between elements.
Margin and Padding Considerations:
Units: Avoid absolute units (pixels) for margins and padding in responsive design. Percentages or relative units (em, rem) adapt better to screen size and font changes.
Unit Calculation: Browsers calculate margin and padding differently based on the unit used (percentage based on parent width, em based on element font size, viewport units based on viewport dimensions).
Margin Collapsing: Adjacent siblings' top and bottom margins can collapse into a single margin (the larger of the two). Adding padding or a border prevents this.
Practical Applications:
Centering: Center block-level elements horizontally using margin: 10px auto;
.
Spacing Elements: Use margins to space elements apart, especially useful with Flexbox.
Maintaining Aspect Ratios: Use padding-top as a percentage (calculated from the desired aspect ratio) on a parent element with height: 0 to maintain image aspect ratios.
Conclusion:
This tutorial provides a solid foundation in understanding and using CSS margins and padding. Further exploration of advanced techniques will enhance your CSS skills.
The above is the detailed content of How to Set CSS Margins and Padding, and Cool Layout Tricks. For more information, please follow other related articles on the PHP Chinese website!