Quick answer
CSS interviews centre on how the browser decides what a page looks like: the cascade and specificity, the box model and box-sizing, and layout with Flexbox (one dimension) and Grid (two dimensions). Experienced candidates are also asked about positioning, stacking contexts, responsive design, and modern CSS such as container queries, :has(), custom properties and cascade layers.
CSS questions come up in every frontend and full-stack interview, and they range from definitions to “why is this element not centered?”. Interviewers want to see that you understand how the browser decides what a page looks like: the cascade, specificity, the box model and layout. This guide covers the questions asked most often, with short answers and code, including the modern CSS features interviewers increasingly ask about.
CSS Interview Questions and Answers for Freshers

1. What is CSS?
CSS, Cascading Style Sheets, is the language that describes how HTML is presented: colors, fonts, spacing, layout, and animation. HTML provides the structure and content, and CSS provides the appearance. It is a standard maintained by the World Wide Web Consortium (W3C).
2. Why separate CSS from HTML?
Keeping presentation separate from content means one stylesheet can style a whole site, so a design change happens in one place. It also keeps HTML cleaner and more meaningful, lets browsers cache the stylesheet across pages, and makes it possible to present the same content differently, for example for print or for small screens.
3. What is the latest version of CSS?
There is no “CSS4” or “CSS5”. After CSS 2.1, CSS was split into separate modules, such as Selectors, Flexbox, Grid and Color, each developing at its own pace with its own level number. “CSS3” is an informal name for that modular era. The W3C publishes a yearly snapshot of which modules are stable, and in practice you check whether a feature is supported in the browsers you need, using a site such as Can I Use or MDN’s Baseline status.
4. What are the ways to add CSS to a page?
- External stylesheet: a separate
.cssfile linked in the page’s head. The usual approach, because it can be shared and cached. - Internal (embedded) stylesheet: CSS inside a
styleelement in the page’s head. It applies to that page only. - Inline styles: CSS in an element’s
styleattribute. It applies to that element only and is hard to maintain.
<!-- External -->
<link rel="stylesheet" href="styles.css">
<!-- Internal -->
<style>
h1 { color: navy; }
</style>
<!-- Inline -->
<h1 style="color: navy;">Hello</h1>
5. What does “cascading” mean?
Several rules often apply to the same element and conflict. The cascade is the algorithm that decides which one wins. In order, it considers:
- Origin and importance: author styles beat browser defaults, and
!importantdeclarations beat normal ones. - Cascade layers: with
@layer, styles in later layers beat earlier ones. - Specificity: a more specific selector wins (see question 6).
- Order: if all else is equal, the rule that appears last wins.
Separately, some properties, such as color and font-family, are inherited from parent elements when no rule sets them.
6. What is specificity?
Specificity is a score that decides which selector wins when rules conflict. It is compared in three columns, from most to least important:
- IDs:
#header - Classes, attributes and pseudo-classes:
.button,[type="text"],:hover - Elements and pseudo-elements:
p,::before
So #nav a (1, 0, 1) beats .menu .item a (0, 2, 1), because one ID outranks any number of classes. Inline styles beat all selectors, and !important overrides everything. The universal selector * adds nothing, and :where() has zero specificity, which is useful for easy-to-override defaults.
7. What is the difference between an ID and a class?
An ID must be unique on the page, and is selected with #. A class can be used on any number of elements, and an element can have several classes. It is selected with .. For styling, prefer classes: they are reusable, and their lower specificity makes them easier to override. IDs are best kept for anchors, JavaScript hooks, and linking form labels to inputs.
#site-header { position: sticky; top: 0; }
.button { padding: 8px 16px; }
.button.primary { background: navy; color: white; }
8. How do you apply the same style to several elements?
Separate the selectors with commas:
h2, h3 {
color: darkred;
}
9. What is the box model?
Every element is a rectangular box made of four layers, from the inside out: the content, the padding around it, the border, and the margin outside the border. The element’s background extends under the padding and border, but not the margin.
10. What is the difference between padding and margin?
Padding is the space inside the border, between the border and the content, and it shows the element’s background. Margin is the space outside the border, between the element and its neighbors, and it is always transparent. Vertical margins between adjacent block elements can also collapse into one, while padding never collapses.
11. What does box-sizing do?
By default (content-box), width sets only the content width, so padding and border are added on top, and a 300px box with 20px padding is really 340px wide. With border-box, width includes padding and border, which is far easier to reason about. Most projects set it everywhere:
*, *::before, *::after {
box-sizing: border-box;
}
12. What are the margin and padding properties and values?
Each side can be set separately: margin-top, margin-right, margin-bottom and margin-left, and the same for padding. The shorthand sets them clockwise from the top: margin: 10px 20px 10px 20px, or margin: 10px 20px for top/bottom and left/right. Values can be lengths such as px, rem or em, percentages of the containing block’s width, or auto for margins. margin: 0 auto centers a block element that has a set width. Negative margins are allowed; negative padding is not.
13. What are the ways to specify colors?
- Keywords:
red,rebeccapurple,transparent. - Hex:
#ff6600, the short form#f60, or with alpha#ff660080. Not case-sensitive. - rgb(): red, green and blue from 0 to 255, such as
rgb(255 102 0), with optional alpha:rgb(255 102 0 / 50%). - hsl(): hue (0 to 360 degrees), saturation and lightness, such as
hsl(24 100% 50%). Easier for adjusting shades by hand. - Modern spaces:
oklch()andoklab()give perceptually even colors, andcolor-mix()blends two colors.
14. What are the main units in CSS?
- px: a fixed CSS pixel.
- em: relative to the element’s font size, or to the parent’s when used on
font-sizeitself, so it compounds when nested. - rem: relative to the root element’s font size. Predictable, and respects the user’s browser font setting, so it is the usual choice for font sizes and spacing.
- %: relative to a property of the parent, usually its width.
- vw, vh: 1% of the viewport width or height.
dvhaccounts for mobile browser toolbars appearing and disappearing.
15. What is the difference between display: block, inline and inline-block?
A block element starts on a new line and takes the full width available, and width, height and vertical margins apply, as with div and p. An inline element flows within a line of text and ignores width, height and vertical margins, as with span and a. Inline-block flows inline but accepts width, height and margins like a block. display: none removes the element from the layout entirely, while visibility: hidden hides it but keeps its space.
16. What are the position values?
- static: the default. The element sits in the normal flow, and
topandlefthave no effect. - relative: stays in the flow, but can be offset from its normal position. Also makes it the reference point for absolutely positioned children.
- absolute: removed from the flow, and positioned relative to the nearest positioned ancestor.
- fixed: removed from the flow, and positioned relative to the viewport, so it stays put when the page scrolls.
- sticky: behaves like relative until the page scrolls to a threshold, such as
top: 0, then sticks like fixed within its container.
17. What are pseudo-classes and pseudo-elements?
A pseudo-class, written with one colon, selects an element in a particular state or position: :hover, :focus-visible, :checked, :first-child, :nth-child(odd). A pseudo-element, written with two colons, styles part of an element, or adds content that is not in the HTML: ::before, ::after, ::first-letter, ::placeholder.
a:hover { text-decoration: underline; }
.required::after { content: " *"; color: red; }
18. What are the most popular CSS frameworks?
Tailwind CSS, a utility-first framework where you compose styles from small classes in the HTML, and Bootstrap, which provides ready-made components and a grid, are the most widely used. Others include Foundation, Bulma and UIkit. Interviewers often ask about trade-offs: frameworks speed up development and keep designs consistent, but can add unused CSS and make sites look alike.
CSS Interview Questions for Experienced Developers

19. What is the difference between Flexbox and Grid?
Flexbox lays items out in one dimension, a row or a column, and is ideal for distributing space among items, such as a navigation bar or buttons in a toolbar. Grid lays items out in two dimensions, rows and columns at once, and is ideal for page layouts and card grids. A common rule: use Grid when you want to define the layout and place items in it, and Flexbox when you want the content to decide the layout. They are often combined.
20. How do you center an element horizontally and vertically?
The simplest modern ways are with Grid or Flexbox on the parent:
.parent {
display: grid;
place-items: center;
}
/* or */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
For an absolutely positioned element, use top: 50%; left: 50%; transform: translate(-50%, -50%).
21. How do Flexbox’s grow, shrink and basis work?
flex-basis sets an item’s starting size. flex-grow says how much of any spare space it takes, compared with its siblings. flex-shrink says how much it shrinks when there is not enough space. flex: 1 is shorthand for grow 1, shrink 1, basis 0, which makes items share space equally.
22. What is the difference between fr, auto-fill and auto-fit in Grid?
fr is a fraction of the remaining free space in the grid. repeat(auto-fill, minmax(200px, 1fr)) creates as many columns as fit, keeping empty tracks if there are few items. auto-fit does the same but collapses empty tracks, so the items stretch to fill the row. This one line gives a responsive card grid with no media queries:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 24px;
}
23. What is responsive design, and how do media queries work?
Responsive design makes a layout adapt to the screen it is viewed on, rather than building separate mobile and desktop sites. It combines fluid layouts with Flexbox and Grid, flexible images, relative units, and media queries, which apply styles only when a condition is true. Writing the mobile styles first and adding larger-screen styles with min-width queries is the common approach. Pages also need the viewport meta tag to scale correctly on phones.
.sidebar { display: none; }
@media (min-width: 768px) {
.sidebar { display: block; }
}
24. What are media types?
Media types target categories of device: all, the default, screen, and print. Older types such as tv, handheld and speech are deprecated. The most practical use today is a print stylesheet, for example hiding navigation and ads with @media print.
25. What are container queries?
Media queries respond to the size of the viewport. Container queries respond to the size of a component’s container, so a card can switch to a horizontal layout when its column is wide, wherever it is placed on the page. They are supported in all major browsers.
.card-wrapper { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; }
}
26. What are CSS custom properties (variables)?
Custom properties store values you can reuse, written with two dashes and read with var(). Unlike preprocessor variables, they exist at runtime: they cascade and inherit, can be changed in media queries or with JavaScript, and can be scoped to a component. They are the usual way to build themes such as dark mode.
:root {
--brand: #3140b8;
--space: 1rem;
}
.button {
background: var(--brand);
padding: var(--space);
}
@media (prefers-color-scheme: dark) {
:root { --brand: #8b96ff; }
}
27. What is z-index, and what is a stacking context?
z-index controls which positioned elements appear in front of others. It only compares elements within the same stacking context. A new stacking context is created by, among other things, an element with a position and a z-index, opacity below 1, transform, or filter. That is why a child with z-index: 9999 can still appear behind something: its parent’s whole stacking context sits behind it.
28. What does the float property do, and is it still used?
float: left or right moves an element to one side and lets text and inline content wrap around it, like a picture in a magazine article. Floats were once used for whole page layouts, which needed clearfix hacks. Today Flexbox and Grid do layout, and floats are only for wrapping text around images.
29. What is the difference between transitions and animations?
A transition animates a property smoothly from one value to another when it changes, for example on hover. An animation, defined with @keyframes, can have several steps, run on its own without a trigger, repeat, and alternate. The browser fills in the frames between keyframes, which is the idea sometimes called tweening.
.button { transition: background-color 0.2s ease; }
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.badge { animation: pulse 2s infinite; }
For smooth performance, animate transform and opacity, which the browser can handle without recalculating layout, and respect users who prefer less motion with @media (prefers-reduced-motion: reduce).
30. What are CSS sprites?
A sprite combines many small images, such as icons, into one image file, and each element shows only its part using background-position. This cut the number of HTTP requests, which mattered when each request was expensive. With HTTP/2 and inline SVG icons, sprites are much less common today, though the question still comes up.
31. What are gradients?
Gradients are images generated by the browser that blend between two or more colors. There are three kinds: linear-gradient() along a line, radial-gradient() outward from a point, and conic-gradient() around a center point, useful for pie charts. Each has a repeating version, such as repeating-linear-gradient().
32. What does the :has() selector do?
:has() selects an element based on what it contains or what follows it, which was not possible in CSS before, so it is often called the parent selector. For example, .card:has(img) styles only cards that contain an image, and form:has(:invalid) styles a form with an invalid field.
33. What are cascade layers?
@layer groups styles into named layers with a defined priority, so you control which set of styles wins regardless of selector specificity. For example, @layer reset, framework, components, utilities; means utility styles always beat component styles, even if a component selector is more specific. It is especially useful for keeping third-party CSS from overriding your own.
34. Does CSS support nesting?
Yes. Native CSS nesting is now supported in all major browsers, so you can nest rules much as in Sass, using & to refer to the parent selector:
.card {
padding: 1rem;
&:hover { box-shadow: 0 4px 12px rgb(0 0 0 / 15%); }
.title { font-weight: 700; }
}
35. How do you add icons to a page?
The most common approaches are inline SVG, which can be styled with CSS, including fill: currentColor to match the text color, an SVG sprite referenced with the use element, or an icon font library such as Font Awesome, used by adding a class to an element. Inline SVG is generally preferred today for sharpness, accessibility and performance. Icons that carry meaning need a text label for screen readers, and purely decorative ones should be hidden with aria-hidden="true".

Conclusion
Most CSS interviews come back to the same foundations: the cascade and specificity, the box model, and layout with Flexbox and Grid. Knowing modern features such as container queries, :has() and custom properties shows you keep up with how CSS is written today. If you were asked a CSS question recently that is not here, share it in the comments and we can work through the answer together.
Sources and further reading
- MDN: CSS: Mozilla’s complete CSS reference
- MDN: The cascade: how conflicting rules are resolved
- W3C CSS work: the CSS standards and their status
- Can I use: browser support for each CSS feature

Leave a Reply