Перейти до основного вмісту
Frontend — це безкоштовний бонус CodeMentor AI; основний продукт — Python. Зареєструйся, щоб зберегти прогрес і відкрити всі 299 уроків фронтенду + 1,100+ уроків Python.Зареєструватись — безкоштовно
← 🎨 CSS as a design system·Модуль B1 · Урок 6
ЗавданняTwo cards: .card-broken with width 300px, padding 20px, border 1px solid #ccc (uses default content-box, ends up 342px wide). .card-fixed has the same width/padding/border BUT add box-sizing: border-box so it stays 300px wide.

box-sizing: border-box (і чому кожен проект підтримує це)

100 XP8 minFREE
Теорія

The default that everyone hates

.card {
  width: 300px;
  padding: 20px;
  border: 1px solid #ccc;
}

How wide is the rendered card? With the default box-sizing: content-box — width applies to the content area only:

  • Content: 300px
  • + padding: 20 left + 20 right = 40px
  • + border: 1 left + 1 right = 2px
  • Total: 342px

You said width: 300px but the box rendered at 342px. Every padding/border change breaks your layout math.

The fix

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

Now width: 300px means the total box width is 300px, including padding and border. Content shrinks to fit.

content-box (default):    [content: 300] [padding: 40] [border: 2]  → 342px total
border-box:               [        content: 258 + padding: 40 + border: 2        ] = 300px

Why every project enables it

width: 100% finally means "the same as the parent" — even when your child has padding. Grid + flexbox calculations don't break when you add padding. You can ship.

Modern projects ship this universal selector first thing. Tailwind ships it. Bootstrap ships it. Every reset stylesheet from the past 10 years includes it.

The mental model

  • content-box = width is the INNER size; box grows outward.
  • border-box = width is the OUTER size; content shrinks inward.

For UI work, you almost always want the outer size to be predictable. So you almost always want border-box.

2 вкладок
Живий перегляд
← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.