This skill treatsdisplay: grid as the first candidate while narrowing down wheredisplay: flex should be used, and is for mapping visual placement directly into code.
grid-templateIt mainly handlesgrid-area , empty cells,flex-direction naming, and the choice of ../kf-g-css-tak-responsive-rules/SKILL.md . For breakpoints, Container Queries, and fluid size unit selection, prioritizegap と margin , and for the separation of responsibilities of ../kf-g-css-spacing-gap-margin-rules/SKILL.md .
When to use this skill
display: gridとdisplay: flexWhen deciding which ofgrid-templateとgrid-template-areasto choosegrid-areaWhen reviewing which of- to write with
gridWhen deciding the naming rules for - When considering whether a vertical stacking layout can be expressed with
flexWhen checking whether AI-generated CSS contains ad-hoc
What to decide first
- Whether that element should really be a layout container
- Whether the visual placement can be expressed as-is with
grid-templateWhether flexis needed only for alignment in a single column direction- Whether layout names and appearance class names should be separated
Selection order
Normal flow
- Do not casually add
gridやflexto elements that work with document flow alone. - If headings, body text, and simple vertical stacking flow naturally, it is fine to first leave them in normal flow.
Grid
- For layouts that are more natural to think about in terms of row and column relationships, such as sections, cards, media objects, forms, and information panels, make
gridthe first candidate. - If you want to explain the appearance as a diagram, first consider whether it can be written with
grid-template. - Even in cases where it looks like you just want to stack elements vertically,
gap, start positions, and future reordering often makegridless prone to breaking.
Flex
flexUse- only for single-row alignment, space-between, centering, and light wrapping arrangements.
flexTypical examples are button groups, chip rows, toolbars, navigation, and inline-direction action groups.gridis that one-directional distribution or alignment is sufficient, and once two-dimensional placement or whitespace design becomes the main topic, return to
Grid rules
grid-template Make
- the top priority
grid-templateWhen building a layout, first consider whether it can be expressed visually with grid-template-areas.grid-templateover- . Because area names and track sizes can be written in the same place, the correspondence between appearance and implementation is less likely to break down.
- Within row strings, use spaces so that columns align visually.
.feature {
display: grid;
gap: 1rem 1.5rem;
grid-template:
"... title" auto
"media body " auto
"media meta " auto
/ 12rem 1fr;
}
Use empty grid cells
- If whitespace is part of the layout, actively use empty grid cells.
- Use empty cells not as a substitute for margins, but to make the structure explicit.
gapとmarginThe proper use of ../kf-g-css-spacing-gap-margin-rules/SKILL.md refer to
grid-area The name is held in [grid-area] with
- Do not use the class name as-is as the source for the
grid-areaname. - Use class as a selector for appearance and responsibility, and separate the layout surface name into the
[grid-area]attribute. [grid-area]Name the value of- Do not pour BEM names or decorative classes directly into surface names.
- If you place a common rule,
*not:where([grid-area])limit to [grid-area]The performance concern for a common rule limited togrid-areaitself only applies to grid items.- However,
attr()をcontentTyped syntax used outside of@supportslimited to progressive enhancement with
<section class="feature">
<h1 class="feature-title" grid-area="title">タイトル</h1>
<div class="feature-media" grid-area="media">...</div>
<p class="feature-body" grid-area="body">...</p>
</section>
@supports (grid-area: attr(grid-area type(<custom-ident>), auto)) {
:where([grid-area]) {
grid-area: attr(grid-area type(<custom-ident>), auto);
}
}
- By separating the responsibility of surface names from class, changing appearance class names is less likely to drag in the layout definition.
- For projects that want broad support including Safari and Firefox, do not assume this common rule, and have individual
grid-areadeclarations as a fallback.
Flex rules
flex limit to horizontal single-row alignment
- If single-axis arrangement and distribution alone are sufficient,
flexmay be used. - When you want to use
justify-content、align-items、flex-wrapin a horizontal row,flexis natural. - However, once you start caring about the relationship between wrapped rows or column-direction alignment, return to
grid.
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
}
AI output checklist
grid-templateEven though it can be written withgrid-template-areasare the row and column definitions not separated?- Are the spaces in the row strings broken, making the column correspondence hard to read?
grid-areaAre you reusing thegrid-areaAre you not applying the common rule of*to[grid-area]Are you limiting it to- typed
attr()Even though you assume the use of@supportsare you ignoring supported browsers or flexAre you forcing a two-dimensional layout with
Policy for output
- First, state in one sentence which of
gridとflexyou choose and why. gridIf you choosegrid-templatefirst show the visual structure withflexIf you choose[grid-area]If you use[grid-area]If you use the common rule of[grid-area]State explicitly whether it is qualified.- If spacing responsibility is the issue, ../kf-g-css-spacing-gap-margin-rules/SKILL.md Use together.
Final check
- For elements that are sufficient with the normal flow,
gridやflexIs it not added? - Visual placement
grid-templateEven though it can be expressed with , are you not escaping to another notation? - Are the spaces in the row strings aligned so that the columns are easy to read?
grid-areaIs the name separated into[grid-area]?- typed
attr()For common rules that use ,:where([grid-area])Limit to ,@supportsIs there a condition of or guaranteed supported browsers? flexIs limited to horizontal single-row alignment?