# Style a recipe

> Style the React parts with className, style, and the data attributes they render, support dark mode and print, and find every attribute's values.

Source: https://recipe-book.siraj.page/guides/style-a-recipe/

Style the React parts with `className`, `style`, and the data attributes they render. The parts ship no CSS and render plain HTML: each one that renders an element gives it a `data-recipe-*` attribute that names the part, and some parts add attributes for their state, such as `data-state` on the measurement system options. The Vite example's `src/styles.css`, which its `src/main.tsx` imports, narrows the servings input, makes it and the measurement system options at least 24 pixels tall so they meet the minimum target size in WCAG 2.2, and bolds the checked measurement system:

src/styles.css
```css title="src/styles.css"
[data-recipe-servings-input] {
  width: 4rem;
}

[data-recipe-servings-input],
[data-recipe-system-option] {
  min-height: 24px;
}

[data-recipe-system-option][data-state='checked'] {
  font-weight: bold;
}
```

## How the parts render

With the Tailwind CSS that `create-next-app` sets up, buttons and inputs have no borders and lists have no markers, so the toggle looks like plain text, and so does the servings input, apart from the up and down arrows that Firefox always shows and Chrome shows on hover or focus. [`<Recipe.Root>`](https://recipe-book.siraj.page/reference/react/root.md) renders no element of its own, only its children. For the [sample recipe](https://recipe-book.siraj.page/guides/render-a-recipe-in-react.md#parse-on-the-server), a [`<Recipe.ServingsInput>`](https://recipe-book.siraj.page/reference/react/servings.md#servingsinput) and a [`<Recipe.SystemToggle>`](https://recipe-book.siraj.page/reference/react/system-toggle.md#systemtoggle) inside it render this HTML, indented here for reading:

```html
<input autoComplete="off" type="number" inputMode="numeric" aria-label="Servings" min="1" data-recipe-servings-input="" value="4"/>
<div aria-label="Measurement system" role="radiogroup" data-recipe-system-toggle="">
  <button autoComplete="off" type="button" role="radio" aria-checked="false" tabindex="-1" data-recipe-system-option="" data-state="unchecked">Metric</button>
  <button autoComplete="off" type="button" role="radio" aria-checked="true" tabindex="0" data-recipe-system-option="" data-state="checked">US</button>
</div>
```

Recipe Book renders two inline styles. One is on the `to` label that follows the dash of a quantity range, as in "2–3 tbsp", which it hides on screen. See [`<Recipe.IngredientQuantity>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientquantity). The other, `white-space: nowrap`, is on a `<span>` around each range that a part writes with no spaces around its dash, such as "2–3", "¼–½", or "180 °C–200 °C" in a step, or a range in an ingredient's amount or a servings or yield count, so a line never ends at that range's dash. That holds for a range with a hyphen, a tilde, or a dash from U+2010 to U+2015, which include the en and em dashes. A range inside one duration, such as "25–30 minutes", gets a word joiner (U+2060) after its dash instead, so the duration can still wrap at its space. With an em dash, as in "25—30 minutes", the line can still break before the dash. To keep a whole duration on one line, add `[data-recipe-duration] { white-space: nowrap }` to your own CSS. A range with spaces around its dash, such as "2 – 3", a range in a step with U+2E3A, U+FE63 or U+FF0D, a doubled dash before a duration, as in "1--2 minutes", a dash between two durations, as in "45 minutes–1 hour", and a range in a step's name or an ingredient's note can still wrap at the dash. So can a range in the text that [`<Recipe.ServingsText>`](https://recipe-book.siraj.page/reference/react/servings.md#servingstext) or [`<Recipe.YieldText>`](https://recipe-book.siraj.page/reference/react/servings.md#yieldtext) gives an `asChild` child, which gets a plain string with no `<span>`.

## Pass `className` and `style`

Every part that renders an element passes `className`, `style`, and its other props to that element. Give a part a class to style it with custom CSS in your stylesheet, or a style object for a one-off value:

```tsx {7-8}
import type { ParsedRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'

export function RecipeControls({ recipe }: { recipe: ParsedRecipe }) {
  return (
    <Recipe.Root recipe={recipe}>
      <Recipe.ServingsInput className="servings" style={{ width: '4rem' }} />
      <Recipe.SystemToggle className="units" />
    </Recipe.Root>
  )
}
```

For the sample recipe, it renders:

```html {2}
<input autoComplete="off" class="servings" style="width:4rem" type="number" inputMode="numeric" aria-label="Servings" min="1" data-recipe-servings-input="" value="4"/>
<div class="units" aria-label="Measurement system" role="radiogroup" data-recipe-system-toggle="">
  <button autoComplete="off" type="button" role="radio" aria-checked="false" tabindex="-1" data-recipe-system-option="" data-state="unchecked">Metric</button>
  <button autoComplete="off" type="button" role="radio" aria-checked="true" tabindex="0" data-recipe-system-option="" data-state="checked">US</button>
</div>
```

The class goes only on the part's element, so `units` is on the radio group, not on its options. To style the options, select them inside the group, as in `.units [data-recipe-system-option]`, or render them yourself with [`<Recipe.SystemOption>`](https://recipe-book.siraj.page/reference/react/system-toggle.md#systemoption) and pass each one a `className`.

With `asChild`, a part renders your element instead of its own and passes its props to it. [Render your own elements](https://recipe-book.siraj.page/reference/react/system-toggle.md#render-your-own-elements) shows `asChild` on the toggle.

## Style with data attributes

Select a part by its `data-recipe-*` attribute, as in `[data-recipe-step]`, without passing a class. The Next.js example's `app/globals.css` keeps the recipe's image inside the page:

```css
[data-recipe-image] {
  max-width: 100%;
  height: auto;
}
```

Section titles render as headings with no data attribute of their own: `<h3>` by default, or the level that you pass as `headingLevel` to [`<Recipe.Ingredients>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredients) or [`<Recipe.Steps>`](https://recipe-book.siraj.page/reference/react/steps.md#steps). Select them inside their section:

```css
[data-recipe-ingredient-section] > h3,
[data-recipe-step-section] > h3 {
  font-size: 1rem;
}
```

### Style states

`data-state` and `data-origin` have values. The other state attributes are empty, so select them by name:

```css
[data-recipe-system-toggle][data-disabled] {
  opacity: 0.5;
}

[data-recipe-ingredient][data-origin='converted'] [data-recipe-ingredient-quantity] {
  font-style: italic;
}

[data-recipe-ingredient][data-unquantified] {
  color: gray;
}
```

With metric checked, the sample recipe shows 296 mL buttermilk, converted from 1 ¼ cups, so its quantity is in italics. Its 190 g all-purpose flour comes from the author's equivalent, so it isn't. Its maple syrup, to serve, has no quantity, so it's gray.

## Support dark mode

A dark theme needs two things: dark values for the colors that you give the parts, and a dark color scheme, without which the browser keeps drawing the servings input, the servings select, and the toggle's buttons in light colors. Keep the colors in CSS variables, and switch them and `color-scheme` together when the reader prefers dark:

```css
:root {
  --recipe-accent: #9a3412;
  --recipe-accent-text: #fff;
  --recipe-muted: #78716c;
}

@media screen and (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --recipe-accent: #fdba74;
    --recipe-accent-text: #1c1917;
    --recipe-muted: #a8a29e;
  }
}

[data-recipe-system-option][data-state='checked'] {
  background: var(--recipe-accent);
  color: var(--recipe-accent-text);
}

[data-recipe-ingredient][data-unquantified] {
  color: var(--recipe-muted);
}
```

`color-scheme: dark` also darkens the page's background and text if your site doesn't set them, and `screen` keeps the light colors on paper. If your site switches themes with a class or attribute instead, put the dark values under its selector inside `@media screen`, such as `:root[data-theme='dark']`.

## Style for print

A printed recipe needs the quantities, not the controls. Hide the measurement system toggle, the servings controls, and the cook mode switch. `break-inside: avoid` keeps each ingredient and step on one page:

```css
@media print {
  [data-recipe-system-toggle],
  [data-recipe-servings-input],
  [data-recipe-servings-select],
  [data-recipe-cook-mode] {
    display: none;
  }

  [data-recipe-ingredient],
  [data-recipe-step] {
    break-inside: avoid;
  }
}
```

The quantities are for the servings shown, so print those as text with [`<Recipe.ServingsText>`](https://recipe-book.siraj.page/reference/react/servings.md#servingstext):

```tsx {8-10}
import type { ParsedRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'

export function RecipeCard({ recipe }: { recipe: ParsedRecipe }) {
  return (
    <Recipe.Root recipe={recipe}>
      <Recipe.ServingsSelect />
      <h2>
        Ingredients for <Recipe.ServingsText />
      </h2>
    </Recipe.Root>
  )
}
```

For the sample recipe, `RecipeCard` renders this HTML, indented here for reading:

```html
<select autoComplete="off" aria-label="Servings" data-recipe-servings-select="">
  <option value="2">2 servings</option>
  <option value="4" selected="">4 servings</option>
  <option value="8">8 servings</option>
  <option value="12">12 servings</option>
</select>
<h2>Ingredients for <span data-recipe-servings-text="">4 servings</span></h2>
```

The heading is text the server renders, so a page printed before its JavaScript loads shows it too. For a recipe with a yield but no servings, `<Recipe.ServingsText>` renders nothing, so print the yield with [`<Recipe.YieldText>`](https://recipe-book.siraj.page/reference/react/servings.md#yieldtext), as [Show the servings, the yield, or both](https://recipe-book.siraj.page/reference/react/servings.md#show-the-servings-the-yield-or-both) does.

## Data attributes

Each part that renders an element always sets its `data-recipe-*` attribute on it. Every `data-recipe-*` attribute is empty except `data-recipe-time` and `data-recipe-nutrient`, which are set to the row's kind or key. The other attributes appear only in the states that the table describes. With `asChild`, they go on your element instead of the part's. [`<Recipe.Image>`](https://recipe-book.siraj.page/reference/react/image.md) passes `data-recipe-image` to its `as` component as a prop, and Next.js's `Image` puts it on the `<img>`.

| Attribute                         | Element and part                                                                                                                  | Values or meaning                                                                                                                                                                                                                |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data-recipe-image`               | The `<img>` of [`<Recipe.Image>`](https://recipe-book.siraj.page/reference/react/image.md)                                        | The recipe's image.                                                                                                                                                                                                              |
| `data-recipe-servings-select`     | The `<select>` of [`<Recipe.ServingsSelect>`](https://recipe-book.siraj.page/reference/react/servings.md#servingsselect)          | The servings select.                                                                                                                                                                                                             |
| `data-recipe-servings-input`      | The `<input>` of [`<Recipe.ServingsInput>`](https://recipe-book.siraj.page/reference/react/servings.md#servingsinput)             | The servings input.                                                                                                                                                                                                              |
| `data-recipe-servings-text`       | The `<span>` of [`<Recipe.ServingsText>`](https://recipe-book.siraj.page/reference/react/servings.md#servingstext)                | The recipe's servings, in the servings shown.                                                                                                                                                                                    |
| `data-recipe-yield-text`          | The `<span>` of [`<Recipe.YieldText>`](https://recipe-book.siraj.page/reference/react/servings.md#yieldtext)                      | The recipe's yield, in the servings shown.                                                                                                                                                                                       |
| `data-recipe-system-toggle`       | The `<div>` of [`<Recipe.SystemToggle>`](https://recipe-book.siraj.page/reference/react/system-toggle.md#systemtoggle)            | The radio group of measurement systems.                                                                                                                                                                                          |
| `data-disabled`                   | The `<div>` of `<Recipe.SystemToggle>`                                                                                            | Set while nothing in the Root can convert, and until React hydrates the page if only the steps can. See [Recipes that can't convert](https://recipe-book.siraj.page/reference/react/system-toggle.md#recipes-that-cant-convert). |
| `data-recipe-system-option`       | The `<button>` of [`<Recipe.SystemOption>`](https://recipe-book.siraj.page/reference/react/system-toggle.md#systemoption)         | One measurement system.                                                                                                                                                                                                          |
| `data-state`                      | The `<button>` of `<Recipe.SystemOption>`                                                                                         | `checked` on the option for the system shown, and `unchecked` otherwise.                                                                                                                                                         |
| `data-disabled`                   | The `<button>` of `<Recipe.SystemOption>`                                                                                         | Set while the option is disabled, by its `disabled` prop or its `asChild` child's, or because the toggle is.                                                                                                                     |
| `data-recipe-cook-mode`           | The `<button>` of [`<Recipe.CookMode>`](https://recipe-book.siraj.page/reference/react/cook-mode.md#cookmode)                     | The cook mode switch.                                                                                                                                                                                                            |
| `data-state`                      | The `<button>` of `<Recipe.CookMode>`                                                                                             | `checked` while the switch is on, and `unchecked` while it's off.                                                                                                                                                                |
| `data-disabled`                   | The `<button>` of `<Recipe.CookMode>`                                                                                             | Set while the switch is disabled, by its `disabled` prop or its `asChild` child's, or until React hydrates the page.                                                                                                             |
| `data-recipe-ingredients`         | The `<div>` of [`<Recipe.Ingredients>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredients)                | Every section of ingredients.                                                                                                                                                                                                    |
| `data-recipe-ingredient-section`  | A `<section>` or `<div>` in `<Recipe.Ingredients>`                                                                                | One section. It's a `<section>` headed by its title when the section has a title, and a `<div>` when it doesn't.                                                                                                                 |
| `data-recipe-ingredient-list`     | The `<ul>` of [`<Recipe.IngredientList>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientlist)           | One section's ingredients.                                                                                                                                                                                                       |
| `data-recipe-ingredient`          | The `<li>` of [`<Recipe.Ingredient>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredient)                   | One ingredient.                                                                                                                                                                                                                  |
| `data-origin`                     | The `<li>` of `<Recipe.Ingredient>`                                                                                               | Where the quantity comes from: `authored`, `equivalent`, or `converted`, as [`QuantityOrigin`](https://recipe-book.siraj.page/reference/core/resolving.md#quantityorigin) describes. Absent when the ingredient has no quantity. |
| `data-unquantified`               | The `<li>` of `<Recipe.Ingredient>`                                                                                               | Set when the ingredient has no quantity.                                                                                                                                                                                         |
| `data-optional`                   | The `<li>` of `<Recipe.Ingredient>`                                                                                               | Set when the ingredient is optional.                                                                                                                                                                                             |
| `data-checked`                    | The `<li>` of `<Recipe.Ingredient>`                                                                                               | Set while the ingredient is ticked in a [`<Recipe.Checklist>`](https://recipe-book.siraj.page/reference/react/checklist.md).                                                                                                     |
| `data-recipe-ingredient-checkbox` | The `<input>` of [`<Recipe.IngredientCheckbox>`](https://recipe-book.siraj.page/reference/react/checklist.md#ingredientcheckbox)  | The ingredient's checkbox. Each `<Recipe.Ingredient>` in a `<Recipe.Checklist>` renders one by default.                                                                                                                          |
| `data-recipe-ingredient-quantity` | The `<span>` of [`<Recipe.IngredientQuantity>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientquantity) | The quantity, in the servings and system shown.                                                                                                                                                                                  |
| `data-recipe-range-separator`     | A `<span>` in `<Recipe.IngredientQuantity>`                                                                                       | The dash of a quantity range, as in "2–3 tbsp". It has `aria-hidden`.                                                                                                                                                            |
| `data-recipe-ingredient-name`     | The `<span>` of [`<Recipe.IngredientName>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientname)         | The ingredient's name.                                                                                                                                                                                                           |
| `data-recipe-ingredient-note`     | The `<span>` of [`<Recipe.IngredientNote>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientnote)         | The ingredient's note.                                                                                                                                                                                                           |
| `data-recipe-ingredient-optional` | The `<span>` of [`<Recipe.IngredientOptional>`](https://recipe-book.siraj.page/reference/react/ingredients.md#ingredientoptional) | The label after an optional ingredient.                                                                                                                                                                                          |
| `data-recipe-steps`               | The `<div>` of [`<Recipe.Steps>`](https://recipe-book.siraj.page/reference/react/steps.md#steps)                                  | Every section of steps.                                                                                                                                                                                                          |
| `data-recipe-step-section`        | A `<section>` or `<div>` in `<Recipe.Steps>`                                                                                      | One section. It's a `<section>` headed by its title when the section has a title, and a `<div>` when it doesn't.                                                                                                                 |
| `data-recipe-step-list`           | The `<ol>` of [`<Recipe.StepList>`](https://recipe-book.siraj.page/reference/react/steps.md#steplist)                             | One section's steps.                                                                                                                                                                                                             |
| `data-recipe-step`                | The `<li>` of [`<Recipe.Step>`](https://recipe-book.siraj.page/reference/react/steps.md#step)                                     | One step.                                                                                                                                                                                                                        |
| `data-checked`                    | The `<li>` of `<Recipe.Step>`                                                                                                     | Set while the step is ticked in a [`<Recipe.Checklist>`](https://recipe-book.siraj.page/reference/react/checklist.md).                                                                                                           |
| `data-recipe-step-checkbox`       | The `<input>` of [`<Recipe.StepCheckbox>`](https://recipe-book.siraj.page/reference/react/checklist.md#stepcheckbox)              | The step's checkbox. Each `<Recipe.Step>` in a `<Recipe.Checklist>` renders one by default.                                                                                                                                      |
| `data-recipe-step-name`           | The `<b>` of [`<Recipe.StepName>`](https://recipe-book.siraj.page/reference/react/steps.md#stepname)                              | The step's name.                                                                                                                                                                                                                 |
| `data-recipe-step-text`           | The `<span>` of [`<Recipe.StepText>`](https://recipe-book.siraj.page/reference/react/steps.md#steptext)                           | The step's text. Each `<Recipe.Step>` renders one by default.                                                                                                                                                                    |
| `data-recipe-temperature`         | A `<span>` in `<Recipe.StepText>`                                                                                                 | One temperature in the text, in the system shown.                                                                                                                                                                                |
| `data-recipe-duration`            | A `<time>` or `<span>` in `<Recipe.StepText>`                                                                                     | One duration in the text, as written. It's a `<time>` with a `dateTime` in seconds, or a `<span>` for a range, such as 25–30 minutes.                                                                                            |
| `data-recipe-converted-step-text` | The `<span>` of [`<Recipe.ConvertedStepText>`](https://recipe-book.siraj.page/reference/react/steps.md#convertedsteptext)         | The step's text with its quantities in the system shown. It has `data-recipe-step-text` too, so styles for `<Recipe.StepText>` apply to it, and so do the temperature and duration attributes.                                   |
| `data-recipe-quantity`            | A `<span>` in `<Recipe.ConvertedStepText>`                                                                                        | One mass or volume quantity in the text, in the system shown.                                                                                                                                                                    |
| `data-recipe-times`               | The `<dl>` of [`<Recipe.Times>`](https://recipe-book.siraj.page/reference/react/times.md)                                         | The recipe's times.                                                                                                                                                                                                              |
| `data-recipe-time`                | A `<div>` in `<Recipe.Times>`                                                                                                     | One time, set to its kind: `prep`, `cook`, `custom`, or `total`.                                                                                                                                                                 |
| `data-recipe-equipment`           | The `<ul>` of [`<Recipe.Equipment>`](https://recipe-book.siraj.page/reference/react/equipment.md)                                 | The recipe's equipment.                                                                                                                                                                                                          |
| `data-recipe-equipment-item`      | An `<li>` in `<Recipe.Equipment>`                                                                                                 | One item of equipment.                                                                                                                                                                                                           |
| `data-recipe-notes`               | The `<div>` of [`<Recipe.Notes>`](https://recipe-book.siraj.page/reference/react/notes.md)                                        | The recipe's notes.                                                                                                                                                                                                              |
| `data-recipe-note`                | A `<div>` in `<Recipe.Notes>`                                                                                                     | One note.                                                                                                                                                                                                                        |
| `data-recipe-nutrition`           | The `<dl>` of [`<Recipe.Nutrition>`](https://recipe-book.siraj.page/reference/react/nutrition.md)                                 | The recipe's nutrition facts.                                                                                                                                                                                                    |
| `data-recipe-nutrient`            | A `<div>` in `<Recipe.Nutrition>`                                                                                                 | The serving size or one nutrient, set to its key in [`Nutrition`](https://recipe-book.siraj.page/reference/core/types.md#nutrition), such as `fat`.                                                                              |
| `data-recipe-json-ld`             | The `<script>` of [`<Recipe.JsonLd>`](https://recipe-book.siraj.page/reference/react/json-ld.md)                                  | The recipe's JSON-LD.                                                                                                                                                                                                            |

Section headings, the `<option>` elements of `<Recipe.ServingsSelect>`, and the hidden `to` label in a quantity range have no data attributes. Neither do the `<label>` around each checkbox that a `<Recipe.Checklist>` renders by default, the `<dt>` and `<dd>` of each row in `<Recipe.Times>` and `<Recipe.Nutrition>`, the `<time>` in each time, a note's heading and `<p>`, or the link of an equipment item with a `url`.

## Next steps

- [SystemToggle](https://recipe-book.siraj.page/reference/react/system-toggle.md) for the toggle's states and rendering your own options
- [Ingredients](https://recipe-book.siraj.page/reference/react/ingredients.md) for rendering your own sections, ingredients, and sub-parts
- [Steps](https://recipe-book.siraj.page/reference/react/steps.md) for rendering your own sections and steps
- [Image](https://recipe-book.siraj.page/reference/react/image.md) for rendering another image component, such as Next.js's `Image`
- [Servings](https://recipe-book.siraj.page/reference/react/servings.md) for the servings select and input
