Writing CSS Layouts With LLMs in October 2026: Paste Your Real Markup, Prefer Grid and Flexbox, and Check It in a Real Browser

Ask a chat model for "a responsive card grid" and you will get CSS back in seconds. Sometimes it is clean and modern. Sometimes it is a pile of floats, fixed pixel widths, magic negative margins, and a media query for a breakpoint your site does not use. The model is not the problem so much as the prompt: CSS depends heavily on context the model cannot see, like your existing markup, your design tokens, and which browsers you support.

This guide covers a practical workflow for getting layout CSS out of an LLM that you can actually ship: give it the right inputs, steer it toward Grid and Flexbox, and verify the result in a real browser instead of trusting how the code looks.

Why LLM-written CSS goes wrong

Most broken layout code from a model falls into a few patterns:

  • It invents markup. If you do not paste your HTML, the model writes selectors for class names it made up, and then the CSS silently matches nothing.
  • It mixes eras. Training data spans decades of CSS, so you can get a clearfix hack next to gap, or vendor prefixes that have not been needed for years.
  • It hardcodes sizes. Fixed widths like width: 1200px or height: 400px look fine in the model's imagination and break on small screens or with longer text.
  • It ignores your system. Without your spacing scale, colors, and breakpoints, it picks its own, and your stylesheet slowly fills with one-off values.
  • It cannot see the page. The model has never rendered its output. Overflow, wrapping text, and focus outlines are things you only find by looking.

Step 1: Give the model your real markup and constraints

The single biggest improvement is pasting the actual HTML for the component. Trim it to the relevant structure, but keep the real class names and nesting. Then state the constraints the model cannot guess.

A prompt that works well looks like this:

Here is the HTML for a product card list (trimmed):

<ul class="card-list">
  <li class="card">
    <img class="card__img" src="..." alt="...">
    <h3 class="card__title">...</h3>
    <p class="card__body">...</p>
    <a class="card__link" href="...">Details</a>
  </li>
</ul>

Write CSS for this layout:
- Cards in a grid, as many columns as fit, each at least 240px wide.
- 1.5rem gap between cards. Use our custom properties:
  --space-md, --radius-md, --color-surface.
- The "Details" link sits at the bottom of every card,
  even when body text lengths differ.
- No fixed heights. No floats. No vendor prefixes.
- Target current evergreen browsers (Chrome, Edge, Firefox, Safari).
- Only use the class names in the markup above.

Each line removes a guess. "Only use the class names in the markup above" alone prevents a whole category of selectors that match nothing.

Diagram of a holy grail page layout built with CSS Grid, with header, sidebars, main content, and footer

A classic page layout expressed as CSS Grid areas. Image: CSS Grid Holy Grail Layout.png by Jnnnnn, Public domain, via Wikimedia Commons.

Step 2: Steer toward Grid and Flexbox

Modern CSS solves most everyday layouts with very little code. Knowing the idioms helps you recognize good output and reject bad output. For the prompt above, a solid answer looks roughly like this:

.card-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(240px, 100%), 1fr));
  gap: 1.5rem;
  list-style: none;
  padding: 0;
  margin: 0;
}

.card {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-md);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

.card__img {
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.card__link {
  margin-top: auto;
}

A few things worth checking in any answer you get:

  • repeat(auto-fill, minmax(...)) gives you responsive columns without a single media query. Wrapping the minimum in min(240px, 100%) stops a card from overflowing on screens narrower than 240px.
  • margin-top: auto inside a column flex container pushes the link to the bottom, so buttons line up across cards with different text lengths.
  • gap replaces margin tricks between items. If the model adds margins to every card and then negative margins on the container, ask it to use gap instead.
  • aspect-ratio keeps images consistent without fixed heights.

A useful rule of thumb to give the model: use Grid when you are laying things out in two dimensions (rows and columns together), and Flexbox when you are arranging items along one axis, like a toolbar or the inside of a card.

Step 3: Ask for an explanation of each rule

Follow up with "Explain each declaration in one line and tell me which ones are optional." This does two things. It surfaces rules the model added out of habit, which you can delete. And it often makes the model catch its own mistakes, such as a height that contradicts aspect-ratio, or a position: absolute that has no positioned parent.

If something looks unfamiliar, check it against MDN Web Docs rather than taking the model's word for it. MDN lists browser support for each property, which matters when the model reaches for something very new.

Step 4: Handle breakpoints on purpose

When you do need media queries, tell the model your actual breakpoints. Otherwise you will get a random set like 576px, 768px, 992px, and 1200px, which may not match the rest of your site.

Our breakpoints: 40rem (small), 64rem (large). Mobile-first:
base styles are for small screens, and media queries use min-width only.

Also consider whether a media query is the right tool. If a component should change layout based on the space it is given rather than the whole viewport, a container query fits better. A sidebar widget and a full-width version of the same component can then share one set of rules:

.card-list-wrapper {
  container-type: inline-size;
}

@container (min-width: 40rem) {
  .card {
    flex-direction: row;
  }
}

Container queries are supported in current versions of the major browsers. If you support older browsers, say so in the prompt and ask for a fallback.

The same website shown responsively on a desktop, notebook, tablet, and mobile phone

One layout, many screen sizes: always check generated CSS across them. Image: Responsive Web Design for Desktop, Notebook, Tablet and Mobile Phone.png by Muhammad Rafizeldi (MRafizeldi), CC BY-SA 3.0, via Wikimedia Commons.

Step 5: Look at it in a real browser

CSS is only correct when it renders correctly. Before you commit anything, open the page and run through a short checklist:

  1. Resize the window from very narrow (around 320px) to very wide. Watch for horizontal scrolling, squashed cards, and text that overflows its box.
  2. Use long content. Put a three-line title and a long unbroken word, like a URL, into one card. Models rarely plan for this. overflow-wrap: anywhere or min-width: 0 on flex and grid children often fixes it.
  3. Zoom to 200%. Layouts built in rem and with flexible tracks should reflow instead of breaking.
  4. Tab through it. Make sure focus outlines are visible and that the visual order matches the keyboard order. Grid and Flexbox can reorder items visually with order or grid placement while the tab order follows the HTML, which confuses keyboard and screen reader users.
  5. Open DevTools. Chrome, Edge, and Firefox all have Grid and Flexbox overlays that draw tracks and gaps on the page, which makes it obvious when an item is not where you think it is.

When you find a problem, describe the symptom to the model with specifics: "At 360px wide, the second card's title overflows to the right by about 20px. Here is the current CSS." A screenshot helps if your model accepts images, but a precise description works too.

Step 6: Keep the stylesheet consistent

The quiet cost of generated CSS is drift. Every answer brings slightly different spacing values, color codes, and naming habits. A few habits keep that in check:

  • Paste your tokens. Include your custom properties (or your Tailwind config, if you use utility classes) in the prompt and tell the model to use only those values.
  • Run a linter. Stylelint can flag unknown properties, duplicate selectors, and hex colors that should be variables. Add a rule set that matches your conventions and run it on generated code just like handwritten code.
  • Delete what you do not need. Generated CSS tends to be defensive. If removing a declaration changes nothing on screen at any size, remove it.
  • Watch specificity. If the model starts adding !important or long chained selectors to win a fight, stop and find which existing rule it is fighting instead.

A reusable prompt template

Here is a short template you can keep in a snippet file:

Context:
- Markup (real class names, trimmed): [paste HTML]
- Design tokens: [paste custom properties]
- Breakpoints: [list], mobile-first, min-width only
- Browser support: [list]

Task:
[Describe the layout in plain words, including what happens
on narrow and wide screens and with long content.]

Rules:
- Use only the class names in the markup.
- Prefer Grid for 2D layout, Flexbox for 1D, gap for spacing.
- No fixed heights, floats, or vendor prefixes unless asked.
- Do not change the HTML unless you explain why.

After the CSS, explain each declaration in one line
and list anything that needs a browser check.

The short version

LLMs are good at producing modern layout CSS when you tell them what they cannot see: your real markup, your design tokens, your breakpoints, and your browser targets. Push them toward Grid, Flexbox, gap, and container queries, and away from fixed sizes and old hacks. Then treat every answer as a draft until you have resized it, stuffed it with long content, zoomed it, and tabbed through it in a real browser. That last step is the one the model can never do for you.

Comments

Popular posts from this blog

Grok Bot - a step closer to AGI

Structured Outputs and Constrained Decoding for Reliable LLM APIs in Production (September 2026)

Tencent Hy4 Preview: Open 770B MoE Built for Real Work