CSS Interview Questions and Answers

Practice every question in this topic using expandable answers and interview tips. Answer aloud first, then open the explanation.

Back to the complete interview hub

Read question Answer aloud Review
Q1. What is CSS? [Easy]

Answer: CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation, layout, colors, fonts, spacing, and overall appearance of HTML documents.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q2. Why is CSS used in web development? [Easy]

Answer: CSS separates presentation from content, making web pages easier to maintain, more consistent, responsive, and visually appealing across different devices.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q3. What are the different ways to apply CSS to an HTML document? [Easy]

Answer: CSS can be applied using Inline CSS (style attribute), Internal CSS (style tag inside the head section), and External CSS (linked stylesheet using the link tag). External CSS is the preferred approach for maintainability.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q4. What is the CSS syntax? [Easy]

Answer: CSS syntax consists of a selector followed by a declaration block enclosed in curly braces. Each declaration contains a property and a value separated by a colon.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q5. What are CSS selectors? [Easy]

Answer: CSS selectors are patterns used to target HTML elements for styling. Common selectors include element, class, id, attribute, universal, pseudo-class, and pseudo-element selectors.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q6. What is the difference between class and ID selectors in CSS? [Medium]

Answer: A class selector can be applied to multiple elements and is prefixed with a dot (.). An ID selector uniquely identifies a single element and is prefixed with a hash (#). ID selectors have higher specificity than class selectors.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q7. What is CSS specificity? [Medium]

Answer: Specificity is the set of rules browsers use to determine which CSS declaration should be applied when multiple rules match the same element. Inline styles have the highest priority, followed by IDs, classes, attributes, pseudo-classes, and element selectors.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q8. What is the CSS Box Model? [Medium]

Answer: The CSS Box Model describes how every HTML element is represented as a rectangular box consisting of content, padding, border, and margin. It determines the total space occupied by an element.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q9. What is the difference between margin and padding? [Medium]

Answer: Margin is the space outside an element's border and separates it from other elements. Padding is the space inside the border between the content and the border, increasing the clickable and visible area.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q10. What is the difference between display: none and visibility: hidden? [Medium]

Answer: display: none removes the element from the document layout so it occupies no space. visibility: hidden hides the element but preserves its allocated space in the layout.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q11. What are the different values of the CSS position property? [Medium]

Answer: The position property has five main values: static, relative, absolute, fixed, and sticky. Each value determines how an element is positioned within the document.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q12. What is the difference between relative, absolute, fixed, and sticky positioning? [Medium]

Answer: Relative positions an element relative to its normal position. Absolute positions it relative to the nearest positioned ancestor. Fixed positions it relative to the viewport and remains visible during scrolling. Sticky behaves like relative until a specified scroll position is reached, then acts like fixed.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q13. What is the z-index property in CSS? [Medium]

Answer: The z-index property controls the stacking order of overlapping positioned elements. Elements with higher z-index values appear above those with lower values.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q14. What is Flexbox in CSS? [Medium]

Answer: Flexbox is a one-dimensional layout model used to align and distribute items efficiently within a container. It simplifies horizontal and vertical alignment and responsive layouts.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q15. What is the difference between justify-content and align-items in Flexbox? [Medium]

Answer: justify-content aligns flex items along the main axis, while align-items aligns items along the cross axis of the flex container.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q16. What is CSS Grid? [Medium]

Answer: CSS Grid is a two-dimensional layout system that allows developers to create complex layouts using rows and columns with precise control over item placement.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q17. What is the difference between CSS Grid and Flexbox? [Hard]

Answer: Flexbox is designed for one-dimensional layouts, either rows or columns. CSS Grid is intended for two-dimensional layouts, allowing simultaneous control of rows and columns. Flexbox is ideal for components, while Grid is better for complete page layouts.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q18. What is the overflow property in CSS? [Easy]

Answer: The overflow property controls how content is handled when it exceeds an element's boundaries. Common values include visible, hidden, scroll, and auto.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q19. What are CSS units? Explain absolute and relative units. [Medium]

Answer: CSS uses absolute units such as px, cm, and in, and relative units such as %, em, rem, vw, and vh. Relative units adapt better to responsive designs because they scale based on screen size or parent elements.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q20. What are the different ways to specify colors in CSS? [Easy]

Answer: Colors in CSS can be specified using color names, hexadecimal values, RGB, RGBA, HSL, HSLA, and the currentColor keyword.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q21. What are CSS Media Queries? [Medium]

Answer: CSS Media Queries allow developers to apply different styles based on device characteristics such as screen width, height, orientation, or resolution. They are the foundation of responsive web design.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q22. What is Responsive Web Design (RWD)? [Medium]

Answer: Responsive Web Design is an approach that makes web pages automatically adapt to different screen sizes and devices using flexible layouts, responsive images, and CSS media queries.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q23. What is the difference between em and rem units? [Medium]

Answer: The em unit is relative to the font size of its parent element, while the rem unit is relative to the root (html) element's font size. Using rem provides more consistent sizing across the application.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q24. What are CSS transitions? [Medium]

Answer: CSS transitions allow property values to change smoothly over a specified duration without using JavaScript. Common transition properties include transition-property, transition-duration, transition-timing-function, and transition-delay.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q25. What are CSS animations? [Medium]

Answer: CSS animations allow elements to animate through multiple stages using @keyframes. Unlike transitions, animations can run automatically, repeat multiple times, and include several intermediate states.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q26. What is the difference between CSS transitions and animations? [Medium]

Answer: Transitions animate changes between two states and are usually triggered by user interaction. Animations use @keyframes to define multiple stages, can start automatically, and can repeat infinitely without user interaction.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q27. What is the CSS transform property? [Medium]

Answer: The transform property modifies the appearance of an element without affecting the document layout. Common transformations include translate(), rotate(), scale(), skew(), and matrix().

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q28. What is the difference between translate(), rotate(), and scale()? [Medium]

Answer: translate() moves an element from its current position, rotate() rotates it around an axis, and scale() increases or decreases its size proportionally.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q29. What are CSS custom properties (CSS variables)? [Medium]

Answer: CSS custom properties, also called CSS variables, are reusable values declared using the -- prefix and accessed with the var() function. They simplify theme management and reduce code duplication.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q30. What are the calc(), min(), max(), and clamp() functions in CSS? [Hard]

Answer: calc() performs mathematical calculations using different CSS units. min() selects the smallest value, max() selects the largest value, and clamp() defines a responsive value with minimum, preferred, and maximum limits. These functions are useful for creating flexible and responsive layouts.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q31. What are pseudo-classes in CSS? [Medium]

Answer: Pseudo-classes define the special state of an HTML element. They allow styles to be applied based on user interaction or element state without adding extra classes. Examples include :hover, :focus, :active, :visited, :first-child, and :nth-child().

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q32. What are pseudo-elements in CSS? [Medium]

Answer: Pseudo-elements style a specific part of an element or insert generated content. Common pseudo-elements include ::before, ::after, ::first-letter, ::first-line, and ::selection.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q33. What is the difference between pseudo-classes and pseudo-elements? [Medium]

Answer: Pseudo-classes target an element based on its state or position, such as :hover or :focus. Pseudo-elements target or create a specific part of an element, such as ::before or ::after.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q34. What are attribute selectors in CSS? [Medium]

Answer: Attribute selectors apply styles based on the presence or value of an HTML attribute. Examples include [type], [type='text'], [href^='https'], [href$='.pdf'], and [class*='btn'].

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q35. What are CSS combinators? [Hard]

Answer: CSS combinators define relationships between selectors. The descendant selector (space) targets nested elements, child selector (>) targets direct children, adjacent sibling selector (+) targets the next sibling, and general sibling selector (~) targets all following siblings.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q36. What is CSS inheritance? [Medium]

Answer: Inheritance is the mechanism by which certain CSS properties, such as color and font-family, are automatically passed from a parent element to its child elements. Layout properties like margin and border are generally not inherited.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q37. What is the difference between inherit, initial, unset, and revert in CSS? [Medium]

Answer: inherit takes the value from the parent element. initial resets a property to its default CSS specification value. unset behaves like inherit for inheritable properties and initial for non-inheritable properties. revert restores the property's value according to the browser or user stylesheet.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q38. What is the object-fit property in CSS? [Medium]

Answer: The object-fit property controls how images or videos fit within their containers. Common values include fill, contain, cover, none, and scale-down. It is frequently used to create responsive image layouts.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q39. What is the box-sizing property and why is border-box commonly used? [Medium]

Answer: The box-sizing property determines how an element's width and height are calculated. With content-box (default), padding and border are added outside the specified size. With border-box, padding and border are included in the specified dimensions, making layouts easier to manage.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q40. What is the difference between opacity: 0 and visibility: hidden? [Medium]

Answer: opacity: 0 makes an element fully transparent but it still occupies space and can receive user interactions unless disabled. visibility: hidden hides the element while preserving its layout space, but it cannot be interacted with. Unlike display: none, both keep the element in the document flow.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q41. How can you optimize CSS for better website performance? [Medium]

Answer: CSS performance can be improved by minimizing unused CSS, combining and minifying stylesheets, reducing selector complexity, avoiding excessive nesting, using efficient layouts like Flexbox and Grid, and loading only the CSS required for the current page.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q42. What is Critical CSS? [Hard]

Answer: Critical CSS is the minimum CSS required to render the visible portion of a webpage (above-the-fold content). It is loaded first to improve page rendering speed, while the remaining CSS is loaded asynchronously.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q43. What is the BEM naming convention in CSS? [Medium]

Answer: BEM (Block, Element, Modifier) is a CSS naming methodology that improves readability and maintainability. Example: .card, .card__title, .card--active.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q44. What are Sass and SCSS? [Medium]

Answer: Sass is a CSS preprocessor that adds features such as variables, nesting, mixins, inheritance, and functions. SCSS is the newer syntax that is fully compatible with standard CSS.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q45. What are the advantages of using Sass or SCSS? [Medium]

Answer: Sass improves code organization through reusable variables, mixins, nested rules, functions, partial files, and modular architecture, making large projects easier to maintain.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q46. What are CSS Modules? [Hard]

Answer: CSS Modules locally scope CSS class names by default, preventing naming conflicts between components. They are commonly used in React applications for modular and maintainable styling.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q47. What is the Shadow DOM? [Hard]

Answer: Shadow DOM is a browser technology used in Web Components to encapsulate HTML, CSS, and JavaScript. Styles inside a Shadow DOM do not affect the main document, and external styles do not affect the component unless explicitly allowed.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q48. How do you make images responsive using CSS? [Medium]

Answer: Responsive images are commonly created by setting max-width: 100% and height: auto. The object-fit property and responsive image techniques using srcset and picture elements further improve adaptability across devices.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q49. How do you ensure CSS works consistently across different browsers? [Medium]

Answer: Cross-browser compatibility can be achieved by following web standards, testing on multiple browsers, using vendor prefixes when necessary, applying CSS resets or Normalize.css, and avoiding unsupported features without fallbacks.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Q50. What are some modern CSS best practices? [Hard]

Answer: Use semantic HTML with CSS, prefer Flexbox and Grid for layouts, write reusable class names, follow methodologies like BEM, use CSS variables for theming, minimize specificity, optimize performance, avoid !important when possible, organize styles into modules, and ensure accessibility and responsive design.

Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.

Study checklist

  1. Explain the answer without reading.
  2. Write one small example.
  3. Compare framework or database version differences in official documentation.

CSS Interview Questions and Answers

Practice one question at a time. Read the question, answer aloud, then compare your explanation with the answer.

← Back to the complete interview hub

Study method: define the concept, give a small example, and mention one practical use or limitation.

Q1. What is CSS?

Easy

Answer: CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation, layout, colors, fonts, spacing, and overall appearance of HTML documents.

Interview tip: Use a short example and connect the answer to real project work.

Q2. Why is CSS used in web development?

Easy

Answer: CSS separates presentation from content, making web pages easier to maintain, more consistent, responsive, and visually appealing across different devices.

Interview tip: Use a short example and connect the answer to real project work.

Q3. What are the different ways to apply CSS to an HTML document?

Easy

Answer: CSS can be applied using Inline CSS (style attribute), Internal CSS (style tag inside the head section), and External CSS (linked stylesheet using the link tag). External CSS is the preferred approach for maintainability.

Interview tip: Use a short example and connect the answer to real project work.

Q4. What is the CSS syntax?

Easy

Answer: CSS syntax consists of a selector followed by a declaration block enclosed in curly braces. Each declaration contains a property and a value separated by a colon.

Interview tip: Use a short example and connect the answer to real project work.

Q5. What are CSS selectors?

Easy

Answer: CSS selectors are patterns used to target HTML elements for styling. Common selectors include element, class, id, attribute, universal, pseudo-class, and pseudo-element selectors.

Interview tip: Use a short example and connect the answer to real project work.

Q6. What is the difference between class and ID selectors in CSS?

Medium

Answer: A class selector can be applied to multiple elements and is prefixed with a dot (.). An ID selector uniquely identifies a single element and is prefixed with a hash (#). ID selectors have higher specificity than class selectors.

Interview tip: Use a short example and connect the answer to real project work.

Q7. What is CSS specificity?

Medium

Answer: Specificity is the set of rules browsers use to determine which CSS declaration should be applied when multiple rules match the same element. Inline styles have the highest priority, followed by IDs, classes, attributes, pseudo-classes, and element selectors.

Interview tip: Use a short example and connect the answer to real project work.

Q8. What is the CSS Box Model?

Medium

Answer: The CSS Box Model describes how every HTML element is represented as a rectangular box consisting of content, padding, border, and margin. It determines the total space occupied by an element.

Interview tip: Use a short example and connect the answer to real project work.

Q9. What is the difference between margin and padding?

Medium

Answer: Margin is the space outside an element's border and separates it from other elements. Padding is the space inside the border between the content and the border, increasing the clickable and visible area.

Interview tip: Use a short example and connect the answer to real project work.

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

Medium

Answer: display: none removes the element from the document layout so it occupies no space. visibility: hidden hides the element but preserves its allocated space in the layout.

Interview tip: Use a short example and connect the answer to real project work.

Q11. What are the different values of the CSS position property?

Medium

Answer: The position property has five main values: static, relative, absolute, fixed, and sticky. Each value determines how an element is positioned within the document.

Interview tip: Use a short example and connect the answer to real project work.

Q12. What is the difference between relative, absolute, fixed, and sticky positioning?

Medium

Answer: Relative positions an element relative to its normal position. Absolute positions it relative to the nearest positioned ancestor. Fixed positions it relative to the viewport and remains visible during scrolling. Sticky behaves like relative until a specified scroll position is reached, then acts like fixed.

Interview tip: Use a short example and connect the answer to real project work.

Q13. What is the z-index property in CSS?

Medium

Answer: The z-index property controls the stacking order of overlapping positioned elements. Elements with higher z-index values appear above those with lower values.

Interview tip: Use a short example and connect the answer to real project work.

Q14. What is Flexbox in CSS?

Medium

Answer: Flexbox is a one-dimensional layout model used to align and distribute items efficiently within a container. It simplifies horizontal and vertical alignment and responsive layouts.

Interview tip: Use a short example and connect the answer to real project work.

Q15. What is the difference between justify-content and align-items in Flexbox?

Medium

Answer: justify-content aligns flex items along the main axis, while align-items aligns items along the cross axis of the flex container.

Interview tip: Use a short example and connect the answer to real project work.

Q16. What is CSS Grid?

Medium

Answer: CSS Grid is a two-dimensional layout system that allows developers to create complex layouts using rows and columns with precise control over item placement.

Interview tip: Use a short example and connect the answer to real project work.

Q17. What is the difference between CSS Grid and Flexbox?

Hard

Answer: Flexbox is designed for one-dimensional layouts, either rows or columns. CSS Grid is intended for two-dimensional layouts, allowing simultaneous control of rows and columns. Flexbox is ideal for components, while Grid is better for complete page layouts.

Interview tip: Use a short example and connect the answer to real project work.

Q18. What is the overflow property in CSS?

Easy

Answer: The overflow property controls how content is handled when it exceeds an element's boundaries. Common values include visible, hidden, scroll, and auto.

Interview tip: Use a short example and connect the answer to real project work.

Q19. What are CSS units? Explain absolute and relative units.

Medium

Answer: CSS uses absolute units such as px, cm, and in, and relative units such as %, em, rem, vw, and vh. Relative units adapt better to responsive designs because they scale based on screen size or parent elements.

Interview tip: Use a short example and connect the answer to real project work.

Q20. What are the different ways to specify colors in CSS?

Easy

Answer: Colors in CSS can be specified using color names, hexadecimal values, RGB, RGBA, HSL, HSLA, and the currentColor keyword.

Interview tip: Use a short example and connect the answer to real project work.

Q21. What are CSS Media Queries?

Medium

Answer: CSS Media Queries allow developers to apply different styles based on device characteristics such as screen width, height, orientation, or resolution. They are the foundation of responsive web design.

Interview tip: Use a short example and connect the answer to real project work.

Q22. What is Responsive Web Design (RWD)?

Medium

Answer: Responsive Web Design is an approach that makes web pages automatically adapt to different screen sizes and devices using flexible layouts, responsive images, and CSS media queries.

Interview tip: Use a short example and connect the answer to real project work.

Q23. What is the difference between em and rem units?

Medium

Answer: The em unit is relative to the font size of its parent element, while the rem unit is relative to the root (html) element's font size. Using rem provides more consistent sizing across the application.

Interview tip: Use a short example and connect the answer to real project work.

Q24. What are CSS transitions?

Medium

Answer: CSS transitions allow property values to change smoothly over a specified duration without using JavaScript. Common transition properties include transition-property, transition-duration, transition-timing-function, and transition-delay.

Interview tip: Use a short example and connect the answer to real project work.

Q25. What are CSS animations?

Medium

Answer: CSS animations allow elements to animate through multiple stages using @keyframes. Unlike transitions, animations can run automatically, repeat multiple times, and include several intermediate states.

Interview tip: Use a short example and connect the answer to real project work.

Q26. What is the difference between CSS transitions and animations?

Medium

Answer: Transitions animate changes between two states and are usually triggered by user interaction. Animations use @keyframes to define multiple stages, can start automatically, and can repeat infinitely without user interaction.

Interview tip: Use a short example and connect the answer to real project work.

Q27. What is the CSS transform property?

Medium

Answer: The transform property modifies the appearance of an element without affecting the document layout. Common transformations include translate(), rotate(), scale(), skew(), and matrix().

Interview tip: Use a short example and connect the answer to real project work.

Q28. What is the difference between translate(), rotate(), and scale()?

Medium

Answer: translate() moves an element from its current position, rotate() rotates it around an axis, and scale() increases or decreases its size proportionally.

Interview tip: Use a short example and connect the answer to real project work.

Q29. What are CSS custom properties (CSS variables)?

Medium

Answer: CSS custom properties, also called CSS variables, are reusable values declared using the -- prefix and accessed with the var() function. They simplify theme management and reduce code duplication.

Interview tip: Use a short example and connect the answer to real project work.

Q30. What are the calc(), min(), max(), and clamp() functions in CSS?

Hard

Answer: calc() performs mathematical calculations using different CSS units. min() selects the smallest value, max() selects the largest value, and clamp() defines a responsive value with minimum, preferred, and maximum limits. These functions are useful for creating flexible and responsive layouts.

Interview tip: Use a short example and connect the answer to real project work.

Q31. What are pseudo-classes in CSS?

Medium

Answer: Pseudo-classes define the special state of an HTML element. They allow styles to be applied based on user interaction or element state without adding extra classes. Examples include :hover, :focus, :active, :visited, :first-child, and :nth-child().

Interview tip: Use a short example and connect the answer to real project work.

Q32. What are pseudo-elements in CSS?

Medium

Answer: Pseudo-elements style a specific part of an element or insert generated content. Common pseudo-elements include ::before, ::after, ::first-letter, ::first-line, and ::selection.

Interview tip: Use a short example and connect the answer to real project work.

Q33. What is the difference between pseudo-classes and pseudo-elements?

Medium

Answer: Pseudo-classes target an element based on its state or position, such as :hover or :focus. Pseudo-elements target or create a specific part of an element, such as ::before or ::after.

Interview tip: Use a short example and connect the answer to real project work.

Q34. What are attribute selectors in CSS?

Medium

Answer: Attribute selectors apply styles based on the presence or value of an HTML attribute. Examples include [type], [type='text'], [href^='https'], [href$='.pdf'], and [class*='btn'].

Interview tip: Use a short example and connect the answer to real project work.

Q35. What are CSS combinators?

Hard

Answer: CSS combinators define relationships between selectors. The descendant selector (space) targets nested elements, child selector (>) targets direct children, adjacent sibling selector (+) targets the next sibling, and general sibling selector (~) targets all following siblings.

Interview tip: Use a short example and connect the answer to real project work.

Q36. What is CSS inheritance?

Medium

Answer: Inheritance is the mechanism by which certain CSS properties, such as color and font-family, are automatically passed from a parent element to its child elements. Layout properties like margin and border are generally not inherited.

Interview tip: Use a short example and connect the answer to real project work.

Q37. What is the difference between inherit, initial, unset, and revert in CSS?

Medium

Answer: inherit takes the value from the parent element. initial resets a property to its default CSS specification value. unset behaves like inherit for inheritable properties and initial for non-inheritable properties. revert restores the property's value according to the browser or user stylesheet.

Interview tip: Use a short example and connect the answer to real project work.

Q38. What is the object-fit property in CSS?

Medium

Answer: The object-fit property controls how images or videos fit within their containers. Common values include fill, contain, cover, none, and scale-down. It is frequently used to create responsive image layouts.

Interview tip: Use a short example and connect the answer to real project work.

Q39. What is the box-sizing property and why is border-box commonly used?

Medium

Answer: The box-sizing property determines how an element's width and height are calculated. With content-box (default), padding and border are added outside the specified size. With border-box, padding and border are included in the specified dimensions, making layouts easier to manage.

Interview tip: Use a short example and connect the answer to real project work.

Q40. What is the difference between opacity: 0 and visibility: hidden?

Medium

Answer: opacity: 0 makes an element fully transparent but it still occupies space and can receive user interactions unless disabled. visibility: hidden hides the element while preserving its layout space, but it cannot be interacted with. Unlike display: none, both keep the element in the document flow.

Interview tip: Use a short example and connect the answer to real project work.

Q41. How can you optimize CSS for better website performance?

Medium

Answer: CSS performance can be improved by minimizing unused CSS, combining and minifying stylesheets, reducing selector complexity, avoiding excessive nesting, using efficient layouts like Flexbox and Grid, and loading only the CSS required for the current page.

Interview tip: Use a short example and connect the answer to real project work.

Q42. What is Critical CSS?

Hard

Answer: Critical CSS is the minimum CSS required to render the visible portion of a webpage (above-the-fold content). It is loaded first to improve page rendering speed, while the remaining CSS is loaded asynchronously.

Interview tip: Use a short example and connect the answer to real project work.

Q43. What is the BEM naming convention in CSS?

Medium

Answer: BEM (Block, Element, Modifier) is a CSS naming methodology that improves readability and maintainability. Example: .card, .card__title, .card--active.

Interview tip: Use a short example and connect the answer to real project work.

Q44. What are Sass and SCSS?

Medium

Answer: Sass is a CSS preprocessor that adds features such as variables, nesting, mixins, inheritance, and functions. SCSS is the newer syntax that is fully compatible with standard CSS.

Interview tip: Use a short example and connect the answer to real project work.

Q45. What are the advantages of using Sass or SCSS?

Medium

Answer: Sass improves code organization through reusable variables, mixins, nested rules, functions, partial files, and modular architecture, making large projects easier to maintain.

Interview tip: Use a short example and connect the answer to real project work.

Q46. What are CSS Modules?

Hard

Answer: CSS Modules locally scope CSS class names by default, preventing naming conflicts between components. They are commonly used in React applications for modular and maintainable styling.

Interview tip: Use a short example and connect the answer to real project work.

Q47. What is the Shadow DOM?

Hard

Answer: Shadow DOM is a browser technology used in Web Components to encapsulate HTML, CSS, and JavaScript. Styles inside a Shadow DOM do not affect the main document, and external styles do not affect the component unless explicitly allowed.

Interview tip: Use a short example and connect the answer to real project work.

Q48. How do you make images responsive using CSS?

Medium

Answer: Responsive images are commonly created by setting max-width: 100% and height: auto. The object-fit property and responsive image techniques using srcset and picture elements further improve adaptability across devices.

Interview tip: Use a short example and connect the answer to real project work.

Q49. How do you ensure CSS works consistently across different browsers?

Medium

Answer: Cross-browser compatibility can be achieved by following web standards, testing on multiple browsers, using vendor prefixes when necessary, applying CSS resets or Normalize.css, and avoiding unsupported features without fallbacks.

Interview tip: Use a short example and connect the answer to real project work.

Q50. What are some modern CSS best practices?

Hard

Answer: Use semantic HTML with CSS, prefer Flexbox and Grid for layouts, write reusable class names, follow methodologies like BEM, use CSS variables for theming, minimize specificity, optimize performance, avoid !important when possible, organize styles into modules, and ensure accessibility and responsive design.

Interview tip: Use a short example and connect the answer to real project work.

Quick revision checklist

  1. Explain the answer without reading.
  2. Write one small example.
  3. Review platform or version differences in official documentation.

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.