CSS Advanced Topics
CSS Advanced Topics Interview with follow-up questions
1. Can you explain the difference between relative and absolute positioning in CSS?
CSS has five position values: static (the default), relative, absolute, fixed, and sticky. The most commonly asked comparison is between relative and absolute.
position: relative
The element stays in the normal document flow — other elements still behave as if it's in its original spot. The top, right, bottom, and left properties offset it from its natural position, but the space it occupied is preserved.
.box {
position: relative;
top: 10px; /* moves the box 10px down from where it would normally sit */
left: 20px;
}
The most important use of position: relative is establishing a positioning context for absolutely positioned descendants, without visually shifting the element itself (i.e., by leaving top/left at auto).
position: absolute
The element is removed from the normal document flow — it takes up no space and other elements act as if it doesn't exist. It's positioned relative to its nearest ancestor that has a position value other than static (a "positioned ancestor"). If no such ancestor exists, it positions relative to the initial containing block (roughly the `` element/viewport).
.parent { position: relative; }
.tooltip {
position: absolute;
top: 100%;
left: 0;
}
This is the standard pattern for dropdowns, tooltips, and overlays: make the parent position: relative and the overlay position: absolute.
Key differences:
relative |
absolute |
|
|---|---|---|
| Document flow | Stays in flow, space preserved | Removed from flow, no space |
| Offset origin | Element's natural position | Nearest positioned ancestor |
| Effect on siblings | Siblings unaffected | Siblings ignore it |
position: fixed — like absolute but always positioned relative to the viewport; stays on screen as the user scrolls. Used for sticky headers, floating buttons, modals.
position: sticky — a hybrid: behaves as relative until a scroll threshold is reached, then acts like fixed within its containing block. Used for sticky table headers and section navigation.
Common gotcha: position: absolute inside a position: relative container is the right pattern — but forgetting to add position: relative to the parent causes the child to escape and position against the wrong ancestor.
Follow-up 1
How would you use each in a practical scenario?
Relative positioning is often used to adjust the position of an element within its normal flow. For example, you can use it to move an image slightly to the right of its normal position. Absolute positioning is commonly used to create overlays, tooltips, or dropdown menus that need to be positioned precisely relative to another element. For example, you can use it to position a dropdown menu below a button when the button is clicked.
Follow-up 2
What are the potential issues that can arise from using absolute positioning?
There are a few potential issues that can arise from using absolute positioning. First, since elements with absolute positioning are removed from the normal document flow, they can overlap other elements and cause layout issues. Second, if the positioned ancestor of an absolutely positioned element is not set correctly, the element may not be positioned as intended. Third, absolute positioning can make a page less responsive to changes in content or viewport size, as the positioned elements are not automatically adjusted.
Follow-up 3
Can you explain how z-index works in relation to positioning?
The z-index property specifies the stack order of positioned elements. It only applies to elements that have a position value other than static (the default). When two or more elements overlap, the element with a higher z-index value will be displayed on top of the others. If two elements have the same z-index value, the one that appears later in the HTML markup will be displayed on top. The z-index property can be used in combination with relative or absolute positioning to control the stacking order of elements.
2. What is the purpose of the 'display' property in CSS?
The display property controls two things: whether an element generates a block or inline box (its outer display type) and how it lays out its children (its inner display type).
Common values:
block— the element takes up the full width available, starts on a new line, and respectswidth,height,margin, andpaddingon all sides. Examples:<div>,<p>, ``.inline— the element flows within text, taking only as much width as its content needs.width,height, and verticalmarginare ignored. Examples:<span>,<a>,<strong>.inline-block— flows inline like text but respectswidth,height, and allmargin/paddingvalues. Historically used for horizontal navigation; now largely replaced by Flexbox.flex— makes the element a block-level flex container. Its children become flex items and are laid out according to Flexbox rules. The most common choice for one-dimensional component layouts.inline-flex— same asflexbut the container itself is inline rather than block-level.grid— makes the element a block-level grid container. Its children are placed into grid cells. The standard choice for two-dimensional layout.inline-grid— same asgridbut the container is inline.none— removes the element from both the visual rendering and the accessibility tree. The element takes up no space and is completely hidden. This is different fromvisibility: hidden, which hides the element visually but preserves its space in the layout and keeps it in the accessibility tree.contents— the element's own box is not generated; only its children are rendered. Useful for unwrapping a container in the DOM without adding a visual wrapper.table,table-row,table-cell— makes non-table elements behave like table elements. Rarely used for new code; use Grid or Flexbox instead.
Interview follow-up — what's the difference between display: none and visibility: hidden? display: none removes the element from layout entirely; visibility: hidden hides it visually but the space is still occupied. A third option, opacity: 0, makes it invisible but keeps it interactive (focusable, clickable) — usually not what you want unless combined with pointer-events: none.
Follow-up 1
Can you provide examples of different display property values and their effects?
Certainly! Here are some examples:
'block': This value makes an element a block-level element, which means it takes up the full width available and starts on a new line.
'inline': This value makes an element an inline-level element, which means it takes up only as much width as necessary and does not start on a new line.
'inline-block': This value combines the characteristics of both 'block' and 'inline'. It takes up only as much width as necessary and starts on the same line, but allows other elements to be displayed next to it.
'flex': This value enables a flexible box layout. It allows you to easily align and distribute space among elements within a container.
These are just a few examples, and there are more display property values available in CSS.
Follow-up 2
What is the difference between 'inline' and 'block' display?
The main difference between 'inline' and 'block' display is how they affect the layout and behavior of elements.
'block': Elements with 'block' display are block-level elements. They take up the full width available and start on a new line. By default, block-level elements have a width of 100% and a height determined by their content or specified dimensions.
'inline': Elements with 'inline' display are inline-level elements. They take up only as much width as necessary and do not start on a new line. By default, inline-level elements have a width determined by their content and a height determined by the font size.
In summary, 'block' elements create a block-level box that starts on a new line, while 'inline' elements create an inline-level box that does not start on a new line.
Follow-up 3
How does 'flex' display affect the layout of elements?
The 'flex' display in CSS enables a flexible box layout. It allows you to easily align and distribute space among elements within a container. When an element is set to 'display: flex', its immediate children become flex items and are laid out in a flex container.
The 'flex' display introduces two main concepts:
Flex Container: The parent element with 'display: flex' becomes a flex container. It establishes a new flex formatting context and acts as a containing block for its flex items.
Flex Items: The children of a flex container become flex items. They can be aligned and distributed within the flex container using various flex properties such as 'justify-content', 'align-items', and 'flex-grow'.
By using the 'flex' display and its associated properties, you can create responsive and flexible layouts with ease.
3. What is the 'float' property in CSS and when would you use it?
The float property removes an element from the normal document flow and pushes it to the left or right of its container, allowing subsequent inline content to wrap around it.
img {
float: left;
margin: 0 1rem 1rem 0;
}
This is the classic use case: wrapping text around an image, like in a newspaper or magazine layout. That remains its most appropriate use.
Values: left, right, none (default), inline-start, inline-end (logical values for RTL support).
The clearfix problem — floated elements are taken out of flow, so their parent container collapses and doesn't grow to contain them. The historical solution was the "clearfix" hack: a pseudo-element with clear: both added after the floated content.
.clearfix::after {
content: "";
display: table;
clear: both;
}
The modern alternative is display: flow-root on the container, which creates a new block formatting context and automatically contains floats:
.container { display: flow-root; }
When to use float in 2026:
- Wrapping text around an image (
float: left/righton the image inside a text block) — this is still the correct semantic tool for this specific layout. - Some print stylesheet layouts.
When not to use float:
- Multi-column layouts — use Flexbox or Grid.
- Horizontal navigation bars — use Flexbox.
- Any general layout purpose — Flexbox and Grid are more predictable, require less hackery around clearing, and support alignment features float cannot replicate.
Interviewers may ask about float to test knowledge of CSS history. The correct answer is: you understand it, you know its original purpose, but you default to Flexbox or Grid for layout and only reach for float when wrapping text around content.
Follow-up 1
What are the different values that the 'float' property can take?
The 'float' property in CSS can take the following values:
- 'left': The element floats to the left side of its container.
- 'right': The element floats to the right side of its container.
- 'none': The element does not float and remains in the normal flow of the document.
- 'inherit': The element inherits the float value from its parent element.
Follow-up 2
How does 'float' interact with other elements on the page?
When an element is floated, it is taken out of the normal flow of the document. This means that other elements on the page will flow around the floated element. If there is enough space, the elements will wrap around the floated element. If there is not enough space, the elements will be pushed down below the floated element.
It's important to note that floated elements do not affect the layout of their parent element. This means that the parent element will not expand to contain the floated elements unless you use a clearfix or another method to clear the float.
Follow-up 3
Can you explain how to clear a float?
When you float an element, it is taken out of the normal flow of the document and can cause issues with the layout of other elements. To clear a float and prevent these issues, you can use the 'clear' property.
The 'clear' property specifies whether an element should be moved below any floating elements that precede it. It can take the following values:
- 'none': No clearing is applied.
- 'left': The element is moved below any left-floating elements.
- 'right': The element is moved below any right-floating elements.
- 'both': The element is moved below any left-floating and right-floating elements.
To clear a float, you can apply the 'clear' property to an element after the floated elements. For example:
.clearfix::after {
content: '';
display: table;
clear: both;
}
In the above example, the '::after' pseudo-element is used to create a clearing element that is inserted after the floated elements. The 'clear: both;' property ensures that the element is moved below any left-floating and right-floating elements.
4. Can you explain the 'z-index' property in CSS?
z-index controls the stacking order of positioned elements — which one appears in front when elements overlap. Higher values appear on top of lower values.
.modal { position: fixed; z-index: 1000; }
.overlay { position: fixed; z-index: 999; }
.tooltip { position: absolute; z-index: 100; }
Critical constraint: z-index only works on positioned elements. An element must have position set to relative, absolute, fixed, or sticky (anything other than the default static) for z-index to have any effect. Setting z-index: 9999 on a position: static element does nothing.
Stacking contexts are the key concept interviewers probe. A stacking context is an independent compositing layer. Elements inside a stacking context are stacked relative to each other, and the entire context is then stacked as a unit against other stacking contexts.
A new stacking context is created by:
- An element with
positionother thanstaticand az-indexvalue other thanauto opacityless than 1transform,filter,clip-path,perspective,will-changeisolation: isolate— explicitly creates a stacking context without other side effects- Flex and grid children with
z-indexother thanauto
The classic gotcha: you set z-index: 9999 on a modal but it still appears behind another element. The reason is almost always that one of the ancestors creates a stacking context with a lower z-index — the modal is correctly stacked within its own context, but the whole context is rendered below the other element's context. The fix is either to move the modal higher in the DOM (closer to `) or to useisolation: isolate` and control the containing stacking context.
isolation: isolate — a property specifically for creating a stacking context without other visual side effects. Useful in component libraries to prevent a component's internal z-index values from leaking out and conflicting with the host page.
Tools like browser DevTools' "layers" view can visualize stacking contexts when debugging z-index issues.
Follow-up 1
How does 'z-index' affect the stacking order of elements?
The 'z-index' property determines the stacking order of elements in the z-axis, which is the axis that represents depth on a web page. Elements with a higher 'z-index' value will be positioned in front of elements with a lower 'z-index' value. If two elements have the same 'z-index' value, the one that appears later in the HTML markup will be positioned on top.
Follow-up 2
What happens when 'z-index' is not specified?
When 'z-index' is not specified, elements are stacked in the order they appear in the HTML markup. The first element will be positioned at the bottom of the stack, and subsequent elements will be positioned on top of each other in the order they appear. This is known as the default stacking order.
Follow-up 3
Can you provide a practical example of when you would use 'z-index'?
Sure! One practical example of using 'z-index' is when creating a dropdown menu. By giving the dropdown menu a higher 'z-index' value than the rest of the page content, you can ensure that the menu appears on top of other elements when it is opened. This allows the menu to be visible and interactable, even if there are other elements on the page that would normally overlap it.
5. What is the 'visibility' property in CSS and how does it differ from 'display'?
The visibility property controls whether an element is visually rendered, while preserving (or not preserving) its space in the layout. The display property controls whether the element participates in layout at all.
visibility values:
visible(default) — element is rendered normally.hidden— element is invisible but still occupies its space in the layout. Other elements position themselves as if the hidden element were there. The element remains in the DOM and the accessibility tree.collapse— for table rows and columns: removes them from the layout without affecting the table's column widths. Behaves likehiddenon non-table elements.
display: none — the element is removed from the rendering tree entirely. It takes up no space, is not accessible to screen readers, and cannot receive focus. The page reflows as if the element doesn't exist.
Comparison:
visibility: hidden |
display: none |
|
|---|---|---|
| Takes up space | Yes | No |
| Screen readers | Still in accessibility tree | Removed |
| Focusable | No (not clickable) | No |
| Animatable | Yes (between visible and hidden) |
No (binary, can't transition) |
| Child override | Child can set visibility: visible to reappear |
No — children hidden too |
The child override behavior is a useful gotcha: a parent with visibility: hidden can have a specific child made visible again with visibility: visible. No equivalent exists for display: none.
opacity: 0 is a third option: the element is invisible but takes up space and remains interactive (clickable, focusable). This is usually undesirable unless combined with pointer-events: none and tabindex="-1".
Animation: visibility can be transitioned alongside opacity to create fade-in/out effects that also remove the element from interaction after it disappears:
.tooltip {
opacity: 0;
visibility: hidden;
transition: opacity 0.2s, visibility 0.2s;
}
.tooltip.is-visible {
opacity: 1;
visibility: visible;
}
This pattern is common in accessible tooltip and modal implementations because display: none cannot be transitioned directly (though @starting-style in modern CSS partially addresses this).
Follow-up 1
What are the different values that the 'visibility' property can take?
The 'visibility' property in CSS can take two values:
- 'visible': This is the default value. It makes the element visible and it is displayed as normal.
- 'hidden': This value hides the element, but it still takes up space in the layout.
Follow-up 2
How does 'visibility' affect the layout of other elements?
When the 'visibility' property of an element is set to 'hidden', the element is not displayed, but it still takes up space in the layout. This means that other elements will still be positioned as if the hidden element is visible. It can affect the layout of other elements, especially if they are positioned relative to the hidden element.
Follow-up 3
Can you provide a practical example of when you would use 'visibility' over 'display'?
One practical example of when you would use 'visibility' over 'display' is when you want to hide an element temporarily, but still have it take up space in the layout. This can be useful in situations where you want to toggle the visibility of an element using JavaScript, without affecting the layout of other elements. For example, you might have a dropdown menu that is hidden by default, but becomes visible when a user clicks on a button. By using the 'visibility' property instead of the 'display' property, you can hide the dropdown menu without causing other elements to shift or reflow.
Live mock interview
Mock interview: CSS Advanced Topics
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
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.