
CSS (Cascading Style Sheets) is a crucial language used to style HTML elements on web pages. Below is a detailed list of the top 50 CSS interview questions and answers, ideal for freshers and experienced developers. These are structured to help improve your understanding and assist your site’s SEO and Google visibility.
Basics of CSS
1. What is CSS?
CSS stands for Cascading Style Sheets. It describes how HTML elements are displayed on screen, paper, or in other media. CSS allows separation of content from design.
2. What are the different types of CSS?
- Inline CSS: Written directly inside an HTML element using the
styleattribute. - Internal CSS: Defined within a
<style>tag inside the<head>section. - External CSS: Written in a separate
.cssfile and linked with the<link>tag.
3. What is the syntax of CSS?
selector {
property: value;
}
Example:
h1 {
color: blue;
font-size: 20px;
}
4. What is the difference between class and ID selectors in CSS?
- ID Selector: Uses
#idand is unique per element. - Class Selector: Uses
.classand can be reused across multiple elements.
5. What is the Box Model in CSS?
The box model consists of:
- Content
- Padding
- Border
- Margin It determines how the size of an element is calculated.
6. What is specificity in CSS?
Specificity is a hierarchy used to determine which CSS rule is applied when multiple rules target the same element.
7. How do you apply styles to multiple selectors?
Use commas:
h1, h2, p {
color: green;
}
8. What is the difference between em and rem units?
em: Relative to the font-size of the parent.rem: Relative to the root element (html).
9. What is a pseudo-class in CSS?
A pseudo-class defines the special state of an element. Example:
a:hover {
color: red;
}
10. What is a pseudo-element?
It targets part of an element. Example:
p::first-line {
font-weight: bold;
}
Intermediate CSS
11. What is the difference between relative, absolute, and fixed positioning?
relative: Positioned relative to its normal position.absolute: Positioned relative to the nearest positioned ancestor.fixed: Positioned relative to the viewport.
12. What is z-index in CSS?
It defines the stack order of overlapping elements. Higher values appear on top.
13. How do you create a responsive layout in CSS?
- Use media queries.
- Use flexible grid or Flexbox.
14. What is the difference between visibility: hidden and display: none?
visibility: hiddenhides the element but keeps space.display: noneremoves the element from layout.
15. What are media queries?
Media queries apply styles based on screen size or device. Example:
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
16. How do you center an element horizontally and vertically?
Using Flexbox:
div {
display: flex;
justify-content: center;
align-items: center;
}
17. What is Flexbox in CSS?
A layout model for arranging elements in a one-dimensional space, either in a row or column.
18. What is Grid in CSS?
CSS Grid is a two-dimensional layout system for the web. Example:
.container {
display: grid;
grid-template-columns: 1fr 2fr;
}
19. How do you use transitions in CSS?
Transitions allow property changes over time:
div {
transition: all 0.3s ease-in-out;
}
20. What is the difference between min-width, max-width, and width?
max-width: Maximum limit.
width: Fixed size.
min-width: Minimum limit.
Advanced CSS Questions (21–50)
21. What are CSS variables?
CSS variables (custom properties) let you store values for reuse throughout a document:
:root {
--main-color: #3498db;
}
div {
color: var(--main-color);
}
22. What is the difference between nth-child() and nth-of-type()?
nth-child()selects elements based on their position among all children.nth-of-type()selects based on the type of element only.
23. How does inherit, initial, and unset work in CSS?
inherit: Takes value from parent.initial: Resets to default browser value.unset: Acts asinheritfor inherited properties, otherwiseinitial.
24. What is the difference between auto, scroll, and hidden in overflow?
auto: Scrollbar appears only when needed.scroll: Always shows scrollbar.hidden: Cuts off overflowing content.
25. What are keyframes in CSS animation?
Defines intermediate steps in an animation sequence:
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
26. How do you use calc() in CSS?
It allows calculations:
width: calc(100% - 50px);
27. What is the use of clamp() in CSS?clamp() provides a responsive value between a defined min and max:
font-size: clamp(14px, 2vw, 18px);
28. What are pseudo-elements vs pseudo-classes?
- Pseudo-class:
:hover,:focus– define a state. - Pseudo-element:
::before,::after– insert content or modify part of an element.
29. How to make a website SEO-friendly with CSS?
- Avoid hiding content with
display: none. - Use semantic HTML and CSS for accessibility.
- Ensure responsiveness with media queries.
30. What is the use of will-change property?
Hints browsers about expected changes, improving performance:
div {
will-change: transform;
}
31. What is layering in CSS and how is it controlled?
Layering is controlled via z-index. Elements with higher z-index values sit above lower ones.
32. How do you apply styles dynamically?
Using JavaScript, or frameworks like React, or via pseudo-classes such as :hover, :checked, etc.
33. What is object-fit used for?
Used for controlling how content (e.g. images or videos) fits in a container:
img {
object-fit: cover;
}
34. How to hide an element visually but keep it accessible to screen readers?
.hidden {
position: absolute;
left: -9999px;
}
35. What are combinators in CSS?
Combinators define relationships between selectors:
- Descendant ()
- Child (
>) - Adjacent sibling (
+) - General sibling (
~)
36. What is the content property used for?
Used with pseudo-elements (::before, ::after) to insert content:
div::before {
content: "👉";
}
37. What is stacking context in CSS?
A stacking context is a three-dimensional conceptualization of HTML elements along the z-axis. It’s triggered by properties like z-index, opacity, or position.
38. What is the difference between inline, block, and inline-block?
inline: Doesn’t allow width/height.block: Takes full width.inline-block: Allows width/height and stays inline.
39. How do you make a fixed aspect ratio box?
Use padding-top with percentage:
.aspect-box {
width: 100%;
padding-top: 56.25%;
position: relative;
}
.aspect-box > * {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
}
40. What are logical properties in CSS?
Logical properties like margin-inline-start, padding-block-end support international layouts (LTR/RTL).
41. How does position: sticky work?
It toggles between relative and fixed based on scroll position:
header {
position: sticky;
top: 0;
}
42. What is the backface-visibility property?
Controls visibility of the backside of transformed elements:
div {
backface-visibility: hidden;
}
43. What is the difference between opacity and visibility?
opacity: 0makes element transparent but clickable.visibility: hiddenhides it but still takes space.
44. How do you create a CSS-only dropdown menu?
Using :hover:
nav li:hover ul {
display: block;
}
45. What is filter in CSS?
Applies graphical effects like blur or brightness:
img {
filter: grayscale(100%);
}
46. What is the difference between @import and <link>?
@import: Can be used in CSS files, slightly slower.<link>: Recommended for external stylesheets.
47. What is a media feature in media queries?
Examples: min-width, orientation, resolution. They help target devices.
48. What is a fallback font in CSS?
Fonts listed after the primary font in case it isn’t available:
font-family: "Roboto", "Arial", sans-serif;
49. How do you prevent a website from zooming on mobile?
Use the viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
50. What are best practices for writing clean CSS?
- Use semantic class names.
- Organize with comments.
- Avoid
!important. - Use shorthand properties.
- Optimize for reusability.
#CSS #WebDevelopment #FrontendDeveloper #CSSInterviewQuestions #CSS3 #WebDesign #LearnToCode #CodeNewbie #CSSTips #CodingInterview




Leave a comment