# HTML & CSS Quick Reference

*Elements, selectors, flexbox, grid, responsive*

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

## HTML Structure

```
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0">
  <title>Page Title</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Hello World</h1>
  <script src="app.js"></script>
</body>
</html>
```

## Common Elements

### Text & Headings

```
<h1>Heading 1</h1> ... <h6>Heading 6</h6>
<p>Paragraph text</p>
<strong>Bold</strong>  <em>Italic</em>
<br>  <!-- line break -->
<hr>  <!-- horizontal rule -->
```

### Links & Images

```
<a href="https://example.com">Link text</a>
<a href="#section">Anchor link</a>
<img src="photo.jpg" alt="Description">
```

### Lists

```
<ul>              <!-- unordered -->
  <li>Item</li>
</ul>
<ol>              <!-- ordered -->
  <li>First</li>
</ol>
```

## Forms

### Input Types

```
<form action="/submit" method="POST">
  <input type="text" name="user"
    placeholder="Name" required>
  <input type="email" name="email">
  <input type="password" name="pw">
  <input type="number" min="0" max="100">
  <input type="checkbox" name="agree">
  <input type="radio" name="size" value="M">
  <button type="submit">Send</button>
</form>
```

### Select & Textarea

```
<select name="color">
  <option value="r">Red</option>
  <option value="g" selected>Green</option>
</select>
<textarea name="msg" rows="4"
  cols="30"></textarea>
```

## Semantic HTML

| Command | Description |
|---------|-------------|
| `<header>` | Intro content or nav container |
| `<nav>` | Navigation links |
| `<main>` | Primary page content (one per page) |
| `<section>` | Thematic grouping of content |
| `<article>` | Self-contained content |
| `<aside>` | Sidebar or tangential content |
| `<footer>` | Footer for section or page |
| `<figure>` | Image/diagram with caption |
| `<figcaption>` | Caption for <figure> |

## CSS Selectors

### Basic Selectors

| Command | Description |
|---------|-------------|
| `element` | `p { }` — all <p> elements |
| `.class` | `.card { }` — class selector |
| `#id` | `#main { }` — id selector |
| `*` | Universal selector (all elements) |

### Combinators

| Command | Description |
|---------|-------------|
| `A B` | Descendant (B inside A) |
| `A > B` | Direct child only |
| `A + B` | Adjacent sibling (B right after A) |
| `A ~ B` | General sibling (B after A) |

### Pseudo-classes

| Command | Description |
|---------|-------------|
| `:hover` | Mouse over element |
| `:focus` | Element has focus |
| `:first-child` | First child of parent |
| `:nth-child(n)` | nth child (2n = even, odd) |

## Box Model

```
.box {
  margin: 10px;           /* outside */
  border: 1px solid #333; /* edge */
  padding: 15px;          /* inside */
  width: 200px;
  box-sizing: border-box; /* include pad+border */
}
```

### Shorthand

| Command | Description |
|---------|-------------|
| `margin: 10px` | All sides |
| `margin: 10px 20px` | Top/bottom \| left/right |
| `margin: 10px 20px 15px` | Top \| left/right \| bottom |
| `margin: 10px 20px 15px 5px` | Top \| right \| bottom \| left |

## Flexbox

### Container

```
.flex-container {
  display: flex;
  flex-direction: row;     /* row | column */
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  gap: 10px;
  flex-wrap: wrap;
}
```

### Item Properties

| Command | Description |
|---------|-------------|
| `flex: 1` | Grow to fill available space |
| `flex-grow: 2` | Grow ratio relative to siblings |
| `flex-shrink: 0` | Don't shrink below basis |
| `flex-basis: 200px` | Initial size before grow/shrink |
| `align-self: flex-end` | Override container alignment |
| `order: -1` | Visual order (default 0) |

## Grid

### Container

```
.grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto;
  gap: 10px;
}
```

### Item Placement

```
.item {
  grid-column: 1 / 3; /* span col 1-2 */
  grid-row: 1 / 2;
}
/* shorthand: repeat, minmax */
grid-template-columns: repeat(3, 1fr);
grid-template-columns:
  repeat(auto-fit, minmax(200px, 1fr));
```

## Positioning

| Command | Description |
|---------|-------------|
| `static` | Default flow (no offset properties) |
| `relative` | Offset from normal position |
| `absolute` | Offset from nearest positioned ancestor |
| `fixed` | Offset from viewport (stays on scroll) |
| `sticky` | Relative until scroll threshold, then fixed |

### Example

```
.parent { position: relative; }
.child {
  position: absolute;
  top: 0; right: 0;  /* top-right corner */
}
```

## Media Queries

```
/* Mobile-first: base styles for small screens */
.container { padding: 10px; }

@media (min-width: 768px) {
  .container { max-width: 720px; }
}
@media (min-width: 1024px) {
  .container { max-width: 960px; }
}
```

### Common Breakpoints

| Command | Description |
|---------|-------------|
| `480px` | Small phones |
| `768px` | Tablets |
| `1024px` | Small desktops / landscape tablets |
| `1280px` | Desktops |
