Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

CSS Interview Questions for Freshers (With Answers)

The CSS interview questions freshers get asked most, with short, clear answers and examples: box model, specificity, position, Flexbox vs Grid, units, media queries and more.

jiocoder TeamUpdated Oct 7, 20267 min readCSS
CSS Interview Questions for Freshers (With Answers)

After HTML, interviewers move to CSS to see whether you can actually build and fix layouts. Here are the 25 CSS questions freshers are asked most, with short answers you can say out loud and code where it helps.

How to use this list

Open a CodePen or a blank HTML file and try each example. Being able to say "I tried this and here is what happened" makes your answers much stronger. For the full theory, read our CSS complete guide.

CSS basics#

1. What is CSS and why do we use it?#

CSS (Cascading Style Sheets) describes how HTML elements are displayed: colours, fonts, spacing and layout. It separates presentation from content, so one stylesheet can style a whole website and changes happen in one place.

2. What are the ways to add CSS to a page?#

Inline (the style attribute), internal (a <style> tag in the head) and external (a .css file linked with <link>). External is best for real projects because the browser caches it and it keeps code organised.

3. What does "cascading" mean?#

When several rules target the same element and property, the browser uses the cascade to choose one. It checks importance (!important), then specificity, then source order (the later rule wins).

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

A class (.btn) can be used on many elements, and an element can have many classes. An ID (#header) must be unique on the page. IDs have much higher specificity, so we style with classes and keep IDs for anchors and JavaScript.

5. What is specificity and how is it calculated?#

Specificity decides which selector wins. Count it as four columns: inline styles, IDs, classes / attributes / pseudo-classes, elements / pseudo-elements. Compare from left to right.

CSS
.menu a        /* 0-0-1-1 */
#nav .menu a   /* 0-1-1-1  wins */

Inline styles beat both, and !important beats everything, which is why you should avoid it.

6. Which properties are inherited?#

Text-related properties such as color, font-family, font-size and line-height are inherited. Box properties such as margin, padding, border, width and background are not.

Box model and display#

7. Explain the CSS box model.#

Every element is a box with four layers: content, padding (space inside the border), border and margin (space outside the border). The total space an element takes depends on all four.

8. What is the difference between content-box and border-box?#

With content-box (the default), width covers only the content, so padding and border make the box bigger. With border-box, width includes padding and border. Most projects set * { box-sizing: border-box; } so sizes are predictable.

9. What is the difference between margin and padding?#

Padding is inside the border and shows the element's background. Margin is outside the border, is always transparent and can be negative. Vertical margins between blocks can collapse; padding never does.

10. What is margin collapsing?#

When two vertical margins meet in normal block flow, they merge into one margin equal to the larger value. A 30px bottom margin and a 20px top margin produce 30px, not 50px. It does not happen in Flexbox or Grid containers.

11. What is the difference between display: none, visibility: hidden and opacity: 0?#

Takes up space?Clickable?Read by screen readers?
display: noneNoNoNo
visibility: hiddenYesNoNo
opacity: 0YesYesYes

12. What is the difference between inline, block and inline-block?#

Block elements start on a new line and fill the width. Inline elements flow with text and ignore width and height. Inline-block flows with text but respects width, height and vertical padding and margin, which is useful for buttons and badges.

Positioning#

13. Explain the values of the position property.#

CSS position values compared: static, relative, absolute, fixed and sticky
  • static: the default; normal flow, offsets do nothing.
  • relative: moved from its normal place, but its original space is kept.
  • absolute: removed from flow and placed relative to the nearest positioned ancestor (or the page if there is none).
  • fixed: placed relative to the viewport and stays visible while scrolling.
  • sticky: behaves like relative until you scroll past a threshold, then sticks within its parent.

14. How do you position a badge in the top-right corner of a card?#

CSS
.card  { position: relative; }
.badge { position: absolute; top: 8px; right: 8px; }

The relative parent becomes the reference point for the absolutely positioned child.

15. What is z-index and why does it sometimes not work?#

z-index controls which element appears on top. It works only on positioned elements and flex or grid items. It can also seem to "not work" because the element is inside a stacking context created by a parent with transform, opacity below 1 or its own z-index; a child cannot rise above its parent's context.

Flexbox and Grid#

16. What is Flexbox?#

Flexbox is a one-dimensional layout system for arranging items in a row or column. The container controls alignment with justify-content (main axis), align-items (cross axis), flex-wrap and gap. Items control sizing with flex-grow, flex-shrink and flex-basis.

17. How do you centre a div horizontally and vertically?#

CSS
/* Flexbox */
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Grid, even shorter */
.parent {
  display: grid;
  place-items: center;
}

For horizontal centring only, a block with a set width can use margin: 0 auto.

18. What does flex: 1 mean?#

It is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. Every item with flex: 1 grows to share the free space equally, which is handy for equal-width columns.

19. What is the difference between Flexbox and Grid?#

Flexbox vs CSS Grid: one-dimensional rows versus two-dimensional rows and columns

Flexbox is one-dimensional: it handles a row or a column, and content size drives the layout. Grid is two-dimensional: it controls rows and columns together, and the layout drives the content. Use Flexbox for navbars and button groups, and Grid for page layouts and card galleries.

20. How do you make a responsive card grid without media queries?#

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

The browser fits as many 220px-or-wider columns as it can and reflows them as the screen changes.

Units and responsive design#

21. What is the difference between px, em and rem?#

px is a fixed size. em is relative to the element's own font size, so it compounds when nested. rem is relative to the root (html) font size, so it is consistent everywhere and respects the user's browser font setting. Use rem for font sizes and spacing.

22. What are media queries?#

Media queries apply CSS only when conditions are true, such as screen width or a dark-mode preference.

CSS
@media (min-width: 768px) {
  .sidebar { display: block; }
}
@media (prefers-color-scheme: dark) {
  body { background: #0f172a; color: #e2e8f0; }
}

23. What is mobile-first design?#

You write the base styles for small screens, then add min-width media queries to enhance the layout on bigger screens. It keeps mobile CSS simple and fast, and most traffic today is on phones.

Pseudo-classes, variables and more#

24. What is the difference between a pseudo-class and a pseudo-element?#

A pseudo-class (single colon) selects an element in a particular state or position, like :hover, :focus or :nth-child(2). A pseudo-element (double colon) styles a part of an element or adds content, like ::before, ::after or ::first-line.

25. What are CSS variables (custom properties)?#

Reusable values defined with --name and read with var(). Unlike Sass variables, they work in the browser at runtime and follow the cascade, so they are perfect for themes and dark mode.

CSS
:root { --primary: #2563eb; }
.btn  { background: var(--primary); }

Tips for answering CSS questions in interviews#

  • Draw it. For the box model, positioning or Flexbox axes, a quick sketch explains more than words.
  • Give a real use case: "I used position: sticky for the table header in my project."
  • Mention trade-offs, such as why you avoid !important or ID selectors.
  • Expect a live task, like centring a div or building a responsive navbar. Practise these until you can do them without searching.

Key takeaways#

  • The cascade checks importance, specificity and source order.
  • Specificity order: inline > ID > class > element.
  • Know the box model and why border-box is the standard.
  • Be able to explain all five position values with an example.
  • Flexbox is one-dimensional, Grid is two-dimensional.
  • Prefer rem units and a mobile-first approach.