DevAcademy
All interview questions
92+ Questions

CSS Interview Questions & Answers

Curated CSS interview questions with detailed answers, grouped by topic. Search, revise and get interview-ready.

Filter by difficulty:

CSS Introduction

Q1. What does CSS stand for, and what does it do?

Cascading Style Sheets. It styles HTML — controlling colors, fonts, spacing, layout, and animation — without changing the underlying content or structure.

Q2. What are the two main parts of a CSS rule?

A selector, which determines which elements are targeted, and a declaration block, which contains the property/value pairs to apply to them.

Q3. What does "cascading" mean in CSS?

IntermediateLearn topic →

It describes how CSS resolves conflicts when multiple rules target the same element — based on source order, specificity, and importance — rather than the last rule always simply overwriting the others.

CSS Setup

Q4. What are the three ways to add CSS to a page?

Inline (a style attribute on an element), internal (a <style> block in the document), and external (a linked .css file).

Q5. Why are external stylesheets preferred for real projects?

They are cached by the browser, keep styles consistent across many pages, and cleanly separate content (HTML) from presentation (CSS).

Q6. Why are inline styles generally discouraged?

IntermediateLearn topic →

They mix content with presentation, are hard to override due to high specificity, and can’t be reused across elements or cached separately from the HTML.

CSS Syntax

Q7. What separates a property from its value, and what ends a declaration?

A colon separates property and value (e.g. color: red), and a semicolon ends each declaration.

Q8. What happens if you forget a semicolon between declarations?

IntermediateLearn topic →

The next declaration gets merged into the value of the previous one, silently breaking both — always terminate every declaration with a semicolon.

Q9. How do you write a comment in CSS?

Between /* and */. Comments can span multiple lines and are ignored entirely by the browser.

CSS Selectors

Q10. What is the difference between a class selector and an ID selector?

A class selector (.name) can match many elements and is reusable; an ID selector (#name) should match only one unique element per page and carries much higher specificity.

Q11. What does an attribute selector like [type="email"] match?

IntermediateLearn topic →

Every element that has a matching attribute and value — useful for styling form inputs by type without adding extra classes.

Q12. How do you apply the same rule to multiple selectors at once?

By grouping them with commas, e.g. h1, h2, h3 { color: #222; }, avoiding repeated declaration blocks.

CSS Colors

Q13. What are the main ways to specify a color in CSS?

Named colors (tomato), hex (#ff6347), rgb()/rgba(), and hsl()/hsla() — all interchangeable wherever a color value is expected.

Q14. What does the alpha value in rgba() or hsla() control?

Transparency, from 0 (fully transparent) to 1 (fully opaque), letting you layer a semi-transparent color over other content.

Q15. Why do some developers prefer HSL over hex or RGB?

IntermediateLearn topic →

HSL describes color the way humans think about it — hue, saturation, lightness — making it far easier to create consistent variations, like a slightly darker hover state, than guessing new hex values.

Units & Values

Q16. What is the difference between em and rem?

IntermediateLearn topic →

em is relative to the current element’s own font size (and compounds with nesting), while rem is always relative to the root <html> element’s font size, making it more predictable.

Q17. What do vw and vh represent?

1vw is 1% of the viewport width and 1vh is 1% of the viewport height, useful for elements that should scale with the browser window.

Q18. Why is rem often recommended for font sizes over px?

rem scales with the user’s root font size setting, which matters for accessibility — users who increase their browser’s default font size for readability will see rem-based text scale accordingly, unlike fixed px values.

The Box Model

Q19. What are the four layers of the CSS box model?

From innermost to outermost: content, padding, border, and margin.

Q20. With the default box-sizing, does padding increase an element’s total rendered width?

IntermediateLearn topic →

Yes — with the default content-box sizing, width only refers to the content area, so padding and border are added on top of it, increasing the total rendered size.

Q21. What does box-sizing: border-box change?

IntermediateLearn topic →

It makes the specified width/height include padding and border, so the element’s total rendered size matches the value you set — a very common global reset.

Text & Fonts

Q22. Why should a font-family list always end with a generic fallback?

IntermediateLearn topic →

If none of the listed fonts are available on the user’s system, the browser falls back to the generic family (like sans-serif), ensuring text always renders reasonably instead of using an unpredictable default.

Q23. What does font-weight control?

The boldness of text, typically from 100 (thin) to 900 (black), with 400 being normal and 700 being bold.

Q24. What is a reasonable line-height for comfortable body text?

Roughly 1.4 to 1.6 (a unitless value, so it scales with the element’s own font size) — tighter values feel cramped, and much looser values feel disconnected.

CSS Backgrounds

Q25. What is the difference between background-size: cover and contain?

IntermediateLearn topic →

cover scales the image to fully cover the box, cropping if necessary. contain scales it to fit entirely within the box, potentially leaving empty space.

Q26. Why should you set a fallback background-color alongside a background-image?

IntermediateLearn topic →

It gives a reasonable appearance while the image is loading or if it fails to load entirely, especially important for text readability over what would otherwise be an empty background.

Q27. How do you prevent a background image from tiling?

With background-repeat: no-repeat.

Borders & Border Radius

Q28. What three things does the border shorthand combine?

Width, style, and color, in that order — e.g. border: 2px solid black.

Q29. What border-radius value turns a square element into a perfect circle?

50%, applied to an element with equal width and height.

Q30. How do you style just one side of an element’s border?

IntermediateLearn topic →

Using a directional property like border-left, border-top, border-right, or border-bottom, each accepting the same width/style/color shorthand.

Margin & Padding

Q31. What is the difference between margin and padding?

padding adds space inside an element, between its content and its border. margin adds space outside an element, separating it from its neighbors.

Q32. What does margin: 0 auto; do to a block element with a fixed width?

IntermediateLearn topic →

It centers the element horizontally within its parent, by splitting the remaining horizontal space equally between the left and right margins.

Q33. What is margin collapsing?

When adjacent vertical margins between block elements combine into a single margin equal to the larger of the two, rather than adding together. It only happens with vertical margins, never horizontal ones.

The display Property

Q34. What is the difference between display: none and visibility: hidden?

display: none removes the element entirely — it takes up no space. visibility: hidden hides it visually but still reserves its layout space.

Q35. What does display: inline-block allow that display: inline does not?

IntermediateLearn topic →

It lets the element accept width and height like a block element, while still flowing inline alongside surrounding content.

Q36. What does display: flex do to an element’s direct children?

IntermediateLearn topic →

It turns them into flex items, enabling properties like justify-content, align-items, and flex to control their alignment and sizing along a row or column.

CSS Positioning

Q37. What is the default position value, and what does it mean?

static — the element follows normal document flow, and top/right/bottom/left have no effect on it.

Q38. What is an absolutely positioned element positioned relative to?

IntermediateLearn topic →

Its nearest ancestor with a position value other than static. If no such ancestor exists, it’s positioned relative to the entire page.

Q39. How does position: sticky behave?

IntermediateLearn topic →

It behaves like relative until the page scrolls past a defined threshold (e.g. top: 0), then it behaves like fixed within its containing block.

Q40. What is the difference between fixed and absolute positioning?

fixed is always positioned relative to the viewport and stays in place while scrolling. absolute is positioned relative to the nearest positioned ancestor and scrolls with the page.

Flexbox

Q41. What does display: flex do?

It turns an element into a flex container, making its direct children flex items that can be aligned and distributed along a row or column using Flexbox properties.

Q42. What is the difference between justify-content and align-items?

IntermediateLearn topic →

justify-content aligns items along the main axis (e.g. horizontally in a row), while align-items aligns them along the cross axis (e.g. vertically in a row).

Q43. What does flex: 1 do to a flex item?

IntermediateLearn topic →

It allows the item to grow and shrink to fill the available space in the container, sharing that space proportionally with any other flexible siblings.

Q44. How do you center content both horizontally and vertically with Flexbox?

By setting display: flex, justify-content: center, and align-items: center on the container.

CSS Grid

Q45. What is the key difference between CSS Grid and Flexbox?

IntermediateLearn topic →

Grid is a two-dimensional layout system, controlling rows and columns together. Flexbox is one-dimensional, aligning items along a single row or column.

Q46. What does the fr unit represent in a grid?

IntermediateLearn topic →

A fraction of the available space in the grid container — grid-template-columns: 1fr 2fr creates two columns, the second twice as wide as the first.

Q47. What does grid-template-areas let you do?

It lets you visually sketch a layout using named regions directly in your CSS, then assign each child element to a named area with grid-area.

Overflow & Visibility

Q48. What is the difference between overflow: hidden and overflow: auto?

hidden clips extra content with no scrollbar at all. auto shows a scrollbar only when the content actually overflows its container.

Q49. How do you truncate long text with an ellipsis?

IntermediateLearn topic →

By combining white-space: nowrap, overflow: hidden, and text-overflow: ellipsis on the element.

Q50. What is the difference between overflow-x and overflow-y?

IntermediateLearn topic →

They control overflow behavior independently per axis — overflow-x for horizontal, overflow-y for vertical — useful for a horizontally scrolling carousel with hidden vertical overflow.

z-index & Stacking

Q51. Does z-index work on an element with position: static?

IntermediateLearn topic →

No — z-index only has an effect on positioned elements (relative, absolute, fixed, or sticky).

Q52. What is a stacking context, and what can create one besides z-index?

A stacking context groups elements so z-index comparisons happen within it, not globally. Besides position + z-index, properties like opacity less than 1, transform, and filter can also create a new stacking context.

Q53. Why might a child with z-index: 9999 still render behind another element?

If its parent is in a separate stacking context with a lower z-index than a competing element, the child can never escape that stacking context to appear above it, regardless of its own z-index value.

Pseudo-classes

Q54. What is a pseudo-class, and how is it written?

It targets an element in a particular state or position without needing extra markup, written with a single colon, e.g. :hover or :nth-child(2).

Q55. Why should :focus be styled in addition to :hover?

IntermediateLearn topic →

Keyboard and screen-reader users rely on visible focus indicators to know where they are on the page — :hover alone provides no feedback for non-mouse interaction.

Q56. How would you style every other row of a list for a zebra-stripe effect?

IntermediateLearn topic →

Using li:nth-child(odd) or li:nth-child(even), which targets alternating children without needing extra classes.

Pseudo-elements

Q57. How do pseudo-elements differ from pseudo-classes in syntax and purpose?

IntermediateLearn topic →

Pseudo-elements use a double colon (::before) and target a specific part of an element or insert generated content, while pseudo-classes (single colon) target an existing element in a certain state.

Q58. What property is required for ::before or ::after to render anything?

The content property — even content: "" (empty string) is valid and commonly used just to insert a styled shape.

Q59. Why should ::before/::after avoid holding essential information?

Generated content isn’t part of the real DOM and may not be reliably read by all assistive technology, so it should be reserved for purely decorative purposes.

Specificity & the Cascade

Q60. Which has higher specificity: a class selector or an ID selector?

IntermediateLearn topic →

An ID selector — IDs outweigh any number of class, attribute, or pseudo-class selectors combined, but are themselves outweighed by inline styles.

Q61. When two rules have equal specificity, which one wins?

The one that appears later in the stylesheet — this is the "cascading" part of Cascading Style Sheets.

Q62. Why should !important be used sparingly?

IntermediateLearn topic →

It overrides normal specificity almost entirely, making styles unpredictable and hard to override later — often forcing even more !important rules to work around it.

Combinators

Q63. What is the difference between the descendant combinator and the child combinator?

IntermediateLearn topic →

The descendant combinator (a space, e.g. "div p") matches any matching element nested at any depth. The child combinator (>, e.g. "div > p") matches only direct children.

Q64. What does the adjacent sibling combinator (+) select?

IntermediateLearn topic →

The single element that comes immediately after another, sharing the same parent — e.g. h2 + p selects only the paragraph directly following an h2.

Q65. When would you prefer > over a plain descendant selector?

When you specifically mean "direct children only" — it keeps styles from unintentionally leaking into deeply nested, unrelated elements as markup grows.

Responsive Design

Q66. What is a media query, and how is a basic one written?

A block of CSS that only applies when certain conditions are met, most commonly viewport width, e.g. @media (max-width: 480px) { ... }.

Q67. What does "mobile-first" mean in responsive design?

IntermediateLearn topic →

Writing base styles for small screens first, then progressively adding complexity for larger screens using min-width media queries, rather than starting from desktop and overriding down.

Q68. Why is the viewport meta tag necessary for responsive design to work correctly?

IntermediateLearn topic →

Without <meta name="viewport" content="width=device-width, initial-scale=1.0" />, mobile browsers render the page at a zoomed-out desktop width, which breaks the intent of media queries.

CSS Variables

Q69. How do you define and use a CSS custom property?

Define it with two leading dashes, typically on :root, e.g. --brand-color: #764ba2;, and reference it anywhere with var(--brand-color).

Q70. What is the key advantage of CSS variables over Sass/Less variables?

CSS variables are live in the browser at runtime — they can be read and updated dynamically (even via JavaScript), unlike preprocessor variables which are compiled away and fixed at build time.

Q71. How do you provide a fallback value for a CSS variable?

IntermediateLearn topic →

By passing a second argument to var(), e.g. var(--spacing, 10px), which is used if --spacing is not defined.

Transitions

Q72. What does the transition property do?

It smooths out a property change over a duration instead of it happening instantly, commonly combined with a state change like :hover.

Q73. What four parts make up the transition shorthand?

IntermediateLearn topic →

Property, duration, timing-function, and delay — e.g. transition: transform 0.4s ease-in-out 0.1s.

Q74. Why are transform and opacity preferred for animated properties?

They can be animated efficiently by the browser’s compositor without triggering expensive layout recalculations, unlike properties like width, height, or top.

Animations

Q75. What is the main difference between a CSS transition and a CSS animation?

IntermediateLearn topic →

A transition animates between exactly two states, usually triggered by an event. An animation, defined with @keyframes, can have multiple steps and run automatically or repeat without any trigger.

Q76. What does animation-iteration-count: infinite do?

It makes the animation loop indefinitely instead of stopping after a fixed number of cycles.

Q77. Why should non-essential animations be wrapped in prefers-reduced-motion?

Some users experience discomfort from motion due to vestibular disorders. Wrapping animations in @media (prefers-reduced-motion: no-preference) respects the OS-level accessibility setting some users enable.

Transforms

Q78. Does changing an element’s transform affect the layout of surrounding elements?

IntermediateLearn topic →

No — transform is purely visual and does not reflow surrounding content, unlike changing width, margin, or position offsets.

Q79. What does transform-origin control?

IntermediateLearn topic →

The pivot point that transforms like rotate() and scale() are calculated from — by default the center of the element.

Q80. In what order are multiple transform functions applied?

In the order they are written — e.g. transform: translateX(20px) rotate(10deg) first translates the element, then rotates it around its (already translated) origin.

Shadows & Filters

Q81. What does the inset keyword do in a box-shadow?

IntermediateLearn topic →

It makes the shadow appear inside the element’s box instead of outside, creating a "pressed in" appearance.

Q82. What is the difference between box-shadow and text-shadow?

box-shadow adds a shadow around an element’s entire box, while text-shadow applies specifically to the glyphs of text.

Q83. What kinds of effects does the filter property provide?

IntermediateLearn topic →

Graphical effects like blur(), brightness(), grayscale(), and contrast(), often used for image effects or interactive hover states.

Gradients

Q84. What is the difference between linear-gradient() and radial-gradient()?

linear-gradient() transitions colors along a straight line at a given angle, while radial-gradient() radiates outward from a center point.

Q85. How are gradients typically applied to an element?

IntermediateLearn topic →

Through background-image (or the background shorthand) — gradients are treated as images in CSS, not colors.

Q86. What does conic-gradient() do differently from the other gradient types?

It sweeps colors around a center point like a color wheel or pie chart, rather than radiating outward or moving along a line.

CSS Functions

Q87. What does calc() allow that a plain CSS value cannot?

IntermediateLearn topic →

It performs math directly in a value and can mix different units, e.g. calc(100% - 150px), which isn’t possible with a plain arithmetic value.

Q88. What does clamp(min, preferred, max) do?

It picks a value that scales with the preferred expression but never goes below min or above max — commonly used for fluid typography that scales with the viewport within safe bounds.

Q89. What is the difference between min() and max() in CSS?

IntermediateLearn topic →

min() resolves to the smallest of a list of values, and max() resolves to the largest — both evaluated live, so they respond to viewport or container size changes.

CSS Best Practices

Q90. What does BEM stand for, and what problem does it solve?

IntermediateLearn topic →

Block, Element, Modifier — a naming convention (.block__element--modifier) that keeps class names descriptive and flat, avoiding deep selector nesting and specificity conflicts.

Q91. Why is box-sizing: border-box commonly included in a CSS reset?

IntermediateLearn topic →

It makes width/height calculations include padding and border, so an element’s specified size matches its actual rendered size, avoiding a very common layout surprise.

Q92. Why is consistency more important than which specific naming convention you choose?

Any convention applied unpredictably becomes as confusing as no convention at all. Consistently applying one system lets any developer predict what a class does without hunting through the whole stylesheet.