Перейти к основному содержимому
← 🎨 CSS as a design system·Модуль B8 · Урок 3
ЗаданиеBuild a card system where 'featured' status is detected via :has. .card { padding: 16px; border: 1px solid #e5e7eb; border-radius: 8px; }. .card:has(.featured-badge) { border-color: #F59E0B; box-shadow: 0 0 0 3px rgba(245,158,11,0.1); }. Two cards, one with <span class='featured-badge'>★</span>.

:has() — наконец-то появился родительский селектор

100 XP7 min
Теория

The selector the web waited 20 years for

.card:has(img) { padding-top: 0; }                /* cards that CONTAIN an image */
.card:has(.featured) { border: 2px solid gold; }  /* cards with a .featured child */
form:has(input:invalid) { border-color: red; }    /* form with any invalid input */

:has(selector) matches the OUTER element if the inner selector matches anywhere inside. Finally — a parent selector in CSS.

The killer use cases

Style parents based on children

li:has(> input[type="checkbox"]:checked) {
  text-decoration: line-through;
  opacity: 0.6;
}

Strikethrough a todo list item when its checkbox is checked. Pure CSS, no JS.

Layout shifts based on content

.layout:has(aside) { grid-template-columns: 1fr 240px; }
.layout:not(:has(aside)) { grid-template-columns: 1fr; }

Layout shifts based on whether a sidebar exists.

Form validation styling

form:has(:invalid) button[type="submit"] {
  background: #ccc;
  cursor: not-allowed;
}

Submit button disables visually when ANY input in the form is invalid.

Browser support

Shipped 2022-2023 in all evergreens. Safe in 2026.

Performance

:has does extra work — browsers optimize aggressively but avoid in HOT loops (every scroll frame). For static layout selectors, fine.

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.