CSS Interview Questions for Experienced
1. Difference between CSS grid vs flexbox?
Ans: CSS Grid Layout is a two-dimensional layout system that can manage both rows and columns. It is ideal for large-scale layouts that are not strictly linear in design.
Flexbox is a one-dimensional layout system, working in either a row or a column. It is best suited for arranging components within an application.
2. Explain CSS position property?
Ans: The position property in CSS is used to control the layout and positioning of HTML elements on a web page. It defines how an element is positioned in the document flow and how it relates to other elements.
Types of CSS Position Values
-
static(default)-
The element is positioned according to the normal document flow.
-
Top, right, bottom, and left properties have no effect.
-
-
relative-
The element remains in the normal flow but can be shifted using
top,right,bottom, andleft. -
Its position is relative to itself.
-
-
absolute-
The element is removed from the normal document flow.
-
It is positioned relative to the nearest positioned ancestor (i.e., an ancestor with
relative,absolute, orfixedposition). -
If no such ancestor exists, it is positioned relative to the
<html>element.
-
-
fixed-
The element is removed from the normal flow and positioned relative to the browser window.
-
It does not move when the page is scrolled.
-
-
sticky-
The element acts like
relativeuntil it reaches a defined scroll position, then behaves likefixed. -
Often used for sticky headers or menus.
-
Summary Table
| Value | Removed from Flow? | Positioned Relative To | Scrolls with Page? |
|---|---|---|---|
static |
No | Normal flow | Yes |
relative |
No | Its normal position | Yes |
absolute |
Yes | Nearest positioned ancestor | No |
fixed |
Yes | Viewport | No |
sticky |
No | Scrolls until fixed at a threshold | Partially |
3. When does DOM reflow occur?
Ans: Reflow is the process in web browsers where the positions and geometries of elements in the document are recalculated to re-render part or all of the page.
Reflow can occur when you:
-
Insert, remove, or update an element in the DOM
-
Modify content on the page (e.g., change text in an input box)
-
Move a DOM element
-
Animate a DOM element
-
Take measurements of an element (e.g.,
offsetHeight,getComputedStyle) -
Change a CSS style
4. Different Box Sizing Property?
Ans: The box-sizing CSS property defines how the total width and height of an element are calculated.
-
Content-box (default): The width and height apply only to the content. Padding and border are added outside the box, increasing the total size.
-
Padding-box: The width and height include the content and padding. The border is added outside. (Note: Supported only in Firefox.)
-
Border-box: The width and height include content, padding, and border. This keeps the total size consistent regardless of padding or border.
5. How to center align a div inside another div?
Ans:
1. Horizontally Center a div (inline-block or block)
Using margin: auto:
2. Horizontally and Vertically Center Using Flexbox
This is the most modern and widely used method.
-
justify-content: center→ centers horizontally -
align-items: center→ centers vertically
3. Using Grid (Modern Alternative)
4. Absolute Positioning (Older Method)
Ans: The initial keyword in CSS can be used to reset a property to its default value as defined by the CSS specification.
13. What do CSS Custom properties variables mean?
Ans: Custom properties, also known as CSS variables, allow users to define reusable values throughout a stylesheet. They are declared using the -- prefix and accessed with the var() function.
Corrected CSS Code:
-
Fixed typo
background-color·tobackground-color: -
Removed space in
var (--main-bg-color)tovar(--main-bg-color) -
Fixed
margin: l0px,tomargin: 10px;(used lowercase L instead of 1) -
Replaced commas
,with semicolons;at the end of CSS declarations -
Corrected
height: 5Opx;toheight: 50px;(used capital “O” instead of zero)
14. What does * { box-sizing: border-box; } do? What are its advantages?
Ans: The CSS rule * { box-sizing: border-box; } applies the box-sizing: border-box property to all elements on the page.
What box-sizing: border-box Does:
By default, elements use box-sizing: content-box, which means:
-
The width and height apply only to the content.
-
Padding and border are added outside the specified width/height.
With box-sizing: border-box, this changes:
-
The width and height include content, padding, and border.
-
So, if you set
width: 200px, the total space it takes up stays 200px, even if you add padding or a border.
Advantages of box-sizing: border-box:
-
Predictable Layouts:
-
It makes layouts easier to manage because padding/border don’t expand the element’s size.
-
-
Simpler Math:
-
No need to manually subtract padding/border from the width/height.
-
-
Consistent Design:
-
Keeps element sizes consistent, which helps avoid overflow and layout bugs.
-
-
Better for Responsive Design:
-
Easier to control element sizes on different screen sizes.
-
Common Usage:
It’s widely used in modern CSS resets and frameworks like this:
This ensures even pseudo-elements follow the same box-sizing behavior.
15. What is the difference between CSS variables and preprocessor(SASS, LESS, Stylus) variables?
Ans: CSS variables can be used directly in modern browsers without the need for a preprocessor, as all major browsers currently support them. Unlike preprocessor variables, CSS variables follow the cascade, allowing inheritance and dynamic behavior. Additionally, CSS variables can be accessed and manipulated using JavaScript.
16. What does !important mean in CSS?
Ans: The style rule with !important has the highest precedence and overrides other conflicting rules, even those with higher specificity.
Explanation:
Although the #thing selector is more specific, the color: red !important; in the p selector overrides it, so the paragraph text will appear red.
17. What is progressive rendering? How do you implement progressive rendering in the website?. What are the advantages of it?
Ans: Progressive Rendering is a web development technique used to improve perceived performance by displaying parts of a web page as soon as they’re ready, instead of waiting for all resources to load. This approach enhances user experience, especially on slower networks or content-heavy websites.
What is Progressive Rendering?
Progressive rendering refers to strategies that render and display content incrementally rather than all at once. This can include:
-
Showing basic structure first (HTML/CSS)
-
Lazy-loading images or scripts
-
Streaming content to the browser in chunks
How to Implement Progressive Rendering
Here are some common ways to implement it:
1. Server-Side Rendering with Chunked Transfer Encoding
-
Sends HTML to the browser in parts.
-
The browser starts parsing and rendering before the entire page is received.
2. Lazy Loading
-
Delay loading of non-critical resources, like images, videos, or below-the-fold content.
3. Skeleton Screens or Content Placeholders
-
Show grey blocks or loading animations while the real content is fetched.
4. Code Splitting
-
Divide your JavaScript code into smaller bundles that load as needed.
5. Prioritize Above-the-Fold Content
-
Ensure CSS and content for the top portion of the page is loaded and rendered first.
Use <link rel="preload">, inlined CSS, or move blocking scripts to the bottom of the page.
Advantages of Progressive Rendering
| Benefit | Description |
|---|---|
| ⚡ Faster Perceived Load | Users see content sooner, improving UX |
| 📶 Better on Slow Networks | Prioritizes essential content |
| 🧠 Perception of Performance | Gives a feeling of responsiveness |
| 📱 Mobile-Friendly | Optimizes experience on low-power devices |
| 💡 Incremental Loading | Reduces Time to First Byte (TTFB) & First Contentful Paint (FCP) |
18. What is specificity? How to calculate specificity?
Ans: CSS Specificity:
It is the process of determining which CSS rule will be applied to an element when multiple rules target the same element. Specificity defines the priority of each rule. Inline styles have the highest specificity, followed by ID selectors, then class selectors (including pseudo-classes and attribute selectors). The universal selector (*) has the lowest specificity. ID selectors have higher specificity than class or attribute selectors.
19. What are the advantages of using translate() instead of absolute position?
Ans: translate() does not trigger a browser repaint or layout recalculation; it operates solely on the compositor layer. In contrast, using absolute positioning can cause a repaint or DOM reflow. Therefore, translate() offers better performance.
20. Does style1.css have to be downloaded and parsed before style2.css can be fetched?
Ans: <head>
<link href=”style1.css” rel=”stylesheet”>
<link href=”style2.css” rel=”stylesheet”>
</head>
Explanation:
No, the browsers will download and apply the CSS files in the order they appear in the HTML page. If both files contain styles for the same elements, the second file (style2.css) will override the styles defined in the first (style1.css) due to the CSS cascade.
21. How to determine if the browser supports a certain feature?
Ans: The @supports rule in CSS is useful for checking whether the current browser supports a specific CSS feature.
If the browser supports display: grid, the styles inside the block will be applied. This helps in writing fallback-friendly and progressive CSS.
22. How will you fix browser-specific styling issues?
Ans: To fix browser-specific styling issues, you can follow these approaches:
-
Use CSS Resets or Normalize.css:
Apply a CSS reset or usenormalize.cssto create consistent default styling across browsers. -
Vendor Prefixes:
Add browser-specific prefixes (e.g.,-webkit-,-moz-,-ms-,-o-) to ensure support for experimental or non-standard CSS properties. -
Feature Detection with Modernizr:
Use libraries like Modernizr to detect features and apply styles or scripts conditionally. -
Conditional Comments (for older IE versions):
Use conditional comments in HTML to target specific versions of Internet Explorer. -
Targeting with Hacks (last resort):
Apply CSS hacks only when absolutely necessary, as they reduce maintainability. Prefer using proper techniques first. -
Cross-Browser Testing Tools:
Use tools like BrowserStack or CrossBrowserTesting to test your site across different browsers and devices. -
Use Standard and Valid Code:
Write valid, standards-compliant HTML/CSS to minimize issues across browsers.
23. How does this property work overflow: hidden?
Ans: How overflow: hidden Works:
When applied, it hides any content that goes beyond the dimensions (width/height) of the element. That means:
-
Extra content is not visible
-
No scrollbars appear
-
The overflowing parts are simply clipped
Syntax:
Example:
In this example:
-
The paragraph is taller than the
div. -
Because of
overflow: hidden, the extra text that doesn’t fit within the100pxheight will be cut off and not shown.
Common Use Cases:
-
Creating clean layouts without scrollbars.
-
Hiding scrollable content temporarily.
-
Clearing floats in layout techniques (often with
overflow: hiddenon the parent element).
If you want to see the content (by scrolling), you would use overflow: auto or overflow: scroll instead.
24. How will you align content inside the p tag at the exact center inside the div?
Ans: We can align content horizontally by using the text-align: center property inside the parent div. However, this does not align the content vertically.
To center content both horizontally and vertically, we can use the following approach:
-
Set the parent element’s position to
relative. -
Set the child element’s position to
absolute. -
Use
top,bottom,left, andrightas0on the child element. -
Set
margin: autoon the child to center it within the parent. -
Ensure both parent and child elements have defined
heightandwidth.
Example:
Suppose we have a div that takes up 20% of the screen’s height and width, and inside it a p tag with a height of 1.2em and width of 20%. Here’s how we can center the paragraph inside the div:
