CSS Layout Basics


CSS Layout Basics Interview with follow-up questions

1. Can you explain the CSS box model and its components?

The CSS box model describes how every HTML element is rendered as a rectangular box made up of four nested areas, from innermost to outermost:

  • Content — the area where text, images, or child elements appear. Its dimensions are set by width and height.
  • Padding — transparent space between the content and the border. Set with padding, padding-top, padding-right, padding-bottom, padding-left. Padding is part of the element's background area — the background color/image extends through it.
  • Border — a line that wraps around the padding and content. Set with border, border-width, border-style, border-color. Borders take up space in the layout (unless outline is used, which does not).
  • Margin — transparent space outside the border that separates the element from neighboring elements. Set with margin. Margins do not inherit the background — they are always transparent.

box-sizing — the most important gotcha:

By default, browsers use box-sizing: content-box, where width and height apply only to the content area. Padding and border are added on top:

total width = width + padding-left + padding-right + border-left + border-right

With box-sizing: border-box, width and height include padding and border. This is far more intuitive for layouts. It is standard practice to apply it globally:

*, *::before, *::after {
  box-sizing: border-box;
}

Every modern CSS reset and framework (including Tailwind, Bootstrap, and the browser's own UA stylesheet for many elements like `) usesborder-box`.

Margin collapsing: Vertical margins between adjacent block-level elements collapse — the larger margin wins and the smaller is absorbed. This only happens vertically (not horizontally) and does not happen inside flex or grid containers. It is a frequent source of confusion in layouts:

  • Two siblings: the gap between them is max(margin-bottom, margin-top), not their sum.
  • A parent and its first/last child: if there is no border, padding, or BFC between them, the child's margin collapses into the parent's margin.

Outline vs border: outline does not participate in the box model — it does not affect layout or the element's dimensions. It is rendered outside the border and is used for focus indicators without disrupting spacing.

width: auto vs width: 100%: auto on a block element fills the available space minus its own margins. 100% fills the parent's content box and does not account for the element's own padding or border (in content-box mode), potentially causing overflow.

↑ Back to top

Follow-up 1

How does the box model impact layout design?

The box model is crucial for layout design as it determines how elements are positioned and sized on a web page. By understanding the box model, you can control the spacing and alignment of elements.

The box model impacts layout design in the following ways:

  • Spacing: The margin property controls the space between elements, allowing you to create gaps or separate elements from each other.
  • Alignment: The padding property can be used to create space between the content and the border, allowing you to align elements within their containers.
  • Sizing: The width and height properties, along with the padding and border, determine the overall size of an element. By adjusting these properties, you can control the size of elements on the page.

By understanding and utilizing the box model, you can create visually appealing and well-structured layouts.

Follow-up 2

What is the difference between content-box and border-box in CSS?

In CSS, there are two main box-sizing values: content-box and border-box.

  • content-box: This is the default value for the box-sizing property. With content-box, the width and height properties only apply to the content area of an element. The padding, border, and margin are added to the width and height, resulting in a larger total size.

  • border-box: With border-box, the width and height properties include the content, padding, and border. The margin is still added to the total size. This means that the element's total size remains constant, regardless of the padding and border values.

The border-box value is often preferred for layout design as it simplifies calculations and ensures that the overall size of an element remains consistent.

Follow-up 3

How can you change the box model for a specific element?

To change the box model for a specific element, you can use the box-sizing property in CSS. The box-sizing property accepts two values: content-box and border-box.

Here's an example of how to change the box model for a specific element:

.element {
  box-sizing: border-box;
}

In this example, the box-sizing property is set to border-box for the .element class. This means that the width and height properties of the element will include the content, padding, and border.

By changing the box-sizing property, you can control how the size of an element is calculated and ensure consistent layout design.

2. What are the different values for the display property in CSS?

The display property controls how an element generates boxes in the layout. In 2026, the full two-value syntax is widely supported and clarifies what was always happening internally.

Block-level values:

  • block — generates a block-level box: takes full available width, starts on a new line, respects width, height, vertical margins. Examples: <div>, <p>, </p> <h1>.
  • flow-root — like block but creates a new block formatting context (BFC). Useful for containing floats without clearfix hacks.

Inline values:

  • inline — generates an inline box: does not start a new line, width is content-driven, vertical width/height are ignored, vertical margins do not affect layout. Examples: <span>, <a>, <strong>.
  • inline-block — renders inline (does not break to a new line) but accepts width, height, and all box model properties. Examples: <img>, ,.

Layout containers:

  • flex — block-level flex container; children become flex items.
  • inline-flex — same as flex but the container itself is inline.
  • grid — block-level grid container; children become grid items.
  • inline-grid — same as grid but the container itself is inline.

Table values:

  • table, table-row, table-cell, table-caption, etc. — make elements behave like HTML table elements. Rarely used directly today but useful for equal-height columns in niche situations.

Special values:

  • none — removes the element from the layout entirely (unlike visibility: hidden, which hides it but preserves the space). Descendant elements also disappear and cannot receive focus or be found by assistive technologies.
  • contents — the element itself generates no box, but its children are rendered as if they were the element's children in the parent's context. Useful for wrappers that should not interfere with a grid or flex layout.
  • list-item — renders the element like a <li>, including a list marker box.

Two-value syntax (modern CSS): CSS Display Level 3 formalizes a two-keyword model that separates the outer display type (how the element participates in its parent's layout) from the inner display type (what layout context it creates for its children). block = block flow, inline = inline flow, flex = block flex, inline-flex = inline flex. Single-keyword values still work and map to the two-value equivalents.

Common interview gotchas:

  • display: none removes from accessibility tree; visibility: hidden hides visually but keeps the element in the tree and in the layout flow.
  • You cannot apply width or height to a pure inline element — use inline-block or change the display.
  • display: contents has an accessibility bug in some older browsers where it removes the element's semantic role from the accessibility tree even though the content is visible.
  • flex and grid containers block margin collapsing inside them, which changes spacing behavior compared to normal flow.
↑ Back to top

Follow-up 1

How does the display property affect the layout of elements?

The display property in CSS affects the layout of elements by determining how elements are rendered and how they interact with other elements. It controls whether an element is block-level or inline-level, which affects how it is positioned and how it interacts with other elements.

For example, the block value makes an element a block-level element, which means it takes up the full width available and starts on a new line. This can be useful for creating sections or divisions on a webpage.

On the other hand, the inline value makes an element an inline-level element, which means it does not start on a new line and only takes up as much width as necessary. This can be useful for elements like spans or links that should appear inline with other text.

The inline-block value combines the characteristics of both inline and block. It allows an element to be inline-level, but also allows for setting a width and height. This can be useful for creating elements that need to be positioned inline, but also require a specific size.

Follow-up 2

What is the difference between inline, block, and inline-block display values?

The difference between the inline, block, and inline-block display values in CSS are as follows:

  • block: This value makes an element a block-level element, meaning it takes up the full width available and starts on a new line.

  • inline: This value makes an element an inline-level element, meaning it does not start on a new line and only takes up as much width as necessary.

  • inline-block: This value combines the characteristics of both inline and block. It allows an element to be inline-level, but also allows for setting a width and height.

The main difference between block and inline is the layout behavior. Block-level elements take up the full width available and start on a new line, while inline-level elements do not start on a new line and only take up as much width as necessary.

The difference between inline and inline-block is that inline-block allows for setting a width and height, while inline does not. This means that inline-block elements can have a specific size, while inline elements are limited to their content size.

Follow-up 3

Can you give an example of when you might use each display value?

Sure! Here are some examples of when you might use each display value:

  • block: You might use the block value when you want to create sections or divisions on a webpage that take up the full width available and start on a new line. For example, you could use it to create a header or a footer.

  • inline: You might use the inline value when you want elements to appear inline with other text, such as links or spans. For example, you could use it to create a navigation menu.

  • inline-block: You might use the inline-block value when you want elements to be positioned inline, but also require a specific size. For example, you could use it to create a button or a form input.

  • none: You might use the none value when you want to hide an element and remove it from the layout flow. For example, you could use it to hide a dropdown menu until it is triggered.

  • flex: You might use the flex value when you want to create a flexible layout with items that can grow or shrink to fit the available space. For example, you could use it to create a responsive grid.

  • grid: You might use the grid value when you want to create a grid layout with rows and columns. For example, you could use it to create a product listing with multiple items.

  • table: You might use the table value when you want to create a table-like layout. For example, you could use it to display tabular data.

  • inline-table: You might use the inline-table value when you want to create an inline-level table element. For example, you could use it to display a small table within a paragraph of text.

3. What is the role of padding and margin in CSS layouts?

Padding and margin both create space around an element, but they serve different purposes and behave differently within the box model.

Padding is the space between an element's content and its border. It is interior spacing:

  • It is part of the element's clickable/interactive area.
  • It is covered by the element's background color or image.
  • It cannot be negative.
  • It does not collapse — two adjacent elements' paddings always add up.

Margin is the space outside the element's border. It is exterior spacing used to push elements apart from their neighbors:

  • It is always transparent (never covered by background).
  • It can be negative — negative margins pull elements closer together or even overlap them.
  • Vertical margins between block-level elements collapse (the larger margin wins).
  • margin: auto on a block element with a set width centers it horizontally; it is also used in flexbox/grid for auto-fill remaining space.

When to use each:

Use padding when you need space inside a component — for example, giving a button breathing room around its text, or spacing content away from a card's edge. The background and border encompass the padding, so it stays visually part of the element.

Use margin when you need space between two separate components — for example, vertical rhythm between paragraphs, or gaps between cards in a list. Margin affects how elements are separated from each other in the layout flow.

Key behavioral differences:

  • In flex and grid containers, margin: auto absorbs all available free space in the relevant axis, which is a powerful centering and distribution technique.
  • Margins collapse between adjacent block boxes in normal flow; padding never collapses.
  • Percentage padding and margin values are always calculated relative to the parent element's width — including vertical padding. This is how the intrinsic aspect-ratio padding hack worked before aspect-ratio was supported natively.
  • box-sizing: border-box makes width/height include padding and border, but never margin. Margin is always additive.

The gap property in flex/grid: In modern layouts, gap (formerly grid-gap) is preferred over margins for spacing between flex or grid items. It is more predictable, does not add space at the outer edges, and is easier to reason about than individual item margins.

↑ Back to top

Follow-up 1

Can you give an example of when you might use padding vs margin?

Sure! Let's say you have a container element with a background color and you want to create some space between the content and the border. In this case, you would use padding. On the other hand, if you want to create space between two adjacent elements, you would use margin. For example, if you have a list of items and you want to add some space between each item, you would apply margin to the items.

Follow-up 2

How do padding and margin affect the size of an element?

Padding and margin do not directly affect the size of an element. However, they can indirectly affect the size by increasing or decreasing the space around the element. For example, adding padding to an element will increase its total size, while adding margin will increase the space between the element and its neighboring elements.

Follow-up 3

What is the difference between padding and margin?

The main difference between padding and margin is that padding is used to create space within an element, while margin is used to create space outside an element. Padding is the space between the content of an element and its border, while margin is the space between an element and its neighboring elements.

4. How do you center an element horizontally and vertically using CSS?

Centering is one of the most frequently asked CSS interview topics. In 2026, there are several solid approaches — the right one depends on context.

1. Flexbox (recommended for most cases):

.container {
  display: flex;
  justify-content: center; /* horizontal */
  align-items: center;     /* vertical */
  min-height: 100vh;       /* ensure the container has height */
}

Works for any child element, handles wrapping, and is easy to extend. No knowledge of the child's dimensions required.

2. CSS Grid with place-items:

.container {
  display: grid;
  place-items: center; /* shorthand for align-items + justify-items */
  min-height: 100vh;
}

place-items: center is the most concise centering solution available. Also works with place-content: center when centering multiple items as a group.

3. Absolute positioning with transform:

.container { position: relative; }
.element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Useful when the element must be removed from normal flow or overlaid. Works without knowing the element's dimensions. A common gotcha: the containing block must have position set to anything other than static.

4. Absolute positioning with inset: 0 and margin: auto:

.container { position: relative; }
.element {
  position: absolute;
  inset: 0;        /* shorthand for top/right/bottom/left: 0 */
  margin: auto;
  width: fit-content;
  height: fit-content;
}

Requires the element to have explicit or intrinsic dimensions. Clean and readable with modern inset shorthand.

5. Centering text horizontally:

.element {
  text-align: center;
}

Only centers inline content (text, inline elements) within the block — does not center the block element itself.

6. Block-level horizontal centering:

.element {
  width: 800px;   /* or max-width */
  margin: 0 auto;
}

Classic pattern for page containers. Only works horizontally; does not vertically center.

Common interview gotchas:

  • margin: auto only works horizontally for block elements in normal flow. In flex/grid, it works in both axes and absorbs all available free space.
  • The transform approach centers visually but the layout position (top/left: 50%) still takes up space in the stacking context.
  • For vertically centering single-line text within a box, line-height equal to the element's height is an old trick but fragile — prefer flexbox.
  • place-items is a shorthand for align-items and justify-items — knowing the difference between justify-items (for grid) and justify-content (for both) is a common follow-up question.
↑ Back to top

Follow-up 1

What are some different methods for centering an element?

Some different methods for centering an element include:

  1. Using Flexbox:
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
  1. Using Grid:
.container {
  display: grid;
  place-items: center;
}
  1. Using absolute positioning and transform:
.container {
  position: relative;
}

.element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
  1. Using table-cell and vertical-align:
.container {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
  1. Using margin:auto:
.container {
  position: relative;
}

.element {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

Follow-up 2

What challenges might you encounter when trying to center an element?

When trying to center an element, some challenges you might encounter include:

  1. Different browser rendering: Different browsers may interpret CSS rules differently, which can affect the centering of elements.

  2. Parent container constraints: If the parent container has a fixed width or height, it may affect the centering of elements.

  3. Content size: If the content inside the element is larger than the element itself, it may affect the centering.

  4. Responsive design: Centering elements may become more challenging when dealing with responsive layouts and different screen sizes.

  5. Compatibility with older browsers: Some centering methods may not be supported in older browsers, requiring fallback solutions.

Follow-up 3

How does centering an element differ in a flex container versus a block container?

Centering an element differs in a flex container versus a block container in the following ways:

  1. Flex container:

In a flex container, you can use the justify-content and align-items properties to center elements horizontally and vertically respectively. This provides a more flexible and powerful way to center elements, especially when dealing with multiple elements.

Example:

.container {
  display: flex;
  justify-content: center; /* center horizontally */
  align-items: center; /* center vertically */
}
  1. Block container:

In a block container, you can use different methods like absolute positioning, table-cell, or margin:auto to center elements. These methods are more suitable for centering a single element.

Example using absolute positioning and transform:

.container {
  position: relative;
}

.element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

5. What is the purpose of the position property in CSS?

The position property controls how an element is positioned in the document and whether it participates in normal document flow. There are five values:

static (default): The element is placed according to normal document flow. top, right, bottom, and left have no effect. This is the default for every element.

relative: The element stays in normal flow (occupies its original space), but can be offset using top, right, bottom, left relative to where it would normally appear. It also establishes a containing block for absolutely positioned descendants.

absolute: The element is removed from normal flow (no space reserved for it). It is positioned relative to its nearest ancestor with a position other than static (the containing block). If none exists, it positions relative to the initial containing block (the viewport). Use top, right, bottom, left to place it.

fixed: Like absolute but positioned relative to the viewport, not any ancestor. It stays in place during scrolling. Common for sticky navbars, modals, and floating buttons. One gotcha: a parent with transform, filter, or perspective applied turns that parent into the containing block for fixed children, breaking the fixed behavior.

sticky: A hybrid. The element behaves as relative until it reaches a defined scroll threshold, then it becomes fixed within its scroll container. The element still occupies space in normal flow.

sticky only works if the containing element has overflow that allows scrolling and the element is not at the very edge of the scroll container. The top, right, bottom, or left value is required to specify when it sticks.

Stacking context and z-index: Positioned elements (any value other than static) can use z-index to control stacking order. A higher z-index appears on top of a lower one, but only within the same stacking context. Elements that create their own stacking context (via transform, opacity &lt; 1, filter, will-change, isolation: isolate, etc.) are compared as a unit against sibling stacking contexts, regardless of their internal z-index values. This is the root cause of most z-index debugging headaches.

Common interview gotchas:

  • position: absolute without a positioned ancestor will position the element relative to the document root, often an unintended result. Always pair it with position: relative on the parent.
  • z-index only works on positioned elements — setting z-index on a static element has no effect.
  • position: sticky silently fails if any ancestor has overflow: hidden or overflow: auto, because the element has no scroll container to stick within.
  • position: fixed breaks inside a transformed ancestor — a known "bug" that is actually per-spec behavior.
↑ Back to top

Follow-up 1

What are the different values for the position property and how do they behave?

The different values for the position property in CSS are:

  1. static: This is the default value and it positions an element according to the normal flow of the document. It is not affected by the top, right, bottom, and left properties.

  2. relative: This value positions an element relative to its normal position. It can be moved using the top, right, bottom, and left properties. Other elements on the page will not be affected by the relative positioning of this element.

  3. absolute: This value positions an element relative to its nearest positioned ancestor. If there is no positioned ancestor, it is positioned relative to the initial containing block. It can be moved using the top, right, bottom, and left properties. Other elements on the page may be affected by the absolute positioning of this element.

  4. fixed: This value positions an element relative to the browser window. It remains fixed in its position even when the page is scrolled. It can be moved using the top, right, bottom, and left properties. Other elements on the page will not be affected by the fixed positioning of this element.

  5. sticky: This value positions an element based on the user's scroll position. It is similar to relative positioning, but it becomes fixed once the element reaches a specified threshold. It can be moved using the top, right, bottom, and left properties. Other elements on the page may be affected by the sticky positioning of this element.

Follow-up 2

How does the position property interact with the top, right, bottom, and left properties?

The top, right, bottom, and left properties are used to specify the position of an element when its position property is set to relative, absolute, fixed, or sticky.

  • When the position property is set to relative, the top, right, bottom, and left properties move the element relative to its normal position.

  • When the position property is set to absolute, the top, right, bottom, and left properties move the element relative to its nearest positioned ancestor.

  • When the position property is set to fixed, the top, right, bottom, and left properties move the element relative to the browser window.

  • When the position property is set to sticky, the top, right, bottom, and left properties move the element based on the user's scroll position.

Follow-up 3

Can you give an example of when you might use each position value?

Sure! Here are some examples:

  • Use static positioning when you want an element to follow the normal flow of the document.

  • Use relative positioning when you want to move an element relative to its normal position, without affecting other elements on the page.

  • Use absolute positioning when you want to position an element relative to its nearest positioned ancestor, or the initial containing block if there is no positioned ancestor.

  • Use fixed positioning when you want an element to remain fixed in its position even when the page is scrolled.

  • Use sticky positioning when you want an element to be positioned based on the user's scroll position, becoming fixed once it reaches a specified threshold.

Live mock interview

Mock interview: CSS Layout Basics

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.