CSS Layout with Flexbox and Grid

Learn how browsers arrange boxes on a page and how to take control of that arrangement. Start with normal flow, the display property and floats, then position elements with relative, absolute, fixed and sticky positioning and manage overlapping layers with z-index. Build rows and columns of items with flexbox and two-dimensional layouts with CSS grid, including sizing, gaps and alignment. Finish by choosing the right method for each part of a page, building common page and component layouts, and finding and fixing layout bugs with the browser's developer tools.

BeginnerLesson 5 of 8 in Web Development Basics Course

What you will learn

  1. Describe how a browser places boxes when no layout CSS is applied, change that behavior with the display property, and use floats to wrap text around an element.
    • Follow normal flow: block and inline boxes
      Predict where block and inline boxes appear in normal flow, how their sizes behave and when vertical margins collapse.
    • Change the layout with the display property
      Use display to change how an element takes part in its parent's layout and how its own children are laid out, and hide elements correctly.
    • Wrap text around an element with float
      Float an image or quote so that text wraps around it, clear content below floats and make a parent contain its floats.
  2. Shift elements from their normal place, pin them to a container or the viewport, keep them in view while scrolling, and control which overlapping element appears on top.
    • Offset elements with relative and absolute positioning
      Use relative and absolute positioning to nudge elements and to place overlays such as badges exactly inside a chosen container.
    • Keep elements in view with fixed and sticky positioning
      Pin elements to the viewport with fixed positioning, make headers and headings stick while scrolling with sticky positioning, and avoid the problems each one causes.
    • Control which elements appear on top with z-index
      Predict the stacking order of overlapping elements, use z-index within stacking contexts, and fix the classic "z-index does not work" problems.
  3. Lay out a row or a column of items with flexbox: set its direction and wrapping, control how items grow and shrink, and align and space them.
    • Set up a flex container and its axes
      Turn an element into a flex container, explain the main and cross axes, and control direction, wrapping and visual order without breaking the reading order.
    • Size flex items with grow, shrink and basis
      Calculate how flexbox shares free space and overflow among items with flex-basis, flex-grow and flex-shrink, and use the flex shorthand with confidence.
    • Align and space flex items
      Distribute space along the main axis, align items on the cross axis, push items apart with auto margins, and space them with gap.
  4. Define rows and columns with CSS grid, place items on lines and named areas, let the grid place items automatically, and align items and tracks.
    • Define grid tracks with fr, repeat and minmax
      Create the rows and columns of a grid, calculate track sizes with the fr unit, and use repeat(), minmax() and gap.
    • Place items by line numbers, spans and named areas
      Place grid items with line numbers, span and named template areas, and keep the visual order and the reading order in agreement.
    • Let the grid place items automatically
      Use auto-placement and the implicit grid to place any number of items, and build card grids that adjust their column count with auto-fill and auto-fit.
    • Align items and tracks in a grid
      Align each item inside its grid area, align the whole set of tracks inside the container, and center content with the place- shorthands.
  5. Pick the right layout method for each part of a page, build common page and component layouts, and find and fix layout bugs.
    • Choose the right layout method
      Decide between normal flow, flexbox, grid, positioning, floats and multiple columns for a given layout, and combine them on one page.
    • Build page-level layouts
      Build a readable content column, a header–sidebar–main–footer page, a footer that stays at the bottom and a sticky sidebar.
    • Build common component layouts
      Lay out recurring interface pieces: centered content, navigation bars, image-beside-text blocks, card grids with aligned parts, overlays, forms and input groups.
    • Find and fix layout problems
      Use the browser's developer tools and a checklist of common causes to find out why a layout breaks or a property has no effect, and test layouts against real content.

CSS Layout with Flexbox and Grid study guide

5 chapters

1

How boxes flow

Browsers arrange readable pages before any special layout rules are added. Block and inline boxes explain that starting arrangement, including why a narrow paragraph still takes its own line and why some margins combine. Learn how display changes the outside and inside of a box, then use floats for the specific job of wrapping text around an image. These rules help you predict what will change when you later introduce flexbox or grid.

1.1 How block and inline boxes share a page

A page already has a layout before you add flexbox or grid. Normal flow places boxes in HTML order. In horizontal English text, blocks stack downward and inline content runs from left to right, wrapping when a line fills.

Blocks take a line

Paragraphs, headings, divisions and sections are block boxes by default. A block with width: auto fills its container's available width and grows in height with its content. Setting its width does not invite the next block onto the same line.

<p>Read the <a href="/guide">setup guide</a> first.</p>
<p>Then install it.</p>

The paragraphs start on separate lines. The link is an inline box within the first paragraph. Even if the first paragraph is only 300px wide, the second paragraph still begins below it.

Inline boxes follow the text

A <span>, <a>, <strong> or <em> normally takes only the width of its text. A non-replaced inline box ignores width and height. Adding a { width: 200px; } therefore does not make the link 200px wide.

Horizontal padding, borders and margins push adjacent text aside. Vertical padding and borders are painted, but they do not make the surrounding lines farther apart; a large background can overlap the line above or below. Vertical margins have no effect. An image is a replaced element: it is inline by default, yet width and height do apply to it.

Vertical margins can combine

In normal flow, touching vertical margins of block boxes can collapse into one gap. A paragraph with margin-bottom: 24px followed by a heading with margin-top: 32px leaves 32px between them. The larger positive margin wins; the gap is not 56px. With a 24px positive margin and a −10px margin, the result is 14px. Horizontal margins stay separate.

A first child's top margin can also collapse with its parent's top margin when no border, padding or inline content separates them. A heading's 20px top margin can move an entire unpadded header downward, leaving no space inside the header. Use top padding for interior space, add a border, or give the parent display: flow-root. A last child's bottom margin can collapse with its parent's bottom margin too.

Flex and grid items do not collapse margins with one another. If you switch a parent from normal flow to flexbox, the same two positive margins may now add together. Floated and absolutely positioned boxes also keep their margins separate.

Directions follow the writing mode

The block direction is where successive blocks go; the inline direction is where text runs. Arabic and Hebrew use horizontal lines too, but their inline direction is right to left. Logical properties follow these directions: margin-inline-start: 16px gives a left margin in English and a right margin in Arabic. padding-block sets padding along the block direction.

Key Takeaways

  • Blocks start new lines even when given a narrow width.
  • Non-replaced inline boxes ignore width and height; images are an exception.
  • Touching vertical block margins can collapse; horizontal margins do not.
  • Logical properties follow the block and inline directions.
  • Padding or flow-root can stop a child margin collapsing through its parent.

1.2 What display changes

The display property controls a box's relationship with its parent and the arrangement of its children. These are separate choices. A flex container can occupy a whole line while arranging its own children side by side.

Read display from the outside in

The outer display type is block or inline. The inner display type describes the layout inside the box, such as flow, flex or grid.

ValueOutsideInside
blockBlockNormal flow
inline-blockInlineSeparate flow region
flexBlockFlex layout
inline-flexInlineFlex layout
gridBlockGrid layout
inline-gridInlineGrid layout

Modern two-keyword syntax makes the distinction visible: display: inline flex means the same as inline-flex. display: block flow-root means the same as flow-root. A single value such as grid supplies a block outer type by default.

Make an inline box accept a size
nav a {
  display: inline-block;
  padding: 12px 16px;
}

Each link still sits on a text line, but now it accepts width, height and vertical margins. Its vertical padding makes room in the line layout instead of just painting over nearby lines. This gives a link a larger click target.

Whitespace between inline-block elements in the HTML appears as a small space between the boxes. If you need a controlled row with an exact gap, flexbox provides that directly.

Changing display does not change the HTML element's meaning. A division styled inline remains a generic container. Choose a list, link or button for its purpose, then style its box.

Hide a box with the right effect
RuleLayout effect
display: noneThe element and its descendants generate no boxes.
visibility: hiddenThe box keeps its layout space while hidden.

A hidden error message with display: none makes no gap. Showing it pushes later content downward. Using visibility: hidden reserves the gap, so revealing the message need not move anything. display: none also prevents keyboard focus and removes the hidden content from screen-reader output. A visually-hidden class serves the different purpose of keeping text available to screen readers while hiding it visually.

Children and independent regions

With ul { display: grid; }, the list items become grid items. Links inside those list items remain inside their own parent's layout. To arrange those links, give the list item its own layout or use subgrid where shared tracks are needed.

display: contents removes the wrapper's own box so its children participate in the surrounding layout. Browser accessibility behavior can also remove the wrapper's role, so avoid this on meaningful controls, lists and tables.

display: flow-root creates a block formatting context, an independent flow region. It contains floated children and prevents the parent's margins from collapsing with its children's margins. It achieves those effects without adding clipping or scrollbars.

Key Takeaways

  • Display has an outer type and an inner type.
  • Flex and grid affect direct children.
  • inline-block keeps inline placement while accepting box dimensions.
  • display: none removes layout space; visibility: hidden preserves it.
  • flow-root contains floats and separates parent–child margins.
  • Use display: contents carefully because browser behavior can remove the wrapper’s accessible role.

1.3 Text wrapping with floats

A portrait in an article often needs text beside it for a few lines, then text at full width below it. A float creates that shape of text flow. It has a different job from a sidebar that stays in a separate column.

Put an image beside flowing text
img.portrait {
  float: left;
  width: 160px;
  margin: 0 16px 8px 0;
}

Place the image before the paragraphs that should wrap around it. The image moves to the left edge of its container. Following text uses the space on its right, staying 16px away horizontally and 8px below its bottom edge. Once the text passes the image, lines return to the container's full width. Use float: right for the opposite edge and float: none to remove floating.

A float is removed from normal flow, but nearby inline content still wraps around it. Following block boxes themselves ignore the float: their backgrounds and borders can extend behind it. Seeing a paragraph background under the image does not mean its text is failing to wrap.

A floated element becomes a block-level box. A floated <span> can therefore accept width and height. With no set width, a floated text box shrinks to fit its content; an image has its own dimensions.

Start the next section below the image

Suppose the text is shorter than the portrait, and a new section heading starts beside the portrait. Clearing controls that relationship:

.next-section {
  clear: both;
}

The heading moves below earlier floats on either side. clear: left clears left floats only; clear: right clears right floats only. Clearing a later heading does not itself make the earlier image an ordinary block again.

Make the parent wrap around its floats

A parent with only floated children gets no normal-flow height from them. Its background and border can look collapsed, and the next content may rise beside the floats. Let the parent contain them:

.profile {
  display: flow-root;
}

The parent's height now reaches around the floated image. This differs from clear: flow-root changes the parent's flow region, while clear moves a following element down past floats.

Older stylesheets often contain a clearfix: an ::after pseudo-element with content: "", display: block and clear: both. You may also see overflow: auto used to contain floats. That can add scrolling when content is too large; flow-root expresses the containment job directly.

Recognize older column layouts

Before flexbox and grid, page columns were often floated with percentage widths and clearing rules. Maintain those rules with an understanding of floats, but choose flexbox or grid for new columns that need sizing and alignment.

A direct child of a flex or grid container follows that layout, so float has no effect on that item. If a floated image stops wrapping text after you add display: flex to its parent, inspect the parent layout first.

Key Takeaways

  • A float lets following inline content wrap beside it.
  • clear moves a later box below earlier floats.
  • flow-root makes a parent contain its floated children.
  • Floats have no effect on flex or grid items.
2

Positioning and overlapping layers

Positioning controls offsets, overlays and elements that stay visible during scrolling. The key is knowing which space a box keeps and which ancestor supplies its reference. Work from a badge inside a card to fixed controls and sticky headings, then examine overlapping layers. Stacking contexts explain why a dropdown can stay hidden even when its own z-index is very large, and show which ancestor actually needs to change.

2.1 Offsets and the box they are measured from

Positioning lets one box overlap another or move away from its normal location. Before setting an offset, decide whether the box should keep its original space and which ancestor should act as its reference.

Static, relative and absolute
PositionSpace in normal flowOffset reference
staticKeptOffsets have no effect
relativeKeptThe box's original position
absoluteRemovedIts containing block

With position: relative; top: 10px; left: 20px, a box is drawn 10px down and 20px right from its original location. Its neighbors stay where they were. The original gap remains, so a large offset can make the box overlap its neighbors.

A relative parent needs no offsets to provide a reference for an absolute child. This is a common use of relative positioning.

Anchor a badge to a card
.card {
  position: relative;
}
.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

The badge is out of normal flow, so the card reserves no space for it. Its top and right edges sit 8px inward from the card's padding edges. Leave enough room in the card's content so a long title cannot run under the badge.

An absolute box's containing block is usually the padding box of its nearest positioned ancestor. A positioned element has any position value except static. If the card remains static, the badge looks farther up the ancestor chain. With no qualifying ancestor, it uses the initial containing block: a viewport-sized rectangle at the top of the document that scrolls with the page. An ancestor's non-none transform, filter or perspective can also establish this reference.

Stretch or center an overlay

An absolute box with automatic size and offsets that do not stretch it shrinks to fit its content. Setting both left and right offsets with an automatic width makes it fill the space between them; the top and bottom pair works similarly for an automatic height.

.overlay {
  position: absolute;
  inset: 0;
}

inset sets top, right, bottom and left together. With automatic dimensions and no margins, this overlay fills the containing block. A set width or height changes the sizing, so remove those limits when the layer should cover the whole box.

For a centered badge of unknown size, use top: 50%; left: 50%; transform: translate(-50%, -50%). The offsets move its top-left corner to the containing block's center. The translation moves it back by half its own width and height. With a known width and height, inset: 0; margin: auto is another centering method.

Check the percentage reference

A content-box parent with width: 400px and horizontal padding of 20px has a 440px padding box. An absolute child with width: 50% therefore has a 220px content width. Measuring only the parent's 400px content box gives the wrong result.

Use absolute positioning for a badge, tooltip or overlay. Page columns need boxes that respond to one another's size; flexbox, grid and normal flow provide that relationship.

Key Takeaways

  • Relative positioning keeps the original space; absolute positioning removes it.
  • An absolute box usually measures offsets from its nearest positioned ancestor’s padding box.
  • inset: 0 stretches an automatically sized absolute overlay to its containing block.
  • Offset percentages and translation percentages use different reference boxes.
  • Use normal flow, flexbox or grid for the page’s main structure.

2.2 Elements that stay visible while you scroll

A back-to-top control and a table heading both need to stay visible, but they have different relationships with the page. Fixed positioning ties a control to a reference window. Sticky positioning keeps a box in the flow and limits its movement to its section.

Pin a control to the viewport
.back-to-top {
  position: fixed;
  right: 16px;
  bottom: 16px;
}

The button stays at the window's lower-right corner as the document scrolls. It takes no space in normal flow. A fixed header likewise reserves no space and can cover the first paragraph.

For a 64px fixed header, body { padding-top: 64px; } leaves space for the initial content. Also set html { scroll-padding-top: 64px; } so section links do not place their target headings under the header. Give the header a background so text scrolling behind it does not show through.

A non-none transform, filter or perspective on an ancestor can make that ancestor the fixed element's containing block. The element then stays inside that ancestor and can scroll away with it. Inspect ancestors when a fixed control unexpectedly moves with the page.

Let a heading stick within a section
thead th {
  position: sticky;
  top: 0;
  background: white;
}

A sticky heading occupies its normal place first. When scrolling would take it above the top: 0 threshold, it stays there. It cannot travel beyond its containing block: the end of its section eventually pushes it out of view. This is useful for long tables and section navigation.

BehaviorFixedSticky
Normal-flow spaceRemovedReserved
Starts at its ordinary locationNoYes
Stops at its section's endNoYes

The viewport is the usual fixed reference. Sticky uses the nearest relevant scrolling ancestor; the page is the reference when no nearer scrolling box intervenes.

Diagnose a sticky box that does not stick

Check these causes in order:

  1. Set a non-auto offset on the axis that should stick, such as top: 0 for vertical scrolling. The keyword sticky alone sets no threshold.
  2. Inspect ancestors for overflow: hidden, auto or scroll. They can become the sticky reference even when a different box is actually scrolling.
  3. Compare the sticky box's height with its containing block. A wrapper only as tall as its heading gives the heading no room to move.

A sticky sidebar in a grid can also stretch to the row's full height. align-self: start lets it keep its content height and leaves travel space.

Keep content and focus visible

Fixed and sticky bars use part of the visible screen. At 200% text zoom, a navigation bar can become much taller. Test keyboard navigation as well as scrolling. WCAG 2.2 criterion 2.4.11, Focus Not Obscured (Minimum), requires that author-added content does not entirely hide a focused element. Keep bars short and provide suitable scrolling space around targets.

Key Takeaways

  • Fixed positioning removes the box from flow; sticky preserves its place.
  • Sticky needs an offset and room to move inside its containing block.
  • Overflow on an ancestor can change the sticky reference.
  • Reserve space for fixed headers and check that focused content stays visible.

2.3 Stacking levels and stacking contexts

When boxes overlap, stacking order decides which one you see in front. A larger z-index can bring a box forward, but only within the group where that value is compared.

Make z-index apply

z-index works on positioned elements and on flex or grid items. A static paragraph in normal flow ignores it. A static grid item can use it without adding position: relative.

Without explicit levels, positioned boxes paint above ordinary non-positioned boxes. For comparable boxes at the same level, the one later in the HTML paints on top. Positive, zero and negative z-index values give you more control over that order.

A context is stacked as one unit

A stacking context contains a group that the browser stacks as a whole. A child's level cannot lift the group above a higher sibling context.

.site-header {
  position: relative;
  z-index: 1;
}
.menu {
  position: absolute;
  z-index: 9999;
}
main {
  position: relative;
  z-index: 2;
}

Here .menu is inside .site-header, while main is its sibling. Compare the outer groups first: the header is at 1 and main is at 2. Main paints above the entire header, including the menu. Increasing 9999 changes only the menu's place within the header.

Raise the header itself, for example to z-index: 3, or remove the unnecessary level on main. Choose the change that matches the intended order of the whole page.

Find what creates the group

The root element creates a stacking context. Common additional causes are:

  • A relative or absolute box with a non-auto z-index.
  • A flex or grid item with a non-auto z-index.
  • Every fixed or sticky box.
  • opacity below 1, or a transform or filter other than none.
  • isolation: isolate.

A parent with opacity: 0.9 can therefore trap a dropdown even if nobody set a level on that parent. Walk up the ancestors in developer tools when a higher number makes no visible difference. Look for the first pair of sibling groups whose order is wrong.

Keep decorations inside a component
.panel {
  position: relative;
  isolation: isolate;
}
.panel-decoration {
  position: absolute;
  inset: 0;
  z-index: -1;
}

isolation: isolate creates a context without changing the panel's appearance. The negative child paints behind the panel's text but above the panel's background. Without the local context, it can fall behind the parent's background and disappear.

Use a small shared scale for intended page layers: for example, 1 for raised cards, 10 for a sticky header, 100 for dropdowns and 1000 for a modal. CSS custom properties such as --z-dropdown: 100 make the choices easier to maintain.

Key Takeaways

  • z-index applies to positioned boxes and flex or grid items.
  • A stacking context is compared with neighboring contexts as one unit.
  • A child’s large level cannot escape a lower ancestor context.
  • Opacity and transforms can create unexpected contexts.
  • isolation: isolate contains a component’s stacking levels.
  • Use a small shared scale after checking the ancestor contexts.
3

Rows and columns with flexbox

Flexbox turns a group of children into a row or column that can share available space. Its axes explain direction and alignment, while basis, grow and shrink explain the final sizes. Follow a wrapped row and work through the arithmetic before choosing gaps, auto margins and alignment. You will also see why visual reordering can disrupt keyboard navigation and why a long code line can stop a flexible column from shrinking.

3.1 Flex rows, columns and wrapping

Flexbox arranges a group along one main direction. It suits navigation links, toolbars and other items whose sizes come from their content. A wrapped flex layout can have several lines, but each line shares its space independently.

Create the container
.nav {
  display: flex;
}

The navigation's direct children become flex items. A link inside a list item is not a flex item of the navigation unless it is also a direct child. Text directly inside the container forms an anonymous flex item.

The defaults are a row, no wrapping, no growing and permission to shrink. Items start at the main start edge. In a horizontal row, auto-height items stretch to the line's height, so they often appear equally tall.

Name the axes before aligning

The main axis follows flex-direction. The cross axis is perpendicular to it. In horizontal English text:

DirectionMain axisFirst item starts at
rowHorizontalLeft
row-reverseHorizontalRight
columnVerticalTop
column-reverseVerticalBottom

A row follows the inline direction, so its start is on the right in right-to-left text. A column follows the block direction. Main and cross describe roles, not permanent horizontal and vertical directions.

justify-content works on the main axis. align-items works on the cross axis. After switching from row to column, justify-content: center centers vertically, provided there is spare height.

Work out which items fit
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 200px;
}

Consider five cards in a 700px content width. Each starts at 200px, and each can grow or shrink. Three cards plus two gaps need 632px; four plus three gaps need 848px. Three fit on the first line.

The first line shares 700px minus 32px of gaps: each card becomes about 222.7px wide. The remaining two share 700px minus one 16px gap: each becomes 342px wide. Items on the second line do not inherit the first line's widths. Use grid when columns must line up across rows.

flex-flow: row wrap is shorthand for direction and wrapping. Without wrapping, flexbox keeps all the items on one line, shrinking them where their sizing limits allow it.

Keep reading order meaningful

order defaults to 0. Lower values appear first; order: -1 brings an item before ordinary items. Reversed directions and order change the drawing order, while screen readers and normal keyboard focus still follow the HTML.

Keep important sequences in a sensible HTML order. Visual reordering can make a keyboard user jump around the screen, and can fail WCAG 1.3.2, Meaningful Sequence, or 2.4.3, Focus Order, when meaning or operation is harmed.

An item can also be a container. A card in a flex row can use its own display: flex; flex-direction: column to stack its contents. Use inline-flex when the outer container should sit within a text line, such as a button holding an icon and label.

Key Takeaways

  • Flexbox changes the layout of direct children.
  • Direction sets the main axis; the cross axis is perpendicular.
  • Wrapped flex lines size independently.
  • Visual order changes do not change the HTML reading or focus order.
  • A flex item can contain its own flex layout.

3.2 How flex items grow and shrink

Flexbox begins with a size for each item, then adjusts those sizes to fit the container. The three parts of flex control that adjustment: grow, shrink and basis, in that order.

Start with the basis

flex-basis supplies the starting size along the main axis. With auto, it uses the item's width in a row or height in a column; if that size is also automatic, content supplies the size. An explicit basis takes precedence over width, while minimum and maximum sizes still limit the final result.

Subtract all bases and gaps from the container's content size. A positive result is free space for growing. A negative result is space the items need to give up.

Share positive space

Three items in a 600px row have bases of 100px and grow factors 1, 1 and 2. With no gaps, the bases use 300px, leaving 300px. Four grow shares divide that remainder into 75px each. The final sizes are 175px, 175px and 250px.

Now add gap: 20px. Two gaps consume 40px, leaving 260px after the bases. Each share becomes 65px; the final widths become 165px, 165px and 230px. A factor of 2 doubles the added space, not the entire final width.

Give up space using weighted shrink

Two items with bases of 300px and 400px occupy a 500px container with no gap. They need to lose 200px. If both have flex-shrink: 1, their shrink weights are 300 and 400: each factor is multiplied by its base size.

The first loses about 85.7px and finishes at about 214.3px. The second loses about 114.3px and finishes at about 285.7px. Equal shrink factors do not mean equal pixel losses. flex-shrink: 0 protects an item's base size, which is useful for an avatar or logo.

These calculations describe items without padding, borders, margins or size limits that alter the result. Check those constraints when a real layout differs.

Read common shorthand values
DeclarationExpanded valueStarting point
flex: initial0 1 autoWidth or content; can shrink
flex: auto1 1 autoWidth or content; can grow or shrink
flex: none0 0 autoWidth or content; stays that size
flex: 11 1 0%Zero basis in a definite-width row

Two items with content widths of 80px and 200px in a 600px row become 240px and 360px with flex: auto: both gain half of the 320px remainder. With flex: 1 and no limiting minimums, both start from zero and become 300px wide.

Let a flexible column become narrow

A flex item's automatic minimum can keep it as wide as an unbroken URL or code line. Permit shrinking on the item and handle the content inside it:

.sidebar { flex: 0 0 240px; }
.main { flex: 1; min-width: 0; }
.main pre { overflow-x: auto; }

The sidebar stays 240px wide. Main takes the remainder. The code scrolls within main instead of forcing the whole page wider. For ordinary long text, overflow-wrap: anywhere allows breaks. A zero minimum makes room available; it does not by itself wrap or scroll the content.

Key Takeaways

  • The shorthand order is grow, shrink, basis.
  • Grow factors divide added space, not final widths.
  • Shrink losses depend on each factor multiplied by its base size.
  • A non-auto basis takes precedence over width.
  • flex: 1 and flex: auto use different starting sizes.
  • min-width: 0 permits shrinking; inner content still needs suitable overflow handling.

3.3 Space and alignment in flexbox

Alignment begins after sizing. Identify the main axis, the cross axis and the space left on each. A property can be valid yet make no visible difference when no room remains to distribute.

Share space on the main axis

justify-content positions the group along the main axis. For three 100px items in a 600px row with no gaps, 300px remains:

ValueResult
center150px before and after the group
space-between150px between neighbors; none at the ends
space-around50px at each outer end; 100px between neighbors
space-evenlyFour equal spaces of 75px

flex-start packs items at main-start; flex-end packs them at main-end. start and end follow the writing direction instead. In an English row-reverse, flex-start packs against the right edge, but start packs against the left.

If items with flex: 1 consume all the line's space, justify-content has no remainder to distribute. Reducing or removing growth changes that situation.

Align within each line

align-items positions items on the cross axis inside their line. The usual default, stretch, makes auto-height items in a row fill the line's height. An item with a set height does not stretch this way. Use center, start or end for other positions, or baseline to line up text of different sizes by its baseline.

align-self overrides this choice for one item. Flexbox has no justify-self: items share the main axis as a group. justify-items does not align flex items either.

To center a single child in both directions, set justify-content: center; align-items: center on its flex parent. A taller parent, such as one with min-height: 100vh, gives vertical centering room to show. If the only item already fills the line's cross size, centering it there produces no movement.

Separate groups with auto margins
nav {
  display: flex;
  align-items: center;
  gap: 16px;
}
.logo {
  margin-inline-end: auto;
}

In horizontal English, the logo stays left while the links move right. Its auto end margin absorbs the remaining space. An auto start margin on an item instead pushes that item and those following it toward the end. Logical margins adapt when text direction changes.

A single item with margin: auto can center on both axes. When an item is too large, auto margins avoid placing its overflow beyond the unreachable start edge, which ordinary centering can do.

Distinguish lines from items

align-content moves the lines as a group on the cross axis in a wrapping container with spare room. It has no effect with flex-wrap: nowrap. With wrap, it can still position a line even if the current items fit on just one line. align-items handles the items within each line.

gap: 16px 24px sets a 16px row gap and a 24px column gap. Gap creates space between items and lines, never around the outside of the container. Add padding for outer space. Any space distributed by alignment is additional to the gap.

Key Takeaways

  • justify-content distributes remaining main-axis space.
  • flex-start follows the flex direction; start follows the writing direction.
  • align-items aligns items within lines; align-content distributes wrapping lines.
  • Auto margins absorb free space and can separate groups.
  • Gap adds interior spacing; padding adds outer spacing.
  • Use align-self for one item’s cross-axis alignment.
  • Flexbox does not support justify-self for individual main-axis placement.
4

Shared tracks with grid

Grid gives several rows a shared set of columns and lets individual boxes span or overlap those tracks. Define sizes, calculate fractional shares and place items by boundaries or named areas. Then let auto-placement handle a growing collection and compare auto-fill with auto-fit. Finish by separating item alignment within areas from track alignment within the whole container, so each property moves the part you intend.

4.1 Grid tracks and the space they share

Grid defines rows and columns together. Set the tracks on a container, then let its direct children occupy them. Unlike wrapped flex lines, grid rows share the same column structure.

Name the parts

A track is one row or column between two grid lines. A cell is the intersection of one row and one column. A grid area is a rectangle of one or more cells. Gaps, also called gutters, separate the tracks.

.dashboard {
  display: grid;
  grid-template-columns: 200px 1fr 2fr;
  gap: 20px;
}

grid-template-columns defines the explicit columns; grid-template-rows defines the explicit rows. Without explicit columns, a grid has one column and its items stack into rows.

Calculate an fr share

The fr unit shares the space left after fixed tracks and gaps. In a 900px container, the example has a 200px fixed column and two gaps of 20px. Subtract those first: 660px remains. The two flexible columns request three shares in total, so one share is 220px. The widths are 200px, 220px and 440px.

The fractions divide the remaining track space, not the entire 900px. A 2fr track gets twice the flexible share of a 1fr track when minimum sizes do not constrain them.

Repeat tracks and set limits

repeat(4, 1fr) is shorthand for four equal flexible tracks. Repeating a pattern is possible too: repeat(2, 100px 1fr) creates 100px 1fr 100px 1fr.

Track sizes can also use lengths, percentages, auto, min-content and max-content. The content keywords request sizes based on the content's intrinsic needs.

minmax(150px, 1fr) gives a track a 150px minimum and a flexible maximum. For rows created automatically, use grid-auto-rows: minmax(100px, auto) to get a 100px minimum while allowing taller content to expand a row.

A plain 1fr track has an automatic minimum, like minmax(auto, 1fr). A long unbreakable URL can make one column wider than the rest. Use repeat(3, minmax(0, 1fr)) when the columns must share the available width equally, and provide wrapping or scrolling for oversized content inside them.

Keep gaps inside the available width

gap: 16px 32px means 16px between rows and 32px between columns. It creates no padding around the grid.

Three columns of 33.33% already use almost all a 900px container. Two 20px gaps then add about 40px beyond its width. Fractional tracks account for those gaps before sharing space, so repeat(3, 1fr) avoids this percentage-plus-gap overflow when content minimums fit.

Let rows respond to content

grid-template-rows: auto 1fr auto suits a header, main area and footer. The outer rows fit their content. With a container height or minimum height to fill, the middle row takes the remaining height. Keep room for growing content; a minimum page height allows a longer page to expand.

Key Takeaways

  • Rows and columns are tracks bounded by grid lines.
  • fr shares space after fixed tracks and gaps.
  • repeat() can repeat one size or a track pattern.
  • minmax() sets a minimum and maximum track size.
  • minmax(0, 1fr) removes a flexible track’s automatic minimum.
  • Percentage tracks can overflow when gaps are added.

4.2 Placing items on lines and named areas

Grid placement describes an item's boundaries. Count the lines around the columns and rows, or name a rectangular region and place the item there. Both methods arrange boxes without changing their order in the HTML.

Count boundaries, not columns

A three-column grid has four column lines. In horizontal English, column line 1 is on the left and row line 1 is at the top. Negative numbers count backward from the explicit grid's end: -1 is its final line.

DeclarationPlacement
grid-column: 1 / 3Covers the first two columns
grid-column: 1 / -1Spans the explicit grid's full width
grid-column: 2 / span 2Starts at line 2 and covers two columns
grid-column: span 2Covers two columns at an automatic location

To cover three columns starting at line 1, end at line 4. Ending at line 3 covers only two tracks.

In a dashboard with four columns and three rows, place a chart with grid-column: 1 / 4; grid-row: 1 / 3. It occupies three columns and two rows: six cells. A statistics panel at grid-column: 4; grid-row: 1 / 3 occupies the last column beside it. A table at grid-column: 1 / -1; grid-row: 3 fills the third row.

Draw the page using area names
.page {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
}
header { grid-area: header; }
nav    { grid-area: nav; }
main   { grid-area: main; }
footer { grid-area: footer; }

Each string describes a row, and each word describes a cell. Repeating header across the first row lets the header span both columns. A period marks an empty cell. Every row must contain the same number of cells, and each repeated name must form one rectangle. An L-shaped named area makes the whole grid-template-areas declaration invalid.

An area called main also creates lines called main-start and main-end. You can use those names for another item's boundaries. Define custom line names in square brackets in the track list, such as [content-start] 600px [content-end].

Overlap inside one grid area

An image and caption can both use grid-area: 1 / 1. They share the first cell; the caption later in the HTML paints over the image. align-self: end moves the caption to the bottom of that area. Grid items accept z-index without needing a non-static position, so you can explicitly change their layer order.

Preserve the reading sequence

Manual placement affects appearance, not the HTML order followed by screen readers and keyboard focus. Keep instructions, headings and links in a meaningful source order. A layout can look sensible while tab navigation jumps between distant cells; this can fail WCAG 1.3.2 or 2.4.3 when meaning or operation suffers.

Key Takeaways

  • Placement uses grid lines; three tracks have four boundaries.
  • -1 refers to the last line of the explicit grid.
  • Every named area must be rectangular, and all template rows need equal cell counts.
  • Items can overlap in one area and use z-index without positioning.
  • Grid placement does not change reading or focus order.

4.3 Automatic grids that fit their content

A product list can grow from a few cards to many without changing its CSS. Define the columns, let auto-placement create rows, and choose whether unused columns should keep their space.

Let items find the next cell

The default grid-auto-flow: row places unpositioned items in source order, moving across a row and then down to the next. It skips occupied cells. Extra tracks form the implicit grid.

.products {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(150px, auto);
}

Seven items create three rows. The final row contains one item. Each row is at least 150px tall and can grow with its content. Explicit tracks come from the template; implicit tracks are added as placement needs them.

Negative line numbers count only within the explicit grid. If you define columns but no rows, grid-row: 1 / -1 cannot mean every automatically created row. Define explicit rows when an item must span their complete height.

grid-auto-flow: column fills columns and creates extra columns sized by grid-auto-columns. Adding dense lets later smaller items backfill holes left by larger spans. That can change the visual sequence, so avoid it for ordered instructions or other content whose sequence matters.

Count columns from a minimum
.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

In a 1000px content width, four 200px columns plus three 16px gaps need 848px. Five columns plus four gaps need 1064px, so only four fit. Their flexible final widths are 238px: subtract 48px of gaps and divide the remaining 952px by four.

At 500px, two minimum columns plus one gap need 416px; three need 632px. Two fit and each becomes 242px. The number of columns changes with the available space without a breakpoint.

Choose what happens to unused columns

With enough items, auto-fill and auto-fit look the same. With only three items in that 1000px grid, auto-fill retains a fourth empty column, so each occupied column remains 238px wide. auto-fit collapses that empty track and its gap. The three occupied columns then share 968px and become about 322.7px each.

A column with an item anywhere in it is occupied, even if one cell in its last row is empty. Auto-fit collapses empty tracks, not every empty cell.

Protect the narrowest container

Auto-repeat needs a definite minimum or maximum track size. repeat(auto-fill, 1fr) is invalid. A fixed minimum of 400px can also overflow a 320px container. Use:

grid-template-columns:
  repeat(auto-fit, minmax(min(100%, 400px), 1fr));

min(100%, 400px) chooses the smaller minimum, so a narrow container can hold one track without being forced to 400px. Continue to handle oversized text or images within the cards.

Key Takeaways

  • Auto-placement fills available cells and creates implicit tracks as needed.
  • grid-auto-rows sizes automatically created rows.
  • Auto-fill retains empty tracks; auto-fit collapses them.
  • Column counts must include the gaps between columns.
  • Use a definite auto-repeat minimum or maximum.
  • min(100%, …) keeps a track minimum within a narrow container.
  • Dense packing can change visual order.

4.4 Align boxes inside areas or tracks inside a container

Grid alignment has two different scales. You can move an item within its area, or move the entire set of tracks within the container. First identify which space you want to distribute.

Align items within their areas

In horizontal English, justify- properties act on the inline, horizontal axis. align- properties act on the block, vertical axis. Grid supports item alignment on both axes.

Set on the containerOverride on one itemEffect
justify-itemsjustify-selfInline position within the area
align-itemsalign-selfBlock position within the area

Values include start, end, center and stretch. align-items: baseline aligns text baselines. Most automatically sized items stretch by default. An image with its own aspect ratio normally starts at the area's start edge instead of being stretched in both directions.

For a row of cards, stretching makes every card fill the row's height. Change to align-items: start and each card keeps its content height; their bottom edges can differ. align-self: end on one caption places it at the bottom of its grid area.

Center a box in both directions
.hero {
  display: grid;
  place-items: center;
  min-height: 60vh;
}

place-items combines align-items followed by justify-items. With one value, it applies that value to both. The minimum height creates vertical room around a short item. place-self is the corresponding shorthand on an individual item.

The item remains a box with text inside. To center the words within that box, set text-align: center on the item. Moving a box to the center of a cell does not change how its text is aligned.

Move the tracks as a group

justify-content positions the full set of columns along the inline axis. align-content positions the rows along the block axis. These need extra space outside the tracks to show their effect.

For three fixed 200px columns in a 900px container with no gaps, 300px remains. justify-content: center leaves 150px before the first column and after the last. justify-content: space-between puts the outer columns at the container edges and distributes 150px between neighboring columns. Items stay inside their own tracks as the tracks move.

If the columns use fr and fill all available width, there is no horizontal remainder for justify-content to distribute. Setting justify-items: center can still visibly center a narrow child within its wide track. The first property positions tracks; the second positions items.

Key Takeaways

  • justify- follows the inline axis; align- follows the block axis.
  • Item alignment moves boxes within their areas.
  • Content alignment moves tracks within the container.
  • place-items combines the two item-alignment properties.
  • Use text-align to align text inside an item.
5

Build and debug real layouts

Real pages combine several layouts: a page grid can hold a flexible toolbar, cards and a positioned badge. Choose the method that fits each relationship, then build patterns that keep working when text grows or space becomes narrow. Work through content columns, footers, sidebars and form controls before using developer tools to locate inactive properties and overflow. Longer text, zoom and keyboard checks expose problems that sample content can hide.

5.1 Choose a method for the relationship you need

A page can use several layout methods at once. Choose each method for the relationship between the boxes in that part of the page: a sequence of text, a flexible group, shared columns or an overlapping layer.

Start from the content

Normal flow already handles articles, headings and paragraphs. It wraps text and moves later blocks down when earlier content grows. Add more structure when the design calls for a different relationship.

NeedUseful methodReason
A row or column of controlsFlexboxShares space among content-sized items
Columns aligned across several rowsGridDefines shared row and column tracks
A badge over an imageAbsolute positioningPlaces a layer relative to a chosen container
A control visible across the pageFixed positioningKeeps it tied to the viewport
A heading visible within its sectionSticky positioningKeeps flow space and stops at the section's end
Text wrapping around a photoFloatShortens nearby text lines around the image
Text flowing into newspaper columnsMultiple-column layoutContinues text from one column into the next

Flexbox is one-dimensional: it shares space along a row or column, with each wrapped line independent. Grid is two-dimensional: it defines columns and rows together. These are choices about the layout's constraints, not a ban on using either tool for a simple row.

Compare a wrapped row with shared columns

Seven cards occupy a 1000px content width with 16px gaps. In flexbox, give each card flex: 1 1 200px and allow wrapping. Four cards fit on line one and grow to 238px each. The three cards on line two grow to about 322.7px each. Each line spends its own free space.

Now use repeat(auto-fill, minmax(200px, 1fr)) in grid. Four columns fit. All seven cards use those same 238px column widths; the final row leaves one cell empty. Grid keeps alignment across the rows.

Use the flex version when wider items on the last line suit the design. Use the grid version when a consistent column structure matters.

Combine methods at different levels

A page can use grid for a sidebar and main area, flexbox for the main area's toolbar, and an absolute badge within one card. A card can be a grid item on the outside and a flex container on the inside. Set the outer relationship on its parent and the inner arrangement on the card itself.

For a long text list, columns: 3 or column-width: 250px creates multiple-column flow. Text continues down one column and then into the next; it is not a grid of independent cards. Check that this reading path suits the content.

Read older layouts with care

Floated columns, inline-block grids and HTML tables used for page layout still appear in older code. Their spacing and containment rules differ from flexbox and grid. When building new page structure, use modern layout tools. Keep <table> for data whose rows and columns express tabular relationships.

Key Takeaways

  • Choose grid for shared rows and columns, flexbox for an independent row or column.
  • Use floats when text must wrap around an image.
  • Use positioning for overlaps or scroll-related placement.
  • Multiple columns continue text through columns.
  • A box can be an item of one layout and a container for another.

5.2 Page patterns that grow with the content

A useful page layout has a readable content width, space at the edges and room for longer content. Build its main regions in flow so a growing article moves the footer down naturally.

Center a content column
.container {
  max-width: 1100px;
  margin-inline: auto;
  padding-inline: 16px;
}

The maximum limits growth on wide screens. Auto inline margins divide leftover space on both sides. Padding keeps text away from narrow screen edges. For running text, max-width: 65ch gives an approximate 65-character measure; one ch is the width of the digit 0 in the current font. A line length around 45–75 characters is a useful reading range, though actual letters vary in width.

Put page regions on a grid
.page {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
}
header { grid-area: header; }
aside  { grid-area: sidebar; }
main   { grid-area: main; }
footer { grid-area: footer; }

The header and footer span both columns. The sidebar keeps 240px, and main receives the remainder. The zero minimum on the flexible track prevents wide content from forcing the track beyond its available share. Handle that content with wrapping or scrolling inside main.

The three-column pattern often called the holy grail layout uses 200px 1fr 200px and middle-row areas "nav main aside", with the header and footer spanning all three columns. Fixed side columns still need enough available width; test the layout at narrow sizes.

Keep a short page's footer at the bottom
body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

With header, main and footer as direct children, the middle row fills unused height. A short page's footer reaches the window bottom; a long page's footer follows the content. This pattern is called a sticky footer, but the footer does not need position: sticky.

The traditional viewport unit is vh. On phones, 100vh can extend behind visible browser controls. Dynamic viewport height, dvh, follows the visible height as those controls change; all major browsers have supported it since 2022. Use min-height so longer content can enlarge the page. A fixed height can leave content overflowing over what follows.

A flex alternative uses a column body with min-height: 100dvh, then main { flex: 1; }. For a sidebar row, use .sidebar { flex: 0 0 240px; } and .main { flex: 1; min-width: 0; }.

Let a sidebar stick and an image span wide

A grid sidebar can use position: sticky; top: 16px; align-self: start. Keeping its own content height leaves room to move within its row.

For full-bleed article content, define columns as 1fr min(65ch, 100% - 32px) 1fr. Put ordinary children in column 2 and a wide image at grid-column: 1 / -1. The central track stays readable while the image spans the full container width.

Key Takeaways

  • Auto inline margins center a limited-width content column.
  • Use min-height for full-height pages that must grow.
  • A sticky footer pattern keeps the footer in normal flow.
  • dvh follows changing visible viewport height.
  • A zero flexible-track minimum helps prevent page overflow.
  • A sticky grid sidebar needs travel space; align-self: start avoids stretching.
  • A full-bleed item spans outside the article’s text column.

5.3 Reusable layouts for interface pieces

Small interface pieces combine the same few relationships: an image keeps its size, text fills what remains, related controls share a row, or several cards align their inner parts. Give each container the layout that expresses its job.

Choose the right kind of centering
Content to centerRule
One box on both axesParent: display: grid; place-items: center
A block with a limited widthBlock: margin-inline: auto
Text or inline contentParent: text-align: center

Flexbox can also center one child with justify-content: center; align-items: center. Make sure the container has spare space on the relevant axis.

A navigation row and a media row
nav { display: flex; align-items: center; gap: 16px; }
.logo { margin-inline-end: auto; }
.media { display: flex; align-items: flex-start; gap: 16px; }
.media img { flex: none; width: 64px; }
.media-body { flex: 1; min-width: 0; }

In English, the logo stays left and the links gather right. When the navigation has exactly two children, a logo and a link list, justify-content: space-between can separate them; the list needs its own flex layout to arrange its links.

In the media row, the image keeps its width. Text receives the remainder and can shrink. Cross-axis start alignment prevents an auto-height image from stretching to match a long text block. This suits a comment, profile or product line.

Cards with aligned buttons
.cards {
  display: grid;
  grid-template-columns:
    repeat(auto-fill, minmax(min(100%, 240px), 1fr));
  gap: 24px;
}
.card { display: flex; flex-direction: column; }
.card .button { margin-top: auto; }

The parent grid stretches cards in each row to the same height. Within each card, the auto top margin spends unused vertical space before the button, placing it at the bottom. Titles can still have different heights.

To align titles, text and footers across cards, use subgrid. Make each card a grid item spanning three parent rows, then set display: grid; grid-template-rows: subgrid. Each card adopts those parent rows for its three parts. Subgrid has worked across major browsers since September 2023.

Overlays, forms and input groups

For text over an image, make the wrapper a grid and put both children at grid-area: 1 / 1. A later caption paints above the image. Another option is a relative wrapper with an absolute caption layer using inset: 0.

A form with alternating labels and fields can use grid-template-columns: max-content 1fr; gap: 12px 16px; align-items: center. The widest label sets one shared label column. The fields align in the second column; separate flex rows cannot share that label width automatically.

A search group uses display: flex; gap: 8px, with input { flex: 1; min-width: 0; } and button { flex: none; }. The input can shrink while the button keeps its natural width. For tags or toolbar controls that should wrap at their natural widths, use display: flex; flex-wrap: wrap; gap: 8px.

Key Takeaways

  • Center a box, a block and inline text with the appropriate kind of alignment.
  • Use a fixed-size image and a flexible text item for a media row.
  • Column flexbox and an auto top margin align card buttons.
  • Subgrid shares parent tracks across nested card parts.
  • Grid can align labels across form rows.
  • A flexible input often needs min-width: 0.

5.4 Find the box causing the layout bug

A layout bug is easier to fix when you inspect the browser's actual boxes. Begin with the element that moved or overflowed, then check the layout it participates in before changing values.

Inspect in a useful order
  1. Find the element in developer tools.
  2. Inspect its parent's computed display: normal flow, flex and grid give properties different jobs.
  3. Check whether your property applies to that element in that layout.
  4. Compare computed sizes, margins and position with your expected values.
  5. Toggle one declaration at a time to see which constraint causes the problem.

The box-model panel shows content, padding, border and margin. Grid overlays show lines, numbers, tracks and gaps; flex overlays show items and their allocation. Use them to check what the browser built, not just what the stylesheet intended.

Check inactive properties

Offsets do nothing on a static element. Width and height do nothing on an ordinary inline <span>, though they do size an inline image. z-index needs a positioned box or a flex/grid item. float has no effect on flex or grid items, and justify-self cannot align a flex item.

A valid alignment declaration may have no visible effect because items already use all the space. align-content also has no effect on a nowrap flex container. Sticky positioning needs an offset and space to move.

Track down horizontal overflow

Overflow can come from an oversized box or from a smaller box pushed beyond an edge by margins, offsets or neighboring widths. Common causes include:

  • A fixed width, such as 1200px, larger than the viewport.
  • width: 100% plus padding under content-box sizing.
  • width: 100vw, which can include a visible vertical scrollbar's width.
  • A long unbroken word, URL or code line.
  • A flex item's automatic minimum or a 1fr track's automatic minimum.
  • An auto-repeat minimum wider than its container.

Temporarily add * { outline: 1px solid red; } to reveal edges. Outlines use no layout space, so they expose boundaries without changing box sizes as borders would.

Work through a wide code block

A flex page has a 240px sidebar and a main column containing a 1400px-wide code sample. The whole page scrolls sideways. The flex overlay shows main wider than its share, and its computed minimum width is auto.

.main { min-width: 0; }
.main pre { overflow-x: auto; }

The first rule lets the flex item shrink. The second keeps the wide code accessible through local horizontal scrolling. Hiding all page overflow would conceal the symptom without making the code available.

For other surprises, inspect collapsing margins, a fixed header covering content, an absolute child whose intended parent is static, or a layer trapped inside an opacity- or transform-created stacking context.

Test real content and narrow space

Resize the window, use a long name or URL, and zoom text to 200%. Check keyboard focus near fixed bars. WCAG 2.2 criterion 1.4.4 requires text resizing to 200% without loss of content or function. Criterion 1.4.10 generally requires reflow at 320 CSS pixels without two-direction scrolling; content that needs a two-dimensional layout, such as a data table, has exceptions. Test more than the short sample text that first made the page look correct.

Key Takeaways

  • Inspect the parent layout and computed values before changing declarations.
  • Use overlays and outlines to locate actual box boundaries.
  • Check whether a property applies and whether it has free space to use.
  • Handle both a flexible item’s minimum and its overflowing content.
  • Test narrow widths, 200% text zoom, longer text and keyboard focus.

CSS Layout with Flexbox and Grid examples

5 Walkthroughs

1

Read the page’s natural layout

13 steps
Follow an article’s boxes, change link display and wrap text around a photo.
  1. 1
    IntroductionStart with an ordinary article

    Picture an article with a heading, two paragraphs, a link and a portrait. The browser already knows how to arrange these elements before you add layout CSS: headings and paragraphs are block boxes, links sit inline in the text, and display can change that behavior.

    You will trace the default boxes, make the link a larger target, then place the portrait with a float so text wraps beside it. Watch both the text and the parent backgrounds: they respond to different parts of the layout.

  2. 2
    ExplanationTrace the block lines

    The heading and paragraphs are block boxes by default. Each starts a new line and grows taller when its content wraps.

    Set the first paragraph to width: 260px. It becomes narrow, but the next paragraph still begins below it. A block does not become inline just because there is spare horizontal room next to it. Leave width automatic when it should fill its available line.

  3. 3
    ExampleFollow the link inside a sentence

    In <p>Open the <a href="/notes">reading notes</a> before class.</p>, the link is part of the text line. It takes the space its words need and can wrap with the sentence.

    Adding width: 190px does not resize this ordinary inline link. Vertical padding can paint a larger background without moving adjacent text lines farther apart. An inline image is different: its width and height do apply.

  4. 4
    ExampleMeasure a collapsed gap

    The article paragraph has a 14px bottom margin. The following heading has a 26px top margin. When those positive vertical margins touch in normal flow, they collapse to 26px.

    They do not add to 40px. If the parent later becomes flex or grid, the margins between its items stay separate, so the same declarations can produce more space.

  5. 5
    ChallengePut the gap inside the header

    An unpadded header contains only a heading with a top margin. The margin collapses through the header and the gap appears above its background. How can you create interior top space instead?

    ---reveal---

    Remove the heading’s top margin and add top padding to the header. Padding belongs inside the header’s box. A border or flow-root would also stop parent–child collapse, but padding directly creates the desired interior spacing.

  6. 6
    ExplanationGive the link its own sized box

    Use display: inline-block on the article link when it needs a width and a taller padded target while staying in the text line.

    .article-link {
      display: inline-block;
      padding: 10px 14px;
    }
    

    The box now participates with its full vertical size. Display changes its layout behavior; it remains an HTML link. Whitespace between neighboring inline-blocks still appears as a text space.

  7. 7
    Key InsightSeparate outside from inside

    A box can take part in one layout and run another inside itself. display: flex means block outside, flex inside. inline-flex keeps the same inner flex layout but uses inline outer placement.

    The rule affects direct children. A flex navigation makes its list a flex item; it does not automatically make every nested link an item in that outer row. Put a separate layout on the list when its links need their own row.

  8. 8
    ChallengeReserve room for a message

    A reading-status message should be hidden initially, with its space reserved so the article does not shift when it appears. Should it use display: none or visibility: hidden?

    ---reveal---

    Use visibility: hidden to keep its layout space. display: none removes its box and the gap, so showing it would move later content. If text must remain available to screen readers while hidden visually, a visually-hidden class serves that separate purpose.

  9. 9
    ExampleFloat the portrait

    Place the portrait before the paragraph and float it:

    .portrait {
      float: left;
      width: 130px;
      margin: 0 18px 10px 0;
    }
    

    Text lines wrap beside the photo with an 18px horizontal gap. Once they pass its bottom margin, they return to full width. The paragraph’s background may extend behind the float even though the text itself stays beside it.

  10. 10
    ExampleContain the image’s height

    If the article text is very short, the float may extend below its parent’s background. Floated children do not supply ordinary flow height.

    Add display: flow-root to the wrapper. Its independent flow region now contains the portrait, so the wrapper’s border and background reach around it. This also separates parent–child margins; it does not add clipping or a scrolling area.

  11. 11
    ChallengeBegin a new section below the portrait

    The portrait still reaches below the article’s first paragraph. The next section heading must begin beneath it, while the paragraph may keep wrapping. What rule belongs on the heading?

    ---reveal---

    Use clear: left, or clear: both if floats on either side should be cleared. This moves the heading below earlier floats. It is a different job from flow-root, which makes a parent include the height of its floated children.

  12. 12
    ChallengeA parent change removes the wrapping

    You change the photo-and-text wrapper to display: flex. Its floated portrait no longer makes text lines wrap beneath the image. Why, and what restores the intended article flow?

    ---reveal---

    The portrait is now a flex item, so its float has no effect. The paragraph occupies a separate flex item. Return this wrapper to normal flow, keep the portrait floated, and use flow-root if the wrapper should contain the image.

  13. 13
    RecapRead the parent before changing the child

    Block and inline behavior explains the article’s starting layout. Display can change outer participation, inner layout or whether a box exists at all.

    For text around a portrait, keep the text in ordinary flow with a float, contain the float when needed, and clear later sections that must start below it. When the result changes unexpectedly, inspect the parent’s display as well as the child’s rules.

2

Place badges and keep navigation visible

14 steps
Build a local overlay, compare fixed and sticky behavior, then repair a hidden menu.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionDecide what should move with what
  2. 2
    ExplanationKeep or remove the original space
  3. 3
    ExampleMake the card the badge’s reference
  4. 4
    ExamplePlace the badge inside the corner
  5. 5
    ChallengeChoose the percentage reference
  6. 6
    ExamplePin help to the window
  7. 7
    ExampleKeep a section heading in its section
  8. 8
    ChallengeA heading has nowhere to travel
  9. 9
    TipLeave space beneath a fixed header
  10. 10
    ExplanationCompare the outer groups first
  11. 11
    ExampleFix the ancestor level
  12. 12
    ChallengeDoes a static grid caption need position?
  13. 13
    ChallengeA fixed toolbar still loses the overlap
  14. 14
    RecapCheck reference, flow space and group
3

Build a flexible toolbar and card row

17 steps
Follow direction, calculate growing and shrinking, and use alignment only after sizing.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionBuild a row that shares its space
  2. 2
    ExplanationMake only the direct children flex items
  3. 3
    ExplanationTurn the axes with direction
  4. 4
    ChallengeCenter a vertical group
  5. 5
    ExampleStart the growth calculation
  6. 6
    ExampleAdd growth to the bases
  7. 7
    ExampleWeight the shrinking by base size
  8. 8
    ChallengeChoose equal widths or preserve size differences
  9. 9
    TipLet a long code line stay local
  10. 10
    ExampleLet cards wrap into independent lines
  11. 11
    ExplanationDistribute only the leftover space
  12. 12
    ExampleAlign text within the line
  13. 13
    ChallengePush a group to the far end
  14. 14
    Key InsightLines and items have different alignment
  15. 15
    TipKeep oversized centered content reachable
  16. 16
    ChallengeA centered row does not move
  17. 17
    RecapRead flexbox in three stages
4

Build a dashboard and an automatic card grid

18 steps
Calculate tracks, place a panel, adapt column counts and choose the correct alignment scale.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionGive several rows shared boundaries
  2. 2
    ExplanationTracks are the spaces between lines
  3. 3
    ExampleSubtract fixed tracks and gaps
  4. 4
    ExampleTurn shares into column widths
  5. 5
    ChallengePercentage columns exceed the row
  6. 6
    ExampleCount the panel’s end boundary
  7. 7
    ExplanationUse names for page regions
  8. 8
    ChallengeA shape that areas cannot express
  9. 9
    ConnectionShare a cell to create an image layer
  10. 10
    ExplanationLet new rows appear
  11. 11
    ExampleFind how many minimum columns fit
  12. 12
    ExampleCollapse unused tracks with auto-fit
  13. 13
    ChallengeKeep a minimum inside a narrow box
  14. 14
    ExplanationMove an item inside its area
  15. 15
    ExampleMove the columns as one group
  16. 16
    ChallengeWhy do card bottoms stop lining up?
  17. 17
    ChallengeA full-width banner above automatic cards
  18. 18
    RecapSeparate sizing, placement and alignment
5

Assemble a reading page and fix its weak points

19 steps
Choose methods for the page and its components, then test the layout with difficult content.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionBuild from the page down to its parts
  2. 2
    ExplanationGive each method a concrete job
  3. 3
    ChallengeChoose columns for a resource collection
  4. 4
    ExampleLimit the reading width
  5. 5
    ExampleReserve one fixed and one flexible column
  6. 6
    ExampleMake room for the footer without fixing it
  7. 7
    TipAllow height and viewport space to change
  8. 8
    ChallengeLet a photo go wider than the article
  9. 9
    ExampleNest a flex toolbar inside the page grid
  10. 10
    ExampleKeep the image and text in balance
  11. 11
    ExampleAlign card parts at the needed level
  12. 12
    ExampleLet a field share a row with a button
  13. 13
    ChallengeCenter the box or its words?
  14. 14
    ExplanationInspect the layout the browser built
  15. 15
    ExampleFollow a wide code block outward
  16. 16
    TipReveal edges without moving them
  17. 17
    ChallengeA width reset is not enough
  18. 18
    ChallengeTest the page beyond the sample
  19. 19
    RecapUse real content to check each relationship

CSS Layout with Flexbox and Grid practice questions

50 Practice Questions in 5 sets

Set 1: Flow, layers and flexible containers10 Practice Questions

Question 1 of 10

A profile box contains only a floated photo, and its background does not extend around the photo. What display value on the profile box contains the float?
  1. inline
  2. contents
  3. flow-root
  4. none
Show answerHide answer
Answer: C
Flow-root creates an independent flow region whose height includes its floated children. The parent can then draw its background around the photo. Display contents removes the parent’s box, so it cannot provide the visible background and containment needed in this situation.

Question 2 of 10

An ordinary inline <span> has width: 180px and 10px vertical padding. Why can its background overlap neighboring text lines while its width stays content-sized?
  1. Inline width is ignored; vertical padding paints without enlarging line spacing.
  2. Inline width is inherited; vertical padding collapses into neighboring margins.
  3. Inline width sets a minimum; vertical padding removes the box from normal flow.
  4. Inline width reserves blank space; vertical padding changes only the baseline.
Show answerHide answer
Answer: A
An ordinary inline span ignores width, and its vertical padding is painted without making surrounding lines farther apart. This allows the background to intrude into nearby lines. Margin collapsing is a rule for certain block margins, so it does not explain the span’s vertical padding.

Question 3 of 10

A relative header at z-index: 4 contains an absolute menu at z-index: 800. A sibling relative main area is at z-index: 5. Both outer boxes are in the same parent stacking context. What change puts the entire header above main?
  1. Raise the menu to z-index: 900.
  2. Lower the menu to z-index: 6.
  3. Set the menu to position: relative.
  4. Raise the header to z-index: 6.
Show answerHide answer
Answer: D
The header and main create sibling stacking contexts, compared at levels 4 and 5. Raising the header to 6 raises its whole group above main. A higher menu number stays inside the lower header group and cannot change the order of those outer contexts.

Question 4 of 10

A paragraph has position: relative; top: 9px. What happens to the following paragraph’s normal-flow position?
  1. It moves down by 9px as well.
  2. It stays where it was laid out.
  3. It moves up into the original gap.
  4. It becomes absolutely positioned.
Show answerHide answer
Answer: B
Relative positioning moves where the first paragraph is drawn while preserving its original space. The following paragraph keeps its normal-flow position and may be overlapped by the shifted box. Moving up into the gap would follow removal from flow, as with absolute positioning.

Question 5 of 10

An English horizontal flex row needs its last control pushed to the far end while earlier controls remain at the start. What margin on the last control absorbs the free space before it?
  1. margin-inline-start: auto
  2. margin-inline-end: auto
  3. margin-block-start: auto
  4. margin-block-end: auto
Show answerHide answer
Answer: A
An auto inline-start margin in this row absorbs free space before the last control, pushing it to the far end. An auto inline-end margin absorbs space after the control instead. Block-axis margins act vertically in this writing mode and do not create the needed horizontal separation.

Question 6 of 10

A 540px flex row has two items with bases 240px and 480px and shrink factors 2 and 1. There are no gaps, margins, borders, padding or limiting minimums. What final widths result?
  1. 180px and 360px
  2. 120px and 420px
  3. 150px and 390px
  4. 160px and 380px
Show answerHide answer
Answer: C
The bases total 720px, so the row must lose 180px. The shrink weights are 2 times 240 and 1 times 480: both 480, so each item loses 90px. Proportional loss by base size alone would give 180px and 360px, but that ignores the different shrink factors.

Question 7 of 10

A three-column grid defines no rows and auto-places eight ordinary items. How many implicit rows are needed?
  1. Two rows.
  2. Three rows.
  3. Four rows.
  4. Eight rows.
Show answerHide answer
Answer: B
Row-by-row placement puts three items in each of the first two rows, then the remaining two in a third row. Only two rows would provide six cells and leave two items without space. The browser adds implicit rows as needed even though none were listed in the template.

Question 8 of 10

A narrow item should sit at the inline center of its own wide grid area, without moving the columns. What property belongs on that item?
  1. justify-content: center
  2. align-content: center
  3. text-align: center
  4. justify-self: center
Show answerHide answer
Answer: D
Justify-self positions one grid item on the inline axis within its own area. Justify-content positions the full set of columns inside the container instead. Text-align changes the inline content within the item’s box, so it does not move that box to the center of the area.

Question 9 of 10

A short page’s footer should reach the viewport bottom, then move down naturally when the article grows. What body grid setup supports this with header, main and footer as children?
  1. height: 100dvh; grid-template-rows: auto 0 auto
  2. max-height: 100dvh; grid-template-rows: 1fr auto 1fr
  3. min-height: 100dvh; grid-template-rows: auto 1fr auto
  4. height: auto; grid-template-rows: auto auto auto
Show answerHide answer
Answer: C
A minimum viewport height creates spare room that the middle 1fr row can fill on a short page. The minimum also lets the page grow with a longer article. A fixed height with a zero middle row cannot reserve room for the article’s growing content and can let it overflow neighboring regions.

Question 10 of 10

A flex page’s main item contains a long unbroken code line. Developer tools show main has min-width: auto; the whole page scrolls sideways. The code must remain available without widening the page. What paired fix addresses both boxes?
  1. Give main min-width: 0 and the code block overflow-x: auto.
  2. Give main width: 100% and the code block white-space: nowrap.
  3. Give main overflow: visible and the code block min-width: 100%.
  4. Give main flex-shrink: 0 and the code block overflow-x: hidden.
Show answerHide answer
Answer: A
Main’s automatic minimum can preserve the code line’s unbreakable width, so a zero minimum lets the flex item shrink. Local horizontal scrolling on the code block keeps the entire line available within that width. Hiding the code’s overflow would conceal part of the content instead of making it accessible.
Set 2: Boxes, space and layout choices10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

Two adjacent blocks in normal flow have touching positive vertical margins of 18px and 30px. How large is the collapsed gap?
  1. 18px
  2. 30px
  3. 48px
  4. 12px
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

An ordinary text link needs a set width while staying within a line of text. What display value gives it that box behavior?
  1. inline
  2. block
  3. contents
  4. inline-block
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

A badge is positioned absolutely inside a card. The card has position: relative and no other rule creates a containing block between them. What box supplies the badge’s offset reference?
  1. The card’s padding box.
  2. The card’s content box.
  3. The badge’s border box.
  4. The viewport’s content box.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

A section heading has position: sticky but all four offsets remain auto. What declaration lets it stick at the top during vertical scrolling?
  1. z-index: 1
  2. margin-top: 0
  3. top: 0
  4. height: auto
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

Three flex items have 60px bases and grow factors 1, 2 and 1 in a 460px row with two 20px gaps. No margins, padding, borders or size limits interfere. What are their final widths, in order?
  1. 130px, 200px, 130px
  2. 120px, 180px, 120px
  3. 105px, 210px, 105px
  4. 140px, 140px, 140px
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

In the three-value flex shorthand, which part sets the item’s starting size along the main axis?
  1. The third part, flex-basis.
  2. The first part, flex-grow.
  3. The second part, flex-shrink.
  4. The parent’s align-items value.
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A 760px grid has columns 160px 1fr 2fr and a 15px column gap. Content minimums do not constrain the tracks. How wide is the 2fr column?
  1. 400px
  2. 300px
  3. 190px
  4. 380px
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

A grid item must cover the first three columns of a five-column grid. What line placement gives that span?
  1. grid-column: 1 / 3
  2. grid-column: 2 / 4
  3. grid-column: 1 / 4
  4. grid-column: 1 / 5
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A directory has many cards. Cards in later rows must use the same column boundaries as cards in earlier rows, even when the last row is incomplete. What layout method best expresses that requirement?
  1. Grid with shared column tracks.
  2. Flexbox with wrapped flex lines.
  3. Floats with individual widths.
  4. Inline blocks with natural widths.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

Cards with different text lengths form one grid row. Their buttons must line up at the bottom without fixed card heights, and the cards must grow if text grows. How should the outer grid and inner cards work together?
  1. Start-align the cards; use flex columns and auto top margins on buttons.
  2. Stretch the cards; use flex columns and auto top margins on buttons.
  3. Stretch the cards; use flex rows and auto top margins on buttons.
  4. Stretch the cards; use flex columns and auto bottom margins on buttons.
Show answerHide answer

Save this lesson to see this answer.

Set 3: Shared tracks and changing content10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

A message should be invisible initially but reserve its current space so later content does not move when it appears. What property value supplies that behavior?
  1. display: none
  2. display: contents
  3. position: absolute
  4. visibility: hidden
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

An article has a left-floated portrait followed by a new-section heading. Text may wrap beside the portrait, but the heading must start below it. What rule should go on the heading?
  1. float: left
  2. clear: left
  3. overflow: visible
  4. position: relative
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

A sticky heading has top: 0 inside an ancestor with overflow: auto. The page scrolls, but that ancestor does not. What should be inspected as the likely reason the heading never sticks to the viewport?
  1. The overflow ancestor has become its scrolling reference.
  2. The heading’s source order has become its stacking level.
  3. The heading’s top offset is measured from its own height.
  4. The page’s flex direction has removed the sticky threshold.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

A tooltip is inside a static card with opacity: 0.8. The card is inside a relative section at z-index: 2. An overlapping sibling section is relative at z-index: 3. Raising the tooltip’s z-index changes nothing. What outer change can bring this tooltip’s whole section forward?
  1. Raise the card’s opacity to exactly 1.
  2. Raise the tooltip’s level above 3000.
  3. Raise its containing section above level 3.
  4. Raise the card’s own z-index to 4.
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

A vertical flex column has spare height and should center its group of children from top to bottom. What container declaration handles that axis?
  1. justify-content: center
  2. align-items: center
  3. justify-items: center
  4. align-self: center
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

A grid must have three exactly equal columns within 810px, with 24px gaps, even when one item contains a long URL. The URL may wrap anywhere. What combined setup handles track sizing and content?
  1. Use repeat(3, 33.33%) and overflow-wrap: anywhere on the URL’s item.
  2. Use repeat(3, minmax(0, 1fr)) and overflow-wrap: anywhere on the URL’s item.
  3. Use repeat(3, 1fr) and white-space: nowrap on the URL’s item.
  4. Use repeat(3, minmax(270px, 1fr)) and overflow: visible on the URL’s item.
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A template uses "banner banner" "banner main" as its two rows. Why is this named-area declaration invalid?
  1. The main name needs at least two cells.
  2. A grid area cannot cross a row boundary.
  3. Every area must fill an entire grid row.
  4. The banner cells do not form a rectangle.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

Three cards use repeat(auto-fit, minmax(180px, 1fr)) in an 860px grid with 20px gaps. They have no size constraints or extra spacing. How many occupied columns remain, and how wide is each?
  1. Four columns, exactly 200px each.
  2. Three columns, about 286.7px each.
  3. Three columns, about 273.3px each.
  4. Four columns, exactly 180px each.
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A long glossary should continue down one narrow column and then into the next, like a newspaper. What CSS layout feature directly provides that text flow?
  1. Grid layout with repeated 1fr tracks.
  2. Multiple-column layout with columns.
  3. Flex layout with flex-direction: row.
  4. Absolute positioning with equal offsets.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

An image and its caption share grid-area: 1 / 1. The caption follows the image in HTML and neither has z-index. Which one paints on top?
  1. The image paints on top.
  2. The taller child paints on top.
  3. The wider child paints on top.
  4. The caption paints on top.
Show answerHide answer

Save this lesson to see this answer.

Set 4: Diagnose placement and alignment10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

A header has no padding or border. Its first child heading has a 22px top margin that collapses with the header. The gap appears outside the header’s background. What change puts the spacing inside the header instead?
  1. Keep that child margin and add 22px top margin to the header.
  2. Remove that child margin and add 22px bottom margin to the header.
  3. Remove that child margin and add 22px top padding to the header.
  4. Keep that child margin and set the header’s position to relative.
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

A <ul> is a grid container. Each <li> contains a link. Which elements become grid items of the <ul>?
  1. The direct-child list items.
  2. The links inside the list items.
  3. The list items and their links.
  4. The list container’s siblings.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

A positioned card has content width 280px, 18px padding on each side and a 2px border. It uses content-box. An absolute child has width: 50% and right: 12px, with no own padding, border or margin. What is the child width and its left-edge distance from the card’s left padding edge?
  1. 140px wide; its left edge is 128px in.
  2. 160px wide; its left edge is 148px in.
  3. 158px wide; its left edge is 158px in.
  4. 158px wide; its left edge is 146px in.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

Seven flex cards have flex: 1 1 150px in a 640px wrapping row with 10px gaps. There are no margins, padding, borders or size limits. How do the cards divide into lines, and what widths do the two lines produce?
  1. Three then four; about 206.7px then 152.5px.
  2. Four then three; 152.5px then about 206.7px.
  3. Four then three; 160px on both lines.
  4. Four then three; 150px then about 213.3px.
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

An English flex container uses row-reverse. The group should be packed against the left edge, regardless of its reversed main-start. What justify-content value follows that writing-direction start edge?
  1. flex-start
  2. flex-end with row
  3. start
  4. space-between
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

What expanded track list is produced by repeat(2, 80px 1fr)?
  1. 80px 80px 1fr 1fr
  2. 80px 1fr 80px 1fr
  3. 160px 2fr
  4. 80px 1fr 1fr
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A grid has seven cards, four auto-fit columns that fit its width, and ordinary row-by-row placement. The last row has one empty cell. Does auto-fit collapse the fourth column, and why?
  1. No; its first-row item keeps that column occupied.
  2. Yes; any empty cell collapses its entire column.
  3. No; auto-fit never collapses generated columns.
  4. Yes; only the final row determines column occupancy.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

Two fixed 140px columns sit in a 500px grid with a 20px gap. No other spacing applies. With justify-content: center, how much space lies before the first column?
  1. 110px
  2. 90px
  3. 200px
  4. 100px
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A sticky sidebar shares a tall grid row with an article. It has top: 12px, no fixed height, and default stretching; it fills the row and does not visibly travel. No ancestor overflow interferes. What change creates room for it to stick while keeping it in flow?
  1. Set justify-self: start on the sidebar.
  2. Set align-self: start on the sidebar.
  3. Set position: absolute on the sidebar.
  4. Set z-index: 12 on the sidebar.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

A box uses width: 100%, 14px left and right padding, and box-sizing: content-box, with no border. Its parent’s content width is 390px. Why does this box exceed the parent?
  1. Its 390px content width gains 28px of padding.
  2. Its content width is reduced to 362px by padding.
  3. Its padding replaces the parent’s outside margins.
  4. Its width is measured from the viewport’s border.
Show answerHide answer

Save this lesson to see this answer.

Set 5: Combine the rules in new layouts10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

What grid shorthand centers each item on both axes inside its area?
  1. place-content: center
  2. place-items: center
  3. text-align: center
  4. justify-content: center
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

An image and a paragraph are direct children of a parent changed from normal flow to display: flex. The image still has float: left, but the paragraph no longer wraps its lower lines beneath it. How can ordinary text wrapping around the image be restored?
  1. Keep flex and add clear: both to the paragraph.
  2. Keep flex and increase the image’s left float margin.
  3. Keep flex and give the paragraph flex-grow: 1.
  4. Return that parent to normal flow and keep the image floated.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

A fixed help button sits inside a transformed panel that scrolls away. No other ancestor changes the fixed positioning reference. A separate 72px fixed header also covers same-page link targets. The button should stay tied to the viewport, and targets should remain visible below the header. What pair of changes addresses both references?
  1. Raise the button’s z-index; add 72px margin-bottom on the fixed header.
  2. Make the button sticky inside the panel; add 72px padding-bottom on html.
  3. Move the button outside the transformed panel; add 72px scroll-padding-top on html.
  4. Give the button top: 0; add 72px border-bottom to the link targets.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

Two relative panels share a parent stacking context: A has z-index 1, B has z-index 2. A contains an isolated component with a negative decoration and a menu at z-index 100. The menu must appear above B, while the decoration stays behind its component text. What change satisfies both goals?
  1. Raise A above 2 and keep the component isolated.
  2. Raise the menu above 200 and remove component isolation.
  3. Keep A at 1 and raise the isolated component above 2.
  4. Remove the menu’s position and keep both panel levels.
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

A flex row’s links are in a sensible HTML sequence, but order: -1 moves the last link visually first. What sequences do normal keyboard focus and screen-reader reading follow?
  1. Only focus follows visual order.
  2. Both follow the HTML sequence.
  3. Only reading follows visual order.
  4. Both follow the visual sequence.
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

Three non-growing 80px flex items sit in a 500px English row with 20px gaps. The third has margin-inline-start: auto, and the container has justify-content: center. No other spacing applies. Where does the third item start from the left content edge?
  1. 420px
  2. 280px
  3. 310px
  4. 400px
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A grid has six explicit columns and four explicit rows. A panel uses grid-column: 2 / -1; grid-row: 2 / span 2. How many cells does it cover?
  1. 8 cells
  2. 12 cells
  3. 15 cells
  4. 10 cells
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

An article grid uses 1fr min(65ch, 100% - 32px) 1fr. In its current font, 1ch is 8px. For content widths of 480px and 900px, what are the middle track widths, and which placement makes a photo full bleed?
  1. 480px and 520px; grid-column: 1 / -1.
  2. 448px and 868px; grid-column: 2.
  3. 448px and 520px; grid-column: 1 / -1.
  4. 520px and 520px; grid-column: 1 / 3.
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

A card collection needs titles, descriptions and footers to align across cards. Each card has those three direct children, but its current independent row grid cannot align their boundaries with neighboring cards. What relationship should replace that independent row sizing?
  1. Each card spans three parent columns and adopts them with column subgrid.
  2. Each card occupies one parent row and gives its footer an auto top margin.
  3. Each card occupies one parent cell and sets all three child rows to 1fr.
  4. Each card spans three parent rows and adopts them with row subgrid.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

A 210px box starts 150px from the viewport’s left edge. The viewport is 340px wide. How far does the box’s right edge extend beyond the viewport’s right edge?
  1. 20px
  2. 60px
  3. 130px
  4. 150px
Show answerHide answer

Save this lesson to see this answer.

Stuck on your own question? Homework Help

Key Terms

86 Key Terms, each with its definition

A

Absolute positioning
Absolute positioning removes a box from normal flow and places it using offsets from its containing block.
Absolute positioning puts the badge 10px from the card’s top and right padding edges.
Align-content
The align-content property distributes flex lines on the cross axis or grid rows on the block axis when room remains.
Align-content can center a wrapping flex line, but it has no effect when the container uses nowrap.
Align-items
The align-items property sets item alignment on the flex cross axis or the grid block axis within each item’s area.
Align-items set to baseline lines up a toolbar label with the first text line of a larger heading.
Align-self
The align-self property overrides the parent’s item-alignment choice for one flex or grid item on its alignment axis.
Align-self set to start prevents one grid sidebar from stretching to fill a tall article row.
Auto margin
An auto margin absorbs available space on its side of an item, allowing flexible separation or centering in a layout.
An auto margin after the logo consumes spare width and keeps navigation links at the other end.
Auto-fill
Auto-fill is an auto-repeat keyword that creates as many tracks as fit and keeps repeated tracks even when they are empty.
Auto-fill leaves an unused final column beside a short row of cards instead of stretching the cards into it.
Auto-fit
Auto-fit is an auto-repeat keyword that collapses empty repeated tracks and their gaps after items have been placed.
Auto-fit lets three flexible card columns expand when a fourth generated column has no item anywhere in it.
Auto-placement
Auto-placement assigns unpositioned grid items to available cells, normally in source order across rows, adding tracks as needed.
Auto-placement puts the seventh card into a third row when the grid has three columns.
Automatic minimum size
An automatic minimum size can keep a flex item or grid track large enough for unbreakable content, preventing expected shrinking.
The automatic minimum size of a long unbreakable code line can push the flex item holding it wider than its available share.

B

Baseline alignment
Baseline alignment positions text-bearing items so their text baselines line up despite differences in font size or box height.
Baseline alignment makes a small toolbar label line up naturally beside a larger heading.
Block box
A block box starts a new block line and normally fills the available width when its width is automatic.
A paragraph remains a block box even when its width is reduced to 280px.
Block formatting context
A block formatting context is an independent flow region that contains its floats and separates its margins from child margins.
When a profile box holds a floated photo, its block formatting context makes its background extend around that photo.

C

Ch unit
The ch unit measures the width of the digit 0 in the current font, giving an approximate character-based limit for text widths.
Using the ch unit for a 65ch text column limits its measure without counting the actual letters.
Clear
The clear property moves a normal-flow box below earlier floats on the selected side or on both sides.
Setting clear to both makes the next section heading start below the article’s floated image.
Clearfix
A clearfix is an older float-containment technique that adds a clearing pseudo-element after a container’s content.
A legacy stylesheet uses a clearfix so the next section does not rise beside a row of floated boxes.
Computed value
A computed value is the resolved CSS value shown by developer tools, useful for checking the rules actually affecting an element.
The computed value of display shows that the parent became flex even though an older rule declared block.
Containing block
A containing block is the reference rectangle used to calculate a box’s sizes or offsets under the applicable layout rules.
The relative card supplies the absolute badge’s containing block at the card’s padding edges.
Content alignment
Content alignment positions the grid’s tracks or flex lines as a group inside their container when there is spare space.
Content alignment moves three fixed columns toward the grid container’s center as one group.
Content column
A content column is a width-limited region for readable page content, often centered with auto inline margins and side padding.
The centered content column stops expanding on wide screens while retaining padding on narrow screens.
Cross axis
The cross axis is the direction perpendicular to a flex container’s main axis, used for item and line cross alignment.
In an English flex row, the cross axis runs vertically, so align-items can center short controls.

D

Dense packing
Dense packing backfills earlier holes in a grid with later smaller items, potentially changing their visible sequence.
Dense packing can move a small gallery image into a hole above an earlier large image.
Display none
Display none is a display setting that removes an element and its descendants from layout, leaving no reserved box space.
Using display none for a closed message removes the gap that the visible message would occupy.
Dynamic viewport height
Dynamic viewport height is the changing visible viewport height represented by dvh as browser controls appear or disappear.
Dynamic viewport height helps a full-screen mobile section fit while the address bar is visible.

E

Explicit grid
The explicit grid consists of the tracks and boundaries defined by the grid template, including its named template areas.
Line -1 marks the end of the explicit grid, even when placement creates more tracks beyond it.

F

Fixed positioning
Fixed positioning removes a box from flow and normally positions it relative to the viewport as the document scrolls.
Fixed positioning keeps the help button at the window corner unless a qualifying ancestor changes its reference.
Flex container
A flex container is a box whose flex display type arranges its direct children as flex items along a main axis.
The navigation becomes a flex container, while its nested link list can have its own layout.
Flex item
A flex item is a direct in-flow child box participating in its parent’s flex layout, with flexible sizing and alignment rules.
Each card is a flex item of the outer row and can also arrange its own contents as a column.
Flex shorthand
The flex shorthand combines grow, shrink and basis, in that order, to describe an item’s flexible main-axis sizing.
The flex shorthand 0 0 180px gives a sidebar a 180px basis without growing or shrinking.
Flex wrapping
Flex wrapping allows items to move onto additional flex lines when the current line cannot fit their starting sizes.
Flex wrapping lets a tag list grow onto a second line while each line shares its own space.
Flex-basis
The flex-basis property supplies a flex item’s starting main-axis size before growth or shrinking distributes space.
A 180px flex-basis supplies the card’s starting width even when its width property is 140px.
Flex-grow
The flex-grow property gives an item a relative share of positive free space after flex bases and gaps are accounted for.
A flex-grow value of 2 gives a card twice the added space of a neighboring card with a value of 1.
Flex-shrink
The flex-shrink property controls space loss using a weight equal to the shrink factor multiplied by the item’s base size.
Two equal flex-shrink factors can remove different pixel amounts when the cards have different bases.
Flexbox
Flexbox is a layout method that shares space among items along a main row or column, with each wrapped line sized independently.
Flexbox lets a toolbar’s short and long controls share one row before wrapping when space is limited.
Float
A float is a box moved to one side of a container so following inline content can wrap around it.
A left float lets the article’s opening lines run beside the portrait before returning to full width.
Flow-root
Flow-root is a display value that creates a block box with its own independent flow region, containing floated children.
Flow-root on a profile wrapper that holds a floated photo keeps the wrapper’s border around that photo.
Form grid
A form grid uses shared tracks for form content, such as one column for labels and another for their fields.
A form grid aligns the fields even when one label is much longer than the labels in other rows.
Fr unit
The fr unit represents a share of the grid space left after fixed tracks and gaps, subject to the tracks’ sizing constraints.
A column using the fr unit receives flexible room after the sidebar and column gaps are subtracted.
Free space
Free space is the main-axis space left after the flex items’ starting sizes and gaps have been subtracted from the container.
A toolbar with 120px left after its bases and gaps are counted gives that free space to its growing controls.
Full bleed
Full bleed describes content that spans beyond a narrower text column to fill the available width of the surrounding layout.
A photo uses full bleed across the article grid while the paragraphs stay in its middle track.

G

Gap
The gap property sets spacing between layout items or tracks; it does not create padding around the container’s outside.
A 12px gap keeps the toolbar controls apart without adding 12px around the toolbar’s outer edges.
Grid area
A grid area is a rectangular region of one or more grid cells that can hold or bound the placement of an item.
In a dashboard laid out as a grid, a chart’s grid area spans three columns and two rows.
Grid cell
A grid cell is the space where one grid row crosses one grid column, bounded by their surrounding lines.
A caption and an image can share one grid cell to create an overlay without absolute positioning.
Grid container
A grid container is a box whose grid display type arranges its direct children using shared row and column tracks.
The dashboard’s grid container defines columns that remain aligned across several rows of panels.
Grid item
A grid item is a direct in-flow child box of a grid container, placed automatically or explicitly into grid tracks.
A card is a grid item even when it uses its own flex layout to arrange its title and button.
Grid line
A grid line is a boundary between or around grid tracks, identified by a number or name for placing items.
Grid line 4 is the end boundary when an item spans the first three columns from line 1.
Grid track
A grid track is one row or column bounded by two neighboring grid lines, with a size controlled by the grid’s sizing rules.
A fixed 180px grid track holds the sidebar while the adjacent fractional track takes the remainder.

I

Implicit grid
The implicit grid consists of additional tracks generated when placement needs space beyond the explicitly defined tracks.
The product list adds implicit grid rows as new cards arrive without changing its column template.
Inline box
An inline box participates in a text line, wrapping with the text instead of taking a whole block line.
The link is an inline box inside the sentence, so the surrounding words share its line.
Inline direction
The inline direction is the direction text runs within a line, such as left to right in English or right to left in Arabic.
An auto margin at the inline direction’s start moves to the right side when the text is Arabic.
Inline-block
An inline-block is a box that stays in an inline line while accepting width, height and vertical box spacing.
A navigation link styled as an inline-block can have a larger padded click target.
Inner display type
The inner display type controls how a box arranges its own children, using a method such as normal flow, flexbox or grid.
Changing a panel’s inner display type to grid arranges its direct children in tracks.
Input group
An input group combines a field with related controls, often using a flexible input and a naturally sized button in one row.
The search input group lets the field shrink while keeping its submit button inside the container.
Inset
The inset shorthand sets the top, right, bottom and left positional offsets in one declaration.
An automatically sized absolute overlay with inset set to zero fills its containing block.
Isolation
Isolation is a CSS control whose isolate value creates a local stacking context without changing the box’s opacity or position.
Isolation keeps a card’s negative decorative layer above its own background and below its text.
Item alignment
Item alignment positions each flex or grid item within the space allocated to it, such as a grid area or flex line.
Grid item alignment can center a narrow card inside its track without moving the track itself.

J

Justify-content
The justify-content property distributes remaining main-axis space in flexbox, or aligns grid columns along the inline axis.
Justify-content centers a flex group only when sizing and auto margins leave room around it.
Justify-self
The justify-self property aligns one grid item on the inline axis within its own area; it does not align individual flex items.
Justify-self set to end moves a grid badge to its area’s inline end without affecting neighboring items.

L

Layout overlay
A layout overlay is a developer-tools drawing that reveals a grid’s tracks and lines or a flex container’s item arrangement.
The layout overlay reveals a main flex item that has grown wider than its share beside a sidebar.

M

Main axis
The main axis is the direction in which a flex container lays out its items, determined by flex-direction.
With a column direction, the main axis runs vertically and justify-content shares spare height.
Margin collapsing
Margin collapsing combines certain touching vertical block margins into a single margin in normal flow.
Margin collapsing makes a 20px bottom margin and a 36px top margin leave a 36px gap.
Media object
A media object is a component pattern with an image beside flexible text, such as a profile, comment or product row.
The media object keeps a 64px avatar beside a message while the message uses the remaining width.
Minmax
The minmax function gives a grid track a minimum and maximum size, combining a lower limit with room to grow.
Minmax with a zero minimum lets an equal column stay narrow while its long text wraps inside it.
Multiple-column layout
Multiple-column layout continues one content flow through successive text columns, using a count or a preferred column width.
Multiple-column layout lets a long glossary continue down its first column before entering the second.

N

Named area
A named area is a rectangular grid region described in template-area strings and assigned to an item with grid-area.
In a two-column template, the header named area repeats across both top-row cells so the header spans the page.
Named line
A named line is a grid boundary with a label, either declared in a track list or generated from a named area.
The named line main-start lets another panel share the start boundary of the main content area.
Nested layout
A nested layout is an inner arrangement whose container is itself an item in an outer layout, allowing methods to be combined.
The page grid and each card’s flex column form a nested layout with separate outer and inner jobs.
Normal flow
Normal flow is the browser’s default arrangement of boxes in source order, with blocks stacking and inline content wrapping into lines.
In normal flow, adding another paragraph moves the later paragraphs down as its text grows.

O

One-dimensional layout
A one-dimensional layout arranges items primarily along one axis, with wrapped lines handled independently as in flexbox.
A wrapping toolbar uses a one-dimensional layout because each row shares only its own spare width.
Outer display type
The outer display type controls how a generated box participates in its parent’s layout, such as block or inline placement.
An inline-flex button has an inline outer display type, so it can sit within a line of text.
Overflow
Overflow is content extending beyond a box’s allocated area, which may be visible, clipped or made scrollable by CSS.
The long code line causes horizontal overflow that can be kept inside a locally scrolling code block.
Overlay
An overlay is a box painted over another element, created with overlapping grid areas or positioned layers.
The image caption forms an overlay by sharing the image’s cell and appearing later in the HTML.

P

Place-items
The place-items shorthand sets align-items and justify-items together, making two-axis item alignment concise in grid.
Place-items set to center centers a short welcome box within the hero’s grid area.
Positioned element
A positioned element has a computed position value other than static, making positional offsets applicable to it.
A relatively positioned element can supply the reference box for a badge without moving itself.

R

Reflow
Reflow is content adapting to a narrower viewing width so it remains usable without unnecessary scrolling in two directions.
The reflow check at 320 CSS pixels exposes a fixed-width panel that pushes the page sideways.
Relative positioning
Relative positioning offsets where a box is drawn while keeping its original space in normal flow.
Relative positioning moves the label downward without moving the paragraph that follows it.

S

Scroll-padding-top
The scroll-padding-top property reserves top clearance in a scrolling box’s target viewing area, useful beneath fixed headers.
Scroll-padding-top on the page helps a section link bring its heading into view below the navigation bar.
Span
A span in grid placement specifies how many tracks an item crosses from a boundary or an automatically chosen location.
The declaration grid-column: 2 / span 3 gives the panel a span of three column tracks.
Stacking context
A stacking context is a group of boxes painted as one unit when compared with other groups in the surrounding context.
The header’s stacking context stays below main even though its menu has a very large internal level.
Stacking order
Stacking order is the order in which overlapping boxes are painted, determining which visible parts appear in front.
The later caption appears above the image until a z-index rule changes their stacking order.
Sticky footer pattern
A sticky footer pattern keeps a footer at the bottom of a short page while allowing it to follow longer content in normal flow.
The sticky footer pattern uses a flexible main row instead of taking the footer out of flow.
Sticky positioning
Sticky positioning keeps a box in flow, then constrains it at an offset threshold while it moves within its containing block.
Sticky positioning keeps table headings visible until their table reaches the end of its scrolling region.
Subgrid
Subgrid lets a nested grid adopt its parent’s tracks on an axis so inner parts can align across neighboring components.
Row subgrid gives neighboring cards shared title, description and footer boundaries.

T

Two-dimensional layout
A two-dimensional layout controls rows and columns together so items can share boundaries across both directions, as in grid.
A dashboard needs a two-dimensional layout to align panels across rows as well as down columns.

V

Viewport
The viewport is the visible window area through which a document is viewed, normally used as the reference for fixed positioning.
The back-to-top control stays 18px from the viewport’s lower edge while the document scrolls.
Visibility hidden
Visibility hidden is a visibility setting that hides a box while preserving the space that box occupies in the layout.
Visibility hidden reserves room for the status message, so revealing it need not move later text.

Z

Z-index
The z-index property sets a stacking level for a positioned box or a flex or grid item within its stacking context.
A high z-index on a dropdown still cannot lift it above a higher sibling of its ancestor context.

About this lesson

By StudeliReport a mistake

Make your own Study Materials

Turn your notes, slides and documents into Study Guides, Practice Questions, Flashcards and 6 more tools.

Get Started