# Referência Rápida de CSS

*Seletores, layout, flexbox, grid, animação, design responsivo*

> Source: MDN Web Docs (developer.mozilla.org) · MIT

## Seletores

### Seletores Básicos

| Command | Description |
|---------|-------------|
| `*` | Universal — todos os elementos |
| `div` | Tipo — todos os elementos `<div>` |
| `.class` | Classe — elementos com a classe |
| `#id` | ID — elemento com o id |
| `[attr]` | Atributo — tem o atributo |
| `[attr="val"]` | Atributo igual ao valor |

### Combinadores

| Command | Description |
|---------|-------------|
| `A B` | Descendente (qualquer profundidade) |
| `A > B` | Filho direto apenas |
| `A + B` | Irmão adjacente (próximo) |
| `A ~ B` | Irmão geral (todos após) |

### Pseudo-classes

| Command | Description |
|---------|-------------|
| `:hover` | Mouse sobre o elemento |
| `:focus` | Elemento com foco |
| `:first-child` | Primeiro filho do pai |
| `:nth-child(n)` | Nº filho (base 1, `odd`, `even`, `2n+1`) |
| `:not(sel)` | Negação — excluir matches |
| `:has(sel)` | Seletor pai (contém match) |

### Pseudo-elementos

| Command | Description |
|---------|-------------|
| `::before` | Inserir conteúdo antes do elemento |
| `::after` | Inserir conteúdo após o elemento |
| `::placeholder` | Estilizar texto placeholder de input |
| `::selection` | Estilizar texto selecionado |

## Box Model

### Box Sizing

```
/* Include padding/border in width */
*, *::before, *::after {
  box-sizing: border-box;
}
```

### Propriedades

| Command | Description |
|---------|-------------|
| `margin` | Espaço fora da borda |
| `border` | Borda ao redor do padding |
| `padding` | Espaço dentro da borda |
| `width / height` | Dimensões do conteúdo |
| `outline` | Anel fora da margem (sem espaço) |

### Shorthand

```
margin: 10px;           /* all sides */
margin: 10px 20px;      /* vertical | horizontal */
margin: 10px 20px 30px; /* top | horiz | bottom */
margin: 10px 20px 30px 40px; /* T R B L */
```

## Flexbox

### Container

```
.flex {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  gap: 1rem;
}
```

### Propriedades do Container

| Command | Description |
|---------|-------------|
| `flex-direction` | `row` \| `column` \| `row-reverse` \| `column-reverse` |
| `flex-wrap` | `nowrap` \| `wrap` \| `wrap-reverse` |
| `justify-content` | `flex-start` \| `center` \| `space-between` \| `space-around` \| `space-evenly` |
| `align-items` | `stretch` \| `center` \| `flex-start` \| `flex-end` \| `baseline` |
| `align-content` | Alinhamento no eixo cruzado multi-linha |
| `gap` | Espaço entre itens flex |

### Propriedades do Item

| Command | Description |
|---------|-------------|
| `flex: 1` | Crescer para preencher o espaço disponível |
| `flex: 0 0 200px` | Largura fixa, sem crescer/encolher |
| `align-self` | Sobrescrever align-items para um item |
| `order` | Alterar ordem visual (padrão 0) |

## Grid

### Container

```
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
}
```

### Propriedades do Container

| Command | Description |
|---------|-------------|
| `grid-template-columns` | Definir colunas: `1fr 2fr`, `repeat(3, 1fr)` |
| `grid-template-rows` | Definir linhas |
| `grid-template-areas` | Áreas nomeadas: `"header header" "nav main"` |
| `gap` | Espaços entre linhas e colunas |
| `justify-items` | Alinhar itens inline (horizontal) |
| `align-items` | Alinhar itens block (vertical) |

### Posicionamento de Item

```
.item {
  grid-column: 1 / 3;   /* span cols 1-2 */
  grid-row: 1 / -1;     /* span all rows */
  grid-area: header;    /* named area */
}
```

## Tipografia

### Propriedades de Fonte

| Command | Description |
|---------|-------------|
| `font-family` | Stack de fontes: `'Inter', sans-serif` |
| `font-size` | Tamanho: `1rem`, `16px`, `clamp(1rem, 2vw, 2rem)` |
| `font-weight` | `normal` (400) \| `bold` (700) \| `100`-`900` |
| `font-style` | `normal` \| `italic` \| `oblique` |
| `line-height` | Espaçamento de linha: `1.5` (sem unidade recomendado) |
| `letter-spacing` | Espaçamento de caracteres: `0.05em` |

### Propriedades de Texto

| Command | Description |
|---------|-------------|
| `text-align` | `left` \| `center` \| `right` \| `justify` |
| `text-decoration` | `none` \| `underline` \| `line-through` |
| `text-transform` | `uppercase` \| `lowercase` \| `capitalize` |
| `text-overflow` | `ellipsis` (com overflow: hidden) |
| `white-space` | `nowrap` \| `pre` \| `pre-wrap` |
| `word-break` | `break-all` \| `break-word` |

## Cores & Fundos

### Formatos de Cor

```
color: #ff6600;             /* hex */
color: rgb(255, 102, 0);    /* rgb */
color: hsl(24, 100%, 50%);  /* hsl */
color: oklch(70% 0.15 50);  /* oklch */
```

### Propriedades de Background

| Command | Description |
|---------|-------------|
| `background-color` | Preenchimento sólido: `#f0f0f0` |
| `background-image` | `url(img.jpg)` ou gradiente |
| `background-size` | `cover` \| `contain` \| `100px 200px` |
| `background-position` | `center` \| `top right` \| `50% 50%` |
| `background-repeat` | `no-repeat` \| `repeat-x` \| `repeat-y` |

### Gradientes

```
background: linear-gradient(to right, #f00, #00f);
background: radial-gradient(circle, #fff, #000);
background: conic-gradient(red, yellow, green);
```

## Transições & Animação

### Transições

```
.btn {
  transition: background 0.3s ease, transform 0.2s;
}
.btn:hover {
  background: #0056b3;
  transform: scale(1.05);
}
```

### Propriedades de Transição

| Command | Description |
|---------|-------------|
| `transition-property` | Qual propriedade animar |
| `transition-duration` | Duração: `0.3s`, `300ms` |
| `transition-timing-function` | `ease` \| `linear` \| `ease-in-out` \| `cubic-bezier()` |
| `transition-delay` | Aguardar antes de iniciar |

### Animação com Keyframes

```
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
.icon { animation: spin 1s linear infinite; }
```

### Propriedades de Animação

| Command | Description |
|---------|-------------|
| `animation-name` | Referência ao nome do @keyframes |
| `animation-duration` | Duração de um ciclo |
| `animation-iteration-count` | `1` \| `3` \| `infinite` |
| `animation-direction` | `normal` \| `alternate` \| `reverse` |
| `animation-fill-mode` | `forwards` \| `backwards` \| `both` |

## Design Responsivo

### Media Queries

```
@media (max-width: 768px) {
  .sidebar { display: none; }
}
@media (prefers-color-scheme: dark) {
  body { background: #1a1a1a; color: #eee; }
}
```

### Breakpoints Comuns

| Command | Description |
|---------|-------------|
| `max-width: 480px` | Celulares |
| `max-width: 768px` | Tablets |
| `max-width: 1024px` | Notebooks pequenos |
| `max-width: 1280px` | Desktops |

### Unidades de Viewport

| Command | Description |
|---------|-------------|
| `vw / vh` | % da largura / altura da viewport |
| `dvh` | Altura de viewport dinâmica (safe para mobile) |
| `svh / lvh` | Altura de viewport pequena / grande |
| `cqi` | Tamanho inline de container query |

### Container Queries

```
.card-wrapper { container-type: inline-size; }
@container (min-width: 400px) {
  .card { flex-direction: row; }
}
```

## Posicionamento

### Valores de Position

| Command | Description |
|---------|-------------|
| `static` | Padrão — fluxo normal do documento |
| `relative` | Deslocado da posição normal; mantém espaço |
| `absolute` | Posicionado ao ancestral posicionado mais próximo |
| `fixed` | Posicionado à viewport; permanece no scroll |
| `sticky` | Alterna relative/fixed baseado no scroll |

### Padrões de Centralização

```
/* Flex center */
display: flex;
justify-content: center;
align-items: center;

/* Grid center */
display: grid;
place-items: center;
```

### Empilhamento

| Command | Description |
|---------|-------------|
| `z-index` | Ordem de empilhamento (maior = na frente); exige `position` |
| `isolation: isolate` | Cria novo contexto de empilhamento |

## Propriedades Customizadas

### Definir & Usar

```
:root {
  --color-primary: #3b82f6;
  --spacing-md: 1rem;
}
.btn {
  background: var(--color-primary);
  padding: var(--spacing-md);
}
```

### Valores de Fallback

```
color: var(--accent, #ff6600);
/* uses #ff6600 if --accent is not defined */
```

### Temas Dinâmicos

```
[data-theme="dark"] {
  --bg: #1a1a2e;
  --text: #e0e0e0;
}
body { background: var(--bg); color: var(--text); }
```
