Introduction
CSS interview questions test more than memorizing properties. Interviewers often check whether you understand selectors, specificity, the box model, positioning, Flexbox, Grid, responsive design, pseudo-classes, pseudo-elements, and practical styling decisions. This chapter provides CSS interview practice questions with concise answers and examples. The questions progress from fundamental concepts to practical frontend situations, helping beginners and intermediate developers prepare for CSS interviews.
Question 1: What is CSS?
Problem Statement
Explain what CSS is and why it is used in web development.
Answer
CSS stands for Cascading Style Sheets. It is used to control the presentation and layout of web content.
CSS Example
.title {
color: blue;
font-size: 28px;
margin-bottom: 15px;
}
Explanation
CSS can control:
- Colors
- Fonts
- Spacing
- Borders
- Layouts
- Animations
- Responsive behavior
HTML provides the document structure, while CSS controls how that structure is presented.
Question 2: What Is the CSS Box Model?
Problem Statement
Explain the four main parts of the CSS box model.
Answer
The CSS box model consists of:
- Content
- Padding
- Border
- Margin
CSS Example
.card {
width: 300px;
padding: 20px;
border: 2px solid black;
margin: 15px;
}
Explanation
The content is surrounded by padding, then the border, and finally the margin.
A useful way to remember it is:
Content → Padding → Border → Margin
Question 3: What Is CSS Specificity?
Problem Statement
Explain CSS specificity and determine which rule will apply.
CSS Code
p {
color: blue;
}
.text {
color: green;
}
#main-text {
color: red;
}
Answer
The ID selector has higher specificity than the class selector and element selector.
Therefore, if the same element has the id="main-text" and class="text", the color will be red.
Explanation
A simplified specificity order is:
Inline styles > ID > Class/attribute/pseudo-class > Element/pseudo-element
Understanding specificity is important when debugging conflicting CSS rules.
Question 4: What Is the Difference Between margin and padding?
Problem Statement
Explain the difference between margin and padding.
CSS Example
.card {
margin: 20px;
padding: 15px;
}
Answer
- Margin creates space outside an element’s border.
- Padding creates space between the content and the element’s border.
Explanation
If you want to create space between two cards, margin is commonly used. If you want more space inside a card around its content, padding is commonly used.
Question 5: What Is the Difference Between display: none and visibility: hidden?
Problem Statement
Explain how display: none and visibility: hidden behave differently.
CSS Example
.hidden-one {
display: none;
}
.hidden-two {
visibility: hidden;
}
Answer
display: none removes the element from the layout.
visibility: hidden hides the element visually while its layout space remains reserved.
Explanation
This distinction is important when designing interfaces where an element may need to disappear without changing the surrounding layout.
Question 6: What Is Flexbox?
Problem Statement
Explain what Flexbox is and show a simple example.
CSS Example
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
Answer
Flexbox is a CSS layout system designed primarily for arranging elements along one dimension: a row or a column.
Explanation
Common Flexbox properties include:
display: flexflex-directionjustify-contentalign-itemsflex-wrapgap
Flexbox is particularly useful for navigation bars, buttons, cards, and horizontal or vertical component layouts.
Question 7: What Is CSS Grid?
Problem Statement
Explain CSS Grid and show how to create a three-column layout.
CSS Example
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
Answer
CSS Grid is a two-dimensional layout system that allows you to control rows and columns.
Explanation
Grid is useful for layouts where both horizontal and vertical positioning matter.
Important Grid properties include:
grid-template-columnsgrid-template-rowsgapgrid-columngrid-rowgrid-template-areas
Question 8: What Is the Difference Between relative, absolute, and fixed?
Problem Statement
Explain the difference between these three CSS positioning values.
CSS Example
.box {
position: relative;
}
.badge {
position: absolute;
top: 10px;
right: 10px;
}
.menu {
position: fixed;
top: 0;
right: 0;
}
Answer
relativekeeps the element in the normal document flow while allowing offsets to be applied.absoluteremoves the element from normal flow and positions it relative to its containing block.fixedremoves the element from normal flow and positions it relative to the viewport in typical cases.
Explanation
A common pattern is to make a parent position: relative and then position a child with position: absolute.
Question 9: What Is a CSS Pseudo-Class?
Problem Statement
Explain pseudo-classes and provide an example.
CSS Example
button:hover {
background-color: blue;
color: white;
}
Answer
A pseudo-class targets an element based on a particular state or condition.
Explanation
Common pseudo-classes include:
:hover:focus:active:checked:first-child:last-child
For example, :hover applies styles when the pointer is over an element.
Question 10: What Is a CSS Pseudo-Element?
Problem Statement
Explain pseudo-elements and show how to insert content before an element.
CSS Example
.title::before {
content: "★ ";
color: gold;
}
Answer
A pseudo-element allows you to style a specific part of an element or create generated content.
Explanation
Common pseudo-elements include:
::before::after::first-letter::first-line::selection
Here, ::before generates content before the element’s existing content.
Key Takeaways
- CSS controls the presentation and layout of web content.
- The box model consists of content, padding, border, and margin.
- Specificity determines which conflicting CSS rule has greater priority.
display: noneremoves an element from the layout, whilevisibility: hiddenkeeps its layout space.- Flexbox is primarily a one-dimensional layout system.
- Grid is a two-dimensional layout system.
position: relativepreserves normal flow while allowing offsets.position: absoluteremoves an element from normal flow.position: fixedpositions an element relative to the viewport in typical cases.- Pseudo-classes target element states or conditions.
- Pseudo-elements style parts of elements or generate content.
Frequently Asked Questions (FAQs)
1. Is CSS a programming language?
CSS is a style sheet language, not a general-purpose programming language. It describes how web content should be presented.
2. What is the difference between Flexbox and Grid?
Flexbox is primarily designed for one-dimensional layouts, while Grid is designed for two-dimensional layouts involving rows and columns.
3. Which is better: Flexbox or Grid?
Neither is universally better. Use Flexbox when arranging content mainly along one axis and Grid when you need stronger control over rows and columns.
4. What is specificity in CSS?
Specificity is the mechanism used by the CSS cascade to determine which selector has greater priority when competing declarations apply to the same element.
5. What does z-index do?
z-index controls the stacking order of positioned elements and other elements that participate in stacking contexts.
.modal {
position: fixed;
z-index: 1000;
}
6. What is the purpose of a media query?
Media queries allow CSS rules to respond to conditions such as viewport width.
@media (max-width: 600px) {
.menu {
flex-direction: column;
}
}
7. Why is box-sizing: border-box commonly used?
It makes the declared width and height include the element’s padding and border, which can make sizing easier to predict.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
