学习库CSSThe Box Model

The Box Model

Control content, padding, borders, sizing, overflow, and spacing with confidence.

更新于 适用于 Modern CSS

Every rendered element occupies a box: content surrounded by padding, a border, and margin. Layout becomes predictable when declared dimensions include the parts you expect.

Use border-box sizing

reset.css
*, *::before, *::after {
  box-sizing: border-box;
}

With border-box, a declared width includes content, padding, and border. Without it, padding and border are added outside the width.

card.css
.card {
  width: min(100%, 32rem);
  padding: 1.5rem;
  border: 1px solid #ded9e1;
  margin-inline: auto;
}

Block, inline, and flow

Block boxes normally fill available inline space and start on a new line. Inline boxes flow with text. inline-block, flex, and grid create different formatting contexts.

Logical properties and overflow

Logical properties follow writing direction, making layouts more adaptable.

logical.css
.notice {
  padding-block: 1rem;
  padding-inline: 1.25rem;
  border-inline-start: 3px solid #f46537;
  overflow-wrap: anywhere;
}

Use overflow: auto for intentionally scrollable regions. Avoid hiding overflow simply to conceal a layout bug.

Keep this

Set global border-box, use logical properties, and distinguish internal padding from external layout spacing.

学习位置已保存在此设备上。