learn2kode.in

Most Important CSS Interview Questions and Answers

Looking for the most important CSS interview questions? Here are the top 25 CSS interview questions and answers covering selectors, box model, Flexbox, Grid, media queries, animations, and more. Perfect for beginners and experienced developers.

1. What is CSS?

CSS (Cascading Style Sheets) is a styling language used to control the appearance of HTML elements. It defines layout, colors, fonts, spacing, responsiveness, and overall presentation of a web page.

  • Inline CSS – written inside HTML tags

  • Internal CSS – placed inside <style> within <head>

  • External CSS – separate .css file linked using <link>

The box model represents how elements are displayed. It includes:

  • Content

  • Padding

  • Border

  • Margin

It helps determine spacing and layout behavior.

  • Class: Reusable, used for multiple elements.

  • ID: Must be unique, used for a single element.

    Specificity defines which rule takes priority. Order from low to high:

    1. Element selectors

    2. Class, pseudo-class

    3. ID selectors

    4. Inline styles

    5. !important overrides all

Pseudo-classes define a special state of an element, e.g.:

  • :hover

  • :focus

  • :active

  • :visited

Pseudo-elements style specific parts of an element:

  • ::before

  • ::after

  • ::first-letter

  • ::selection

  • display: none → Element removed from layout

  • visibility: hidden → Element occupies space but is invisible

 Flexbox is a layout module designed for arranging items in one dimension (row or column). It provides easy alignment, spacing, and responsiveness.

Grid is a 2-dimensional layout system allowing precise control of rows and columns for advanced page layouts.

  • Flexbox: One-dimensional layout

  • Grid: Two-dimensional layout (rows + columns)

Media queries make websites responsive by applying styles based on screen size, device type, or resolution.

@media (max-width: 768px) {
  body { font-size: 16px; }
}

 z-index controls the stacking order of overlapping elements. Higher values appear above lower ones.

Defines how an element is placed:

  • static

  • relative

  • absolute

  • fixed

  • sticky

  • relative: Moves based on its normal position

  • absolute: Moves relative to the nearest positioned parent

 Reusable custom properties defined using --variable-name.

:root {
  --main-color: #3498db;
}

Originally used to wrap text around images; later used for layouts before Flexbox and Grid.

  • Margin: Space outside the element

  • Padding: Space inside the element around content

Transitions animate changes in a property smoothly over time.

button {
  transition: background 0.3s ease;
}

Animations allow keyframes-based movement or style changes without JavaScript.

Controls extra content behavior:

  • visible

  • hidden

  • scroll

  • auto

Used to perform dynamic calculations:

  • em: Relative to parent font-size

  • rem: Relative to root (html) font-size

Controls how images or videos fill their container:

margin: 0 auto;
width: fit-content;