Advanced CSS Interview Questions
1. Name different ways to position some aspects in CSS.
Ans: The position property defines the positioning method used for an element. The five possible values are static, relative, absolute, fixed, and sticky. Elements can then be positioned using the top, right, bottom, and left properties, which take effect based on the value of the position property.
2. When should you use translate () instead of absolute positioning?
Ans: translate() is a CSS transform function that moves elements without triggering layout reflow or repaint. When you change properties like opacity or transform, the browser avoids costly reflows and instead uses the GPU for rendering, improving performance. In contrast, changes to absolute positioning rely on the CPU and can trigger reflow. Using translate() is more efficient—it results in faster paint times and maintains the element’s original space in the document flow, unlike absolute positioning changes which alter layout structure.
3. How can you optimize the webpage for prints?
Ans: A typical website consists of distinct sections such as the header, footer, sidebar, navigation bar, and main content area. Most styling and layout adjustments are primarily focused on the main content area. To ensure effective print output without compromising the website’s integrity, follow these practices:
-
Use CSS page breaks to control how content appears on printed pages.
-
Create a dedicated print stylesheet to optimize formatting specifically for print.
-
Size your layout appropriately for standard paper dimensions.
-
Avoid using unnecessary HTML tables, as they can interfere with clean print formatting.
4. What are mixins?
Ans: A mixin is similar to a function, but instead of returning a single value, it outputs multiple lines of Sass code that compile directly into CSS styles. While functions return a value used within a CSS property or passed to another mixin or function, mixins are used to include reusable style blocks with or without parameters.
5. What is meant by CSS working under the hood?
Ans: When a browser displays a document, it combines style information (CSS) and document content (HTML) in two stages:
-
Conversion of HTML and CSS into the Document Object Model (DOM) – The browser parses the HTML and CSS to build a structured representation of the page.
- Rendering the DOM – The browser uses the DOM to display the content visually on the screen.
6. Differentiate between the use of ID selector and class selector.
Ans: ID Selector:
CSS Class Selector:
Explanation:
-
#righttargets an element with the IDright. -
.righttargets elements with the classright.
7. What do you understand by pseudo-elements?
Ans: Pseudo-elements allow you to apply special effects to selectors without adding extra HTML markup. They help insert content or style parts of elements—like the first line or first letter—when additional HTML isn’t feasible, making styling more efficient and non-intrusive.
8. Tell us about CSS float property.
Ans: The float property in CSS positions an element, such as an image, to the left or right, allowing text to wrap around it. It does not affect the properties of preceding elements.
9. Differentiate between logical and physical tags.
Ans: Logical tags primarily emphasize the meaning of content and are older than physical tags. While physical tags are commonly used for styling, logical tags are less frequently applied for aesthetic purposes.
10. What does the CSS overflow property do?
Ans: The overflow property in CSS controls how content that exceeds an element’s box is handled. It determines whether the overflow is clipped or if scrollbars are added. Available values include:
-
visible – Content is not clipped and may overflow the element’s box.
-
hidden – Overflowing content is clipped and not visible.
-
scroll – Content is clipped, but scrollbars are always shown to view the rest.
-
auto – Scrollbars appear only when the content overflows.
11. How media types in CSS work?
Ans: The four types of media properties are: all, print, speech, and screen.
Example of using the print media type:
