Frontend

CSS3

Referência completa de CSS3 — box model, seletores, Flexbox, Grid, Custom Properties, funções modernas, animações, Container Queries, scroll-driven animations e mais

Box Model

Cada elemento HTML é uma caixa retangular composta de quatro camadas: content, padding, border e margin. O box-sizing: border-box faz com que width e height incluam padding e border — o comportamento mais intuitivo e amplamente adotado. O padrão histórico content-box calcula o tamanho apenas do conteúdo interno.

/* Reset global recomendado */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.card {
  /* Com border-box: width total = 300px (padding e border incluídos) */
  /* Com content-box: width total = 300 + 32 + 2 = 334px */
  width: 300px;
  padding: 1rem;        /* 16px em todos os lados */
  border: 1px solid #e2e8f0;
  margin: 1rem auto;    /* 0 vertical, auto horizontal = centraliza */
  outline: 2px solid blue; /* outline NÃO afeta o layout — não ocupa espaço */
  outline-offset: 4px;
}

Margin collapsing — margens verticais adjacentes se fundem em uma única (o maior valor). Ocorre em três situações:

/* 1. Irmãos adjacentes: margem entre p1 e p2 = max(1rem, 2rem) = 2rem */
p { margin-block: 1rem; }
h2 + p { margin-top: 2rem; }

/* 2. Pai e primeiro/último filho — quando não há border/padding separando */
.container { margin-top: 20px; }
.container > p:first-child { margin-top: 30px; }
/* A margem do container será 30px, não 50px */

/* 3. Elemento vazio — top e bottom colapsam */
.vazio { margin-top: 10px; margin-bottom: 20px; } /* = 20px */

/* Como evitar collapsing: */
.container {
  padding-top: 1px;     /* qualquer padding quebra o colapso */
  border-top: 1px solid transparent;
  overflow: hidden;     /* cria BFC (Block Formatting Context) */
  display: flow-root;   /* BFC moderno sem efeitos colaterais */
}

Seletores avançados

CSS oferece um sistema de seleção poderoso. Combinadores definem relações estruturais; pseudo-classes filtram por estado e posição; :is(), :where() e :has() são os grandes modernizadores.

/* ── Combinadores ── */
div p        { }   /* descendente (qualquer nível) */
div > p      { }   /* filho direto */
h2 + p       { }   /* irmão adjacente imediato */
h2 ~ p       { }   /* todos os irmãos após h2 */
[data-theme="dark"] { } /* atributo exato */
[class^="icon-"]   { }  /* começa com "icon-" */
[href$=".pdf"]     { }  /* termina com ".pdf" */
[class*="btn"]     { }  /* contém "btn" */

/* ── Pseudo-classes estruturais ── */
li:first-child      { }
li:last-child       { }
li:nth-child(2)     { }             /* 2º filho */
li:nth-child(odd)   { }             /* filhos ímpares */
li:nth-child(even)  { }             /* filhos pares */
li:nth-child(3n+1)  { }             /* 1º, 4º, 7º... */
li:nth-last-child(2){ }             /* 2º a partir do fim */
p:nth-of-type(2)    { }             /* 2º <p> entre irmãos */
p:only-child        { }             /* único filho do pai */
p:only-of-type      { }             /* único <p> entre irmãos */
:root               { }             /* elemento raiz (= html normalmente) */
:empty              { }             /* sem filhos nem texto */

/* ── :is() — agrupa seletores com especificidade do mais específico ── */
:is(h1, h2, h3, h4) a { color: inherit; }  /* em vez de 4 regras */

/* ── :where() — como :is(), mas especificidade ZERO ── */
:where(article, section, aside) p { margin-block: 1em; }

/* ── :not() avançado — aceita lista de seletores ── */
a:not(:hover, :focus, .ativo) { opacity: 0.8; }
button:not([disabled]) { cursor: pointer; }

/* ── :has() — selector pai — joia do CSS moderno ── */
/* Card que contém uma imagem */
.card:has(img) { padding: 0; }

/* Formulário com campo inválido */
form:has(:invalid) .btn-submit { opacity: 0.5; pointer-events: none; }

/* Navegação quando o menu está aberto */
nav:has(.menu[aria-expanded="true"]) { background: #1e293b; }

/* Label de campo required */
label:has(+ input:required)::after {
  content: " *";
  color: red;
}

Pseudo-elementos

Pseudo-elementos criam conteúdo virtual no DOM — ou selecionam partes específicas do conteúdo.

/* ::before e ::after — conteúdo gerado antes/depois do elemento */
.badge::before {
  content: "🔥 ";  /* pode ser texto, url(), counter(), attr() */
}

.btn::after {
  content: "";       /* vazio para elementos decorativos */
  display: block;
  width: 100%;
  height: 2px;
  background: currentColor;
  transform: scaleX(0);
  transition: transform 200ms ease;
}

.btn:hover::after {
  transform: scaleX(1);
}

/* ::first-line e ::first-letter */
article p::first-letter {
  float: left;
  font-size: 3em;
  line-height: 0.8;
  margin-right: 0.1em;
  font-weight: bold;
}

article > p:first-of-type::first-line {
  font-variant: small-caps;
}

/* ::selection — texto selecionado pelo usuário */
::selection {
  background: #38bdf8;
  color: #0f172a;
}

/* ::placeholder — texto de placeholder no input */
input::placeholder {
  color: #94a3b8;
  font-style: italic;
  opacity: 1; /* Firefox reduz a opacidade por padrão */
}

/* ::marker — marcador de lista (bullet, número) */
li::marker {
  content: "→ ";
  color: #38bdf8;
}

/* ::backdrop — fundo do dialog e do fullscreen */
dialog::backdrop {
  background: rgb(0 0 0 / 0.6);
  backdrop-filter: blur(4px);
}

Cascade, especificidade e @layer

A cascata determina qual regra “ganha” quando há conflito. A ordem de prioridade: !important > especificidade > ordem de aparição > herança. @layer introduz camadas explícitas na cascata.

/*
  Cálculo de especificidade (A, B, C):
  A = IDs (#id)         → (1, 0, 0)
  B = Classes, atributos, pseudo-classes → (0, 1, 0)
  C = Elementos, pseudo-elementos        → (0, 0, 1)

  Exemplos:
  #nav .link:hover      → (1, 1, 1) = 111
  .card > p::first-line → (0, 1, 2) = 012
  button                → (0, 0, 1) = 001
  :is(h1, h2)           → especificidade do argumento mais específico
  :where(h1, h2)        → sempre (0, 0, 0)
*/

/* !important — sobrescreve tudo exceto outro !important mais específico */
/* Use apenas em: reset utilitário, estilos de acessibilidade, override de biblioteca */
.sr-only { position: absolute !important; width: 1px !important; }

/* @layer — define camadas explícitas na cascata */
/* Camadas posteriores têm prioridade sobre as anteriores */
@layer reset, base, tokens, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
}

@layer tokens {
  :root {
    --color-accent: #38bdf8;
    --space-4: 1rem;
  }
}

@layer components {
  .btn {
    padding: 0.5rem 1rem;
    background: var(--color-accent);
  }
}

/* Utilities sobrescrevem components mesmo com especificidade menor */
@layer utilities {
  .mt-0 { margin-top: 0; }
}

Custom Properties avançadas

Custom Properties (variáveis CSS) são valores reutilizáveis que cascateiam como qualquer propriedade. @property permite definir tipos, valores iniciais e comportamento de herança — habilitando animações de custom properties.

/* Definição e uso básico */
:root {
  --color-accent: #38bdf8;
  --space-4: 1rem;
  --radius-md: 0.5rem;
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  --transition-fast: 150ms ease;
}

.card {
  background: var(--color-accent);
  border-radius: var(--radius-md);
  /* Fallback — usado se a variável não existir */
  color: var(--color-text, #0f172a);
  /* Fallback encadeado */
  font-size: var(--size-custom, var(--size-base, 1rem));
}

/* Composição — combinar variáveis */
:root {
  --hue: 210;
  --saturation: 90%;
  --lightness: 50%;
  --color-base: hsl(var(--hue) var(--saturation) var(--lightness));
}

/* Temas — sobrescreve no escopo */
[data-theme="dark"] {
  --color-bg: #0f172a;
  --color-text: #f8fafc;
}

[data-theme="light"] {
  --color-bg: #ffffff;
  --color-text: #0f172a;
}

/* @property — typed custom properties (CSS Houdini) */
@property --progress {
  syntax: '<number>';         /* type: <length>, <color>, <percentage>, <angle>... */
  inherits: false;
  initial-value: 0;
}

@property --gradient-angle {
  syntax: '<angle>';
  inherits: false;
  initial-value: 0deg;
}

/* Agora é possível animar --gradient-angle */
.rotating-gradient {
  background: conic-gradient(from var(--gradient-angle), #38bdf8, #818cf8, #38bdf8);
  animation: girar 4s linear infinite;
}

@keyframes girar {
  to { --gradient-angle: 360deg; }
}

/* Modificar via JavaScript */
document.documentElement.style.setProperty('--color-accent', '#f59e0b');
const valor = getComputedStyle(document.documentElement).getPropertyValue('--color-accent');

Flexbox completo

Flexbox é o modelo de layout unidimensional do CSS. O container define o contexto (eixo, quebra, alinhamento); os itens definem como crescem, encolhem e se posicionam individualmente.

/* ── Propriedades do Container ── */
.flex-container {
  display: flex;                    /* ou inline-flex */
  flex-direction: row;              /* row | column | row-reverse | column-reverse */
  flex-wrap: wrap;                  /* nowrap | wrap | wrap-reverse */
  /* Shorthand: direction + wrap */
  flex-flow: row wrap;

  /* Alinhamento no eixo principal (row = horizontal) */
  justify-content: space-between;
  /* flex-start | flex-end | center | space-between | space-around | space-evenly */

  /* Alinhamento no eixo cruzado (row = vertical) */
  align-items: center;
  /* stretch | flex-start | flex-end | center | baseline */

  /* Alinhamento das linhas quando há wrap (múltiplas linhas) */
  align-content: space-between;
  /* flex-start | flex-end | center | space-between | space-around | stretch */

  /* Shorthand: align-content + justify-content */
  place-content: center space-between;

  /* Shorthand: align-items + justify-items */
  place-items: center start;

  gap: 1rem;              /* row-gap + column-gap */
  gap: 1rem 2rem;         /* row-gap | column-gap */
  row-gap: 1rem;
  column-gap: 2rem;
}

/* ── Propriedades do Item ── */
.flex-item {
  /* flex: grow shrink basis */
  flex: 1;            /* flex: 1 1 0% — cresce e encolhe igualmente */
  flex: 0 0 auto;     /* tamanho fixo — não cresce nem encolhe */
  flex: 1 1 200px;    /* começa em 200px, cresce e encolhe */

  flex-grow: 2;        /* cresce 2x mais que itens com flex-grow: 1 */
  flex-shrink: 0;      /* não encolhe quando houver falta de espaço */
  flex-basis: 200px;   /* tamanho inicial (pode ser length ou porcentagem) */

  align-self: flex-start; /* sobrescreve align-items do container */
  /* auto | stretch | flex-start | flex-end | center | baseline */

  order: -1;           /* reposiciona visualmente sem alterar o HTML */
  /* padrão = 0; menor order aparece primeiro */

  min-width: 0;        /* permite shrink abaixo do conteúdo — fix comum */
}

/* ── Padrões comuns ── */

/* Centralizar completamente */
.centralizado {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

/* Sidebar fixa + conteúdo flexível */
.layout {
  display: flex;
  gap: 2rem;
}
.sidebar { flex: 0 0 240px; }   /* 240px fixo */
.conteudo { flex: 1 1 0; }      /* ocupa o resto */

/* Cards responsivos que quebram naturalmente */
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.card { flex: 1 1 280px; }   /* pelo menos 280px, cresce para preencher */

/* Sticky footer */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
main { flex: 1; }   /* main cresce e empurra o footer para baixo */

Grid completo

CSS Grid é o modelo de layout bidimensional — trabalha em linhas e colunas simultaneamente. Ideal para layouts de página inteira e componentes complexos.

/* ── Propriedades do Container ── */
.grid {
  display: grid;

  /* Colunas e linhas explícitas */
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 1fr auto;

  /* repeat() e fr */
  grid-template-columns: repeat(3, 1fr);
  grid-template-columns: repeat(12, 1fr); /* grid de 12 colunas clássico */

  /* auto-fill vs auto-fit */
  /* auto-fill: cria colunas vazias para preencher o espaço */
  /* auto-fit: colapsa colunas vazias — itens se expandem */
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));

  /* minmax(min, max) — coluna nunca menor que min, nunca maior que max */
  grid-template-columns: minmax(200px, 1fr) minmax(0, 3fr);

  /* auto — tamanho pelo conteúdo */
  grid-template-columns: auto 1fr auto;

  /* Areas nomeadas */
  grid-template-areas:
    "header  header  header"
    "sidebar main    main  "
    "footer  footer  footer";

  /* Shorthand completo: rows / columns */
  grid-template: auto 1fr auto / 240px 1fr;

  gap: 1rem 2rem;     /* row-gap column-gap */

  /* Alinhamento de itens dentro das células */
  justify-items: start;    /* stretch | start | end | center */
  align-items: center;     /* stretch | start | end | center */
  place-items: center;     /* align-items + justify-items */

  /* Alinhamento do grid inteiro dentro do container */
  justify-content: space-between;
  align-content: start;
  place-content: start space-between;

  /* Auto rows/columns para itens implícitos (overflow do grid explícito) */
  grid-auto-rows: minmax(100px, auto);
  grid-auto-columns: 200px;
  grid-auto-flow: dense; /* preenche buracos — útil para masonry-like */
}

/* ── Posicionamento de itens ── */
.header  {
  grid-area: header;              /* usa área nomeada */
  grid-column: 1 / -1;           /* da linha 1 até a última (-1) */
}

.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

.destaque {
  grid-column: 2 / 4;            /* da coluna 2 até antes da 4 */
  grid-row: 1 / 3;               /* da linha 1 até antes da 3 */
}

.span-2-cols { grid-column: span 2; }  /* ocupa 2 colunas a partir de onde está */

/* Posicionamento individual */
.item { justify-self: end; align-self: start; }

/* ── Subgrid — herda trilhas do pai ── */
/* (Chrome 117+, Firefox 71+, Safari 16+) */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-template-rows: auto;
}

.card {
  display: grid;
  grid-row: span 3;  /* ocupa 3 linhas implícitas no pai */
  /* subgrid herda as colunas do pai */
  grid-template-rows: subgrid;
}

/* ── Layout de página clássico ── */
.pagina {
  display: grid;
  grid-template:
    "header" auto
    "main  " 1fr
    "footer" auto
    / 1fr;
  min-height: 100vh;
}

@media (min-width: 768px) {
  .pagina {
    grid-template:
      "header  header " auto
      "sidebar main   " 1fr
      "footer  footer " auto
      / 240px   1fr;
  }
}

Positioning e stacking context

position controla como um elemento é posicionado no fluxo normal e como interage com outros elementos. z-index funciona dentro de contextos de empilhamento (stacking contexts), não globalmente.

.static   { position: static; }    /* padrão — segue o fluxo normal */
.relative { position: relative; }  /* desloca sem sair do fluxo — cria contexto */
.absolute { position: absolute; }  /* remove do fluxo — posiciona em relação ao pai mais próximo com position ≠ static */
.fixed    { position: fixed; }     /* remove do fluxo — relativo ao viewport */
.sticky   { position: sticky; }    /* híbrido: relativo até rolar para o offset, então fixo */

/* Uso típico: ícone absoluto dentro de container relativo */
.input-wrapper {
  position: relative;
}
.input-icon {
  position: absolute;
  top: 50%;
  right: 0.75rem;
  transform: translateY(-50%);
}

/* Sticky — fica fixo ao rolar */
.header-sticky {
  position: sticky;
  top: 0;           /* cola ao topo quando chegar nele */
  z-index: 100;
}

.sidebar-sticky {
  position: sticky;
  top: 1rem;
  align-self: start; /* necessário dentro de grid/flex para funcionar */
}

/*
  Stacking context — criado por:
  - position ≠ static com z-index ≠ auto
  - opacity < 1
  - transform, filter, clip-path, mask (qualquer valor)
  - will-change
  - isolation: isolate ← a forma limpa e explícita

  Dentro de um stacking context, z-index é relativo ao contexto, não à página.
*/
.modal-container {
  isolation: isolate;   /* cria stacking context sem efeitos visuais */
  position: relative;
}

.overlay { z-index: 1; }
.modal   { z-index: 2; }
.tooltip { z-index: 3; }

Typography

Tipografia no CSS vai muito além do font-size. Variable fonts permitem controlar eixos de variação em tempo real; font-display controla como fontes externas carregam sem causar flash de texto invisível.

/* @font-face — carregamento de fonte customizada */
@font-face {
  font-family: 'Inter';
  src:
    url('/fonts/inter-var.woff2') format('woff2') tech(variations),
    url('/fonts/inter.woff2') format('woff2');
  font-weight: 100 900;       /* range para variable font */
  font-style: normal;
  font-display: swap;         /* swap: mostra texto com fallback primeiro */
  unicode-range: U+0000-00FF, U+0131; /* carrega apenas para esses caracteres */
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 1rem;           /* base: 16px */
  line-height: 1.6;          /* relativo ao font-size — preferível */
  font-weight: 400;
  letter-spacing: -0.01em;   /* leve tracking negativo para fontes grandes */
  color: #0f172a;
}

/* Variable font — eixos customizados */
.titulo-variable {
  font-variation-settings:
    'wght' 700,      /* peso */
    'wdth' 75,       /* largura */
    'opsz' 32;       /* tamanho óptico */
}

/* Propriedades de texto */
.texto-overflow {
  white-space: nowrap;        /* não quebra linha */
  overflow: hidden;
  text-overflow: ellipsis;    /* trunca com "..." */

  /* Múltiplas linhas com clamp */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Hifenização automática */
.paragrafo {
  hyphens: auto;
  hyphenate-character: '‐';   /* ou auto */
  overflow-wrap: break-word;  /* quebra palavra muito longa */
  word-break: break-word;     /* compatibilidade */
}

/* font-variant — variantes tipográficas */
h1 {
  font-variant-numeric: oldstyle-nums; /* números com descendentes */
  font-variant-ligatures: common-ligatures; /* fi, fl, ff */
  font-variant: small-caps;
}

/* font-display valores:
   auto    — comportamento do browser
   block   — bloco de renderização (até 3s) → sem FOUT mas FOIT
   swap    — troca imediata (FOUT, não FOIT) — recomendado
   fallback — 100ms bloco, depois swap
   optional — carrega se imediato, senão usa fallback */

Funções modernas

As funções matemáticas e de comparação do CSS moderno substituem muitas media queries e cálculos JavaScript para responsividade.

/* clamp(min, ideal, max) — valor fluido que nunca sai do intervalo */
h1 { font-size: clamp(1.5rem, 4vw + 1rem, 3.5rem); }
  /* mínimo: 1.5rem | ideal: responde ao viewport | máximo: 3.5rem */

.container { width: clamp(300px, 90%, 1200px); }

/* min() — usa o menor dos valores */
.box { width: min(100%, 600px); }  /* substitui max-width: 600px */
.foto { height: min(50vh, 400px); }

/* max() — usa o maior dos valores */
.padding-responsivo { padding: max(1rem, 4vw); } /* nunca menor que 1rem */
.footer { min-height: max(200px, 20vh); }

/* calc() — operações aritméticas */
.sidebar { width: calc(100% - 320px - 2rem); }
.fullscreen-minus-header { height: calc(100svh - 4rem); }
.col-4-of-12 { width: calc(4/12 * 100%); }

/* env() — variáveis de ambiente do browser */
/* Usado para safe-areas em iPhones com notch */
.header {
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left, 1rem); /* fallback */
}

/* Funções trigonométricas (CSS Nivel 4) */
.dial {
  --angle: 45deg;
  transform: translate(
    calc(sin(var(--angle)) * 100px),
    calc(cos(var(--angle)) * -100px)
  );
}

Cores modernas

O CSS ganhou espaços de cor mais expressivos. oklch é o mais recomendado para UIs porque é perceptualmente uniforme — brilho e saturação são consistentes entre cores diferentes.

/* Espaços de cor */
.elemento {
  /* sRGB — histórico, gamut limitado */
  color: rgb(56 189 248);
  color: rgb(56 189 248 / 0.5);    /* com transparência */

  /* HSL — intuitivo mas não perceptualmente uniforme */
  color: hsl(200 90% 60%);
  color: hsl(200 90% 60% / 0.8);

  /* oklch — perceptualmente uniforme, gamut P3 (melhor opção moderna) */
  /* oklch(Lightness Chroma Hue / Alpha) */
  color: oklch(70% 0.2 230);
  color: oklch(70% 0.2 230 / 0.9);

  /* display-p3 — gamut mais amplo em monitores modernos */
  color: color(display-p3 0.2 0.7 0.9);
}

/* color-mix() — mistura duas cores */
.btn-hover {
  background: color-mix(in oklch, var(--color-accent) 80%, white);
}
.shadow-color {
  --shadow: color-mix(in srgb, var(--color-accent), transparent 70%);
}

/* Sintaxe de cor relativa (CSS Color Level 5) */
/* Modifica uma cor existente sem saber os valores internos */
:root { --accent: oklch(60% 0.2 230); }

.accent-lighter {
  color: oklch(from var(--accent) calc(l + 0.2) c h);
}
.accent-muted {
  color: oklch(from var(--accent) l calc(c * 0.5) h);
}

/* color-scheme — diz ao browser qual esquema de cor usar em elementos nativos */
:root {
  color-scheme: light dark; /* suporta ambos */
}

/* Gamut query */
@media (color-gamut: p3) {
  :root { --color-accent: oklch(70% 0.25 230); } /* mais vibrante em P3 */
}

Backgrounds, gradientes e mask

/* Gradientes */
.gradiente-linear {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background: linear-gradient(to right in oklch, oklch(60% 0.2 250), oklch(70% 0.25 30));
}

.gradiente-radial {
  background: radial-gradient(circle at center, #38bdf8, #0f172a);
  background: radial-gradient(ellipse 60% 40% at 50% 50%, #fff 0%, transparent 70%);
}

.gradiente-conico {
  background: conic-gradient(from 0deg, red, yellow, green, blue, red);
  background: conic-gradient(from 180deg at 50% 50%, #e3f0ff, #b3d0ff);
}

/* Múltiplos backgrounds — pilhados (primeiro na lista = mais na frente) */
.hero {
  background:
    linear-gradient(rgb(0 0 0 / 0.4), rgb(0 0 0 / 0.4)),
    url('/images/hero.jpg') center / cover no-repeat;
}

/* background-clip — delimita onde o background é pintado */
.gradient-text {
  background: linear-gradient(90deg, #38bdf8, #818cf8);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.border-gradient {
  background-clip: padding-box; /* background não vaza para a border */
}

/* mask — recorta o elemento com uma máscara */
.mask-fade {
  mask-image: linear-gradient(to bottom, black 60%, transparent);
  mask-size: 100% 100%;
}

.mask-shape {
  mask-image: url('/assets/shape.svg');
  mask-size: cover;
  mask-repeat: no-repeat;
  mask-position: center;
}

/* border-image */
.borda-gradiente {
  border: 3px solid transparent;
  border-image: linear-gradient(90deg, #38bdf8, #818cf8) 1;
}

Shadows, Transforms e Filters

/* box-shadow — múltiplas sombras */
.card {
  box-shadow:
    0 1px 3px rgb(0 0 0 / 0.12),
    0 1px 2px rgb(0 0 0 / 0.24),
    0 0 0 1px rgb(0 0 0 / 0.05); /* terceira: outline sutil */
}

.card:hover {
  box-shadow:
    0 14px 28px rgb(0 0 0 / 0.25),
    0 10px 10px rgb(0 0 0 / 0.22);
}

/* text-shadow */
.titulo-shadow {
  text-shadow: 2px 2px 4px rgb(0 0 0 / 0.3);
}

/* Transforms 2D e 3D */
.card {
  transform: translateX(20px) translateY(-10px);
  transform: rotate(45deg);
  transform: scale(1.05);
  transform: skewX(10deg);
  transform: matrix(1, 0, 0, 1, 50, 100); /* a, b, c, d, tx, ty */

  /* 3D */
  transform: perspective(800px) rotateX(20deg) rotateY(-10deg);
  transform: rotate3d(1, 1, 0, 45deg);
  transform: translateZ(50px);

  transform-origin: center center;      /* ponto de origem */
  transform-origin: top left;
  transform-origin: 20% 40%;

  /* 3D context */
  perspective: 800px;
  perspective-origin: 50% 50%;
  transform-style: preserve-3d;         /* filhos em 3D */
  backface-visibility: hidden;          /* oculta face traseira */
}

/* Filters */
.foto-hover {
  filter: brightness(1);
  transition: filter 300ms ease;
}
.foto-hover:hover {
  filter: brightness(1.1) saturate(1.2) contrast(1.05);
}

.blur-element    { filter: blur(4px); }
.grayscale       { filter: grayscale(100%); }
.hue-rotation    { filter: hue-rotate(180deg); }
.drop-shadow-png { filter: drop-shadow(0 4px 8px rgb(0 0 0 / 0.4)); }
  /* drop-shadow segue o contorno real (canal alpha), diferente de box-shadow */

/* backdrop-filter — aplica filtro ao conteúdo atrás do elemento */
.glass-card {
  background: rgb(255 255 255 / 0.1);
  backdrop-filter: blur(12px) saturate(180%);
  border: 1px solid rgb(255 255 255 / 0.2);
}

/* Blend modes */
.foto-overlay {
  mix-blend-mode: multiply;   /* como camadas no Photoshop */
}

.bg-blend {
  background-blend-mode: screen;
}

Transitions e Animations

transition anima a mudança de um estado para outro. animation com @keyframes cria sequências autônomas mais complexas.

/* Transitions — especifique propriedades individualmente */
.btn {
  background: #0f172a;
  transform: translateY(0) scale(1);
  box-shadow: none;
  transition:
    background-color 150ms ease,
    transform 200ms cubic-bezier(0.34, 1.56, 0.64, 1), /* spring effect */
    box-shadow 200ms ease;
  /* EVITE: transition: all — anima propriedades não intencionais */
}

.btn:hover {
  background: #1e293b;
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
}

/* Timing functions */
.ease         { transition-timing-function: ease; }          /* padrão */
.ease-in      { transition-timing-function: ease-in; }
.ease-out     { transition-timing-function: ease-out; }
.ease-in-out  { transition-timing-function: ease-in-out; }
.linear       { transition-timing-function: linear; }
.spring       { transition-timing-function: cubic-bezier(0.34, 1.56, 0.64, 1); }
.steps        { transition-timing-function: steps(4, end); }   /* discreto */

/* will-change — avisa o browser para criar camada GPU */
.animating {
  will-change: transform, opacity;   /* use com parcimônia — consome memória */
}
/* Remover após animação para liberar memória: */
element.addEventListener('transitionend', () => element.style.willChange = 'auto');

/* @keyframes */
@keyframes fade-in-up {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.05); }
}

@keyframes shimmer {
  from { background-position: -100% 0; }
  to   { background-position: 100% 0; }
}

/* animation shorthand */
.card-enter {
  animation: fade-in-up 400ms ease-out forwards;
  /* name duration timing-function delay iteration-count direction fill-mode */
}

.skeleton {
  background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite linear;
}

/* fill-mode: none | forwards | backwards | both */
/* forwards  — mantém estado final após terminar */
/* backwards — aplica estado inicial antes de começar (com delay) */
/* both      — ambos */

/* play-state — pausar/retomar */
.paused { animation-play-state: paused; }

/* Scroll-driven animations (Chrome 115+, Firefox 110+) */
@keyframes revelar {
  from { opacity: 0; transform: translateX(-40px); }
  to   { opacity: 1; transform: translateX(0); }
}

.elemento-scroll {
  animation: revelar linear both;
  animation-timeline: view();           /* progresso baseado na visibilidade */
  animation-range: entry 0% entry 40%; /* anima durante a entrada na viewport */
}

/* scroll() — progresso baseado no scroll de um container */
.progress-bar {
  animation: crescer linear;
  animation-timeline: scroll(root);    /* scroll da página inteira */
  transform-origin: left;
}

@keyframes crescer {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

/* prefers-reduced-motion — acessibilidade obrigatória */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

Media Queries e Container Queries

Media queries respondem ao viewport. Container queries respondem ao tamanho do elemento-pai — muito mais úteis para componentes reutilizáveis que aparecem em contextos diferentes.

/* ── Media Queries ── */

/* Breakpoints por viewport */
@media (max-width: 639px)  { /* mobile */ }
@media (min-width: 640px)  { /* sm */ }
@media (min-width: 768px)  { /* md */ }
@media (min-width: 1024px) { /* lg */ }
@media (min-width: 1280px) { /* xl */ }
@media (min-width: 1536px) { /* 2xl */ }

/* Orientação */
@media (orientation: landscape) { }
@media (orientation: portrait)  { }

/* Preferências do usuário */
@media (prefers-color-scheme: dark)    { :root { --bg: #0f172a; } }
@media (prefers-color-scheme: light)   { :root { --bg: #ffffff; } }
@media (prefers-reduced-motion: reduce) { }
@media (prefers-contrast: more)         { }
@media (prefers-contrast: less)         { }

/* Capacidades do dispositivo */
@media (hover: hover) and (pointer: fine) {
  /* Mouse ou trackpad de precisão */
  .btn:hover { background: var(--hover-bg); }
}
@media (hover: none) and (pointer: coarse) {
  /* Touch device — aumentar áreas de toque */
  .btn { min-height: 44px; }
}

/* Display mode (PWA) */
@media (display-mode: standalone) {
  .install-banner { display: none; }
}

/* Combinação com and, or (,), not */
@media (min-width: 768px) and (max-width: 1023px) { }
@media (min-width: 768px), print { } /* virgula = OR */
@media not (prefers-color-scheme: dark) { }

/* ── Container Queries ── */

/* Passo 1: Definir o container */
.card-wrapper {
  container-type: inline-size;  /* responde à largura inline */
  container-name: card;         /* nome opcional — permite selecionar específicos */
}

/* Passo 2: Consultar o container */
@container card (min-width: 400px) {
  .card {
    flex-direction: row;
    gap: 1.5rem;
  }
  .card-imagem { width: 40%; }
}

@container (min-width: 600px) { /* sem nome — qualquer container com inline-size */
  .titulo { font-size: 1.5rem; }
}

/* Unidades de container */
.card-titulo {
  font-size: clamp(1rem, 5cqw, 2rem); /* cqw = container query width */
  /* cqh, cqi (inline), cqb (block), cqmin, cqmax */
}

Scroll behavior e Logical Properties

/* ── Scroll snap ── */
.carrossel {
  display: flex;
  overflow-x: scroll;
  scroll-snap-type: x mandatory;    /* x|y|block|inline + mandatory|proximity */
  scroll-padding: 1rem;             /* offset do snap point */
  overscroll-behavior-x: contain;  /* evita scroll chain (rolar além) */
  scrollbar-width: thin;            /* thin | none | auto */
  scrollbar-color: #94a3b8 transparent; /* thumb track */
}

.carrossel-item {
  flex: 0 0 calc(100% - 2rem);
  scroll-snap-align: start;        /* start | center | end */
  scroll-snap-stop: always;        /* não pula itens rápido */
}

/* Scroll suave nativo */
html { scroll-behavior: smooth; }

/* scroll-padding — compensa header fixo ao usar âncoras */
html { scroll-padding-top: 4rem; }

/* ── Logical Properties ── */
/* Usam eixos lógicos (inline/block) em vez de físicos (horizontal/vertical)
   Essenciais para suporte a RTL e textos verticais */

.elemento {
  /* Equivalentes físicos → lógicos */
  margin-inline: auto;          /* margin-left + margin-right: auto */
  margin-block: 1rem;           /* margin-top + margin-bottom */
  padding-inline-start: 1.5rem; /* padding-left em LTR, padding-right em RTL */
  padding-block-end: 2rem;      /* padding-bottom em escrita horizontal */
  border-block-end: 1px solid;  /* border-bottom */
  inset-inline-start: 0;        /* left em LTR */
  inset: 0;                     /* top right bottom left: 0 */

  inline-size: 100%;            /* width em escrita horizontal */
  block-size: auto;             /* height */
  max-inline-size: 1200px;      /* max-width */
}

Aspect Ratio, Object Fit e CSS Nesting

/* aspect-ratio */
.video-wrapper {
  aspect-ratio: 16 / 9;
  width: 100%;
}

.avatar {
  width: 64px;
  aspect-ratio: 1;        /* quadrado */
  border-radius: 50%;
}

.card-imagem {
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

/* object-fit — como a mídia preenche o container */
img, video {
  width: 100%;
  height: 100%;
  object-fit: cover;          /* preenche sem distorcer, corta se necessário */
  object-fit: contain;        /* cabe no container sem cortar */
  object-fit: fill;           /* distorce para preencher */
  object-fit: none;           /* tamanho original */
  object-position: center top; /* posição dentro do container */
}

/* ── CSS Nesting Nativo (Chrome 120+, Firefox 117+, Safari 17.2+) ── */
.card {
  background: white;
  border-radius: 0.5rem;

  /* Nesting direto — o & representa o elemento pai */
  &:hover {
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
  }

  & .titulo {
    font-size: 1.25rem;
    font-weight: 600;
  }

  & > p {
    margin-block: 0.5rem;
    line-height: 1.6;
  }

  /* Nested media query */
  @media (min-width: 768px) {
    display: flex;
    gap: 1rem;
  }

  /* Nested container query */
  @container (min-width: 400px) {
    flex-direction: row;
  }
}

@scope

@scope limita o alcance de seletores a uma subárvore do DOM, evitando conflitos sem aumentar especificidade.

/* Escopo de componente — estilos só afetam dentro de .card */
@scope (.card) {
  /* Sem precisar de .card p, .card h2, etc. */
  p { color: #334155; }
  h2 { font-size: 1.25rem; }
  a { color: var(--color-accent); }
}

/* Escopo com buraco — exclui elementos dentro de .conteudo-externo */
@scope (.componente) to (.conteudo-externo) {
  p { font-size: 0.875rem; }
}

Versões — CSS2.1 ao presente

Versão / EraAnoPrincipais novidades
CSS 2.12004/2011Seletores básicos, box model, posicionamento (float, position), pseudo-classes e pseudo-elementos simples, @media rudimentar
CSS3 (início dos módulos)2011–2012border-radius, box-shadow, text-shadow, rgba/hsla, gradientes, transition, animation/@keyframes, transform, @font-face, opacity
Flexbox2012Layout unidimensional sem floats — display: flex, justify-content, align-items, gap
Grid2017Layout bidimensional nativo — display: grid, grid-template, fr, grid-area, minmax()
Custom Properties2017Variáveis CSS nativas em cascata — --variavel, var()
2019–2020aspect-ratio, clamp()/min()/max(), gap em Flexbox, color-scheme, prefers-color-scheme
2021–2022@layer, Container Queries (:container), :has(), color-mix(), oklch/oklch, @property, accent-color, overscroll-behavior, Logical Properties ampliadas
2023–2024CSS Nesting nativo, @scope, Scroll-driven Animations (animation-timeline), Subgrid (amplo suporte), Relative Color Syntax, :is()::where() amplos, color() com gamuts P3 e Display-P3