Intermediate CSS Interview Questions
1. Define z-index.
Ans: Z-index is a common topic in CSS interview questions. It controls the stacking order of overlapping elements. By default, its value is 0, but it can accept positive or negative integers. Elements with a higher z-index are stacked above those with a lower value. It accepts the following values: auto, a number (e.g., 1, -1), initial, and inherit.
2. What are the benefits of CSS Sprites?
Ans: With CSS sprites, loading multiple images is efficient. Blinking issues are eliminated, and assets are not downloaded in advance unless required.
3. How can you target h3 and h2 with the same styling?
Ans: * Multiple elements can be targeted by separating them with a comma */
h2, h3 {
color: red;
}
This CSS rule applies the color: red; style to both <h2> and <h3> elements.
4. Name media types allowed by CSS.
Ans: The different media types allowed by CSS include:
-
all – Suitable for all devices.
-
print – Intended for paged material and documents viewed on a printed medium.
-
screen – Intended primarily for screens (e.g., desktops, tablets, smartphones).
-
speech – Intended for screen readers that read the content aloud.
-
braille – For braille tactile feedback devices.
-
embossed – For paged braille printers.
-
handheld – Intended for handheld devices (largely deprecated).
-
projection – For projected presentations (like slides).
-
tty – For media using a fixed-pitch character grid, such as teletypes and terminals.
-
tv – For television-type devices.
Notes:
-
Terms like audio, visual, grip media, and bitmap are not valid CSS media types.
-
Some older media types like
handheld,tty, andtvare obsolete or rarely used in modern web development. -
Modern CSS uses media features (like
prefers-reduced-motion,color,hover, etc.) within@mediaqueries to handle device capabilities rather than focusing solely on media types.
5. How can you use CSS to control image repetition?
Ans: The background-repeat property is used to control whether and how a background image repeats.
Example:
Note:
Use no-repeat (not none) to prevent the background image from repeating.
6. Tell us about the property used for image scroll controlling.
Ans: The background-attachment property defines whether a background image stays fixed in place or scrolls along with the content.
Example (fixed background image):
