What does the CSS display property do?
The display property controls how an element generates boxes and participates in layout. Common values include block, inline, inline-block, flex, grid, and none.
CSS interview questions covering normal flow, display, positioning, stacking, overflow, Flexbox, Grid and responsive layout techniques.
Open a question for the full answer, code and interview guidance.
The display property controls how an element generates boxes and participates in layout. Common values include block, inline, inline-block, flex, grid, and none.
display: none removes the element's generated box from normal layout, so surrounding content behaves as if the element were not present. visibility: hidden keeps the element's layout space but makes it invisible.
A block box normally starts on a new line and expands in the block direction according to its containing context. An inline box participates within a line of text, and ordinary width and height behavior differs from block layout.
Normal flow is the default layout process used when elements have not been taken into special positioning or floating schemes. Block-level content generally flows in the block direction, while inline content flows within line boxes.
The position property controls how an element is positioned and whether offset properties such as top, right, bottom, left, or logical inset properties affect it. Common values are static, relative, absolute, fixed, and sticky.
A relatively positioned element remains in normal flow, and offsets move its visual position relative to where it would normally be. It can also establish a positioning reference for absolutely positioned descendants.
A fixed-positioned element is removed from normal flow and is commonly positioned relative to the viewport, so it remains in the same viewport location while the page scrolls.
z-index controls the stacking level of positioned elements and certain other layout items within stacking contexts. A larger z-index does not automatically place an element above every element on the page because stacking contexts create independent stacking hierarchies.
A stacking context is a self-contained stacking environment. Elements inside one stacking context are ordered relative to each other, and the entire context is then stacked as one unit relative to sibling contexts.
overflow controls what happens when content exceeds an element's box in a relevant direction. Common values include visible, hidden, clip, auto, and scroll, with overflow-x and overflow-y available for individual axes.
Flexbox is a one-dimensional CSS layout system designed to arrange items along a main axis while also controlling alignment on the cross axis. It is well suited to navigation bars, toolbars, card rows, button groups, and many component layouts.
The main axis is the direction in which flex items are laid out. It is controlled primarily by flex-direction. The cross axis is perpendicular to the main axis.
justify-content distributes flex items along the main axis when there is available free space. align-items controls how items are aligned along the cross axis within the flex line.
align-items sets the default cross-axis alignment for all flex items in a flex container. align-self allows an individual flex item to override that alignment for itself.
flex-basis defines an item's initial main-size basis before free space is distributed. flex-grow determines how an item can consume positive free space, while flex-shrink determines how it can shrink when the container has insufficient space.
The flex shorthand sets flex-grow, flex-shrink, and flex-basis together. Common patterns include flex: 1 for flexible items that share available space and flex: 0 0 auto for an item that should not grow or shrink from its automatic basis.
flex-wrap controls whether flex items remain on a single flex line or can wrap onto additional lines when there is insufficient main-axis space. The default is nowrap.
gap defines spacing between rows and columns in Grid and Flexbox layouts without requiring margins on individual children. row-gap and column-gap can be set independently.
CSS Grid is a two-dimensional layout system designed to control rows and columns together. It is particularly effective for page regions, dashboards, galleries, card grids, and layouts where items need explicit relationships across both dimensions.
Flexbox is primarily one-dimensional: it lays items out along one main axis at a time. Grid is two-dimensional and can coordinate both rows and columns.
grid-template-columns defines the columns in the explicit grid, while grid-template-rows defines its rows. Track sizes can use fixed lengths, percentages, fractional units, minmax(), repeat(), and other sizing functions.
The fr unit represents a fraction of the available space in a grid container after relevant fixed sizes, gaps, and sizing constraints are considered. It makes proportional track sizing concise.
repeat() avoids repeating identical track definitions. minmax() defines a minimum and maximum track size. auto-fit or auto-fill can create as many repeated tracks as fit in the available space.
Both auto-fit and auto-fill repeat tracks according to available space. auto-fill can preserve empty generated tracks when there is room for more columns, while auto-fit collapses empty repeated tracks so existing items can expand into the available space.
The best centering technique depends on the layout context. Flexbox can center along both axes with justify-content and align-items, while Grid can use place-items: center. Horizontal block centering can often use auto inline margins when a suitable width or max-width is defined.