Learn and master the top CSS Media Queries interview questions along with detailed answers and code snippets
- What are CSS Media Queries?
CSS Media Queries are a feature in CSS that allow you to apply different styles based on the characteristics of the device or viewport, such as screen size, resolution, orientation, etc. - How do you write a basic media query in CSS?
A basic media query consists of an@media
rule followed by a condition enclosed in parentheses. Here’s an example of a media query for screens with a maximum width of 600 pixels:
@media (max-width: 600px) {
/* CSS styles for screens with max width of 600px */
}
- How do you apply styles specifically for mobile devices?
You can use media queries to target mobile devices by specifying the maximum width for mobile screens. For example:
@media (max-width: 480px) {
/* CSS styles for mobile devices */
}
- How do you apply styles specifically for desktop devices?
You can use media queries to target desktop devices by specifying the minimum width for desktop screens. For example:
@media (min-width: 1024px) {
/* CSS styles for desktop devices */
}
- How do you apply styles based on device orientation?
You can use theorientation
property within a media query to target devices in portrait or landscape mode. For example:
@media (orientation: landscape) {
/* CSS styles for landscape mode */
}
@media (orientation: portrait) {
/* CSS styles for portrait mode */
}
- How do you apply styles for high-resolution displays (Retina)?
You can use themin-resolution
property within a media query to target high-resolution displays. For example, targeting Retina displays with a pixel density of 2x:
@media (min-resolution: 192dpi) {
/* CSS styles for high-resolution displays */
}
- How do you apply styles for specific screen resolutions?
You can use themin-width
andmax-width
properties within a media query to target specific screen resolutions. For example, targeting screens between 768px and 1024px:
@media (min-width: 768px) and (max-width: 1024px) {
/* CSS styles for screens between 768px and 1024px */
}
- How do you apply styles for both landscape and portrait orientations?
You can combine media queries to apply styles for both landscape and portrait orientations. For example:
@media (orientation: landscape), (orientation: portrait) {
/* CSS styles for both landscape and portrait orientations */
}
- How do you apply styles for touch-enabled devices?
You can use thepointer
media feature within a media query to target touch-enabled devices. For example:
@media (pointer: coarse) {
/* CSS styles for touch-enabled devices */
}
- How do you apply styles for devices with a specific aspect ratio?
You can use theaspect-ratio
media feature within a media query to target devices with a specific aspect ratio. For example, targeting devices with an aspect ratio of 16:9:
@media (aspect-ratio: 16/9) {
/* CSS styles for devices with aspect ratio of 16:9 */
}
- How do you apply styles for devices with a specific pixel density?
You can use themin-resolution
media feature within a media query to target devices with a specific pixel density. For example, targeting devices with a pixel density of 2x:
@media (min-resolution: 192dpi) {
/* CSS styles for devices with pixel density of 2x */
}
- How do you apply styles specifically for print media?
You can use theprint
media type within a media query to target styles specifically for printing. For example:
@media print {
/* CSS styles for print media */
}
- How do you apply styles for dark mode?
You can use theprefers-color-scheme
media feature within a media query to target devices with dark mode enabled. For example:
@media (prefers-color-scheme: dark) {
/* CSS styles for devices with dark mode enabled */
}
- How do you apply styles for devices with a specific display mode (e.g., fullscreen)?
You can use thedisplay-mode
media feature within a media query to target devices with a specific display mode. For example, targeting devices in fullscreen mode:
@media (display-mode: fullscreen) {
/* CSS styles for devices in fullscreen mode */
}
- How do you apply styles based on the user’s preferred language?
You can use theprefers-reduced-motion
media feature within a media query to target devices where the user prefers reduced motion. For example:
@media (prefers-reduced-motion: reduce) {
/* CSS styles for devices where user prefers reduced motion */
}
- How do you apply styles for devices with a specific color gamut?
You can use thecolor-gamut
media feature within a media query to target devices with a specific color gamut. For example, targeting devices with a wide color gamut:
@media (color-gamut: p3) {
/* CSS styles for devices with wide color gamut */
}
- How do you apply styles specifically for devices with a hover capability?
You can use thehover
media feature within a media query to target devices with a hover capability. For example:
@media (hover: hover) {
/* CSS styles for devices with hover capability */
}
- How do you apply styles specifically for devices with a pointer capability?
You can use thepointer
media feature within a media query to target devices with a pointer capability. For example:
@media (pointer: fine) {
/* CSS styles for devices with fine pointer capability */
}
- How do you apply styles for devices with a specific device width and height?
You can use thedevice-width
anddevice-height
properties within a media query to target devices with a specific device width and height. For example, targeting devices with a device width of 320px and height of 480px:
@media (device-width: 320px) and (device-height: 480px) {
/* CSS styles for devices with specific device width and height */
}
- How do you apply styles based on the user’s preferred color scheme?
You can use theprefers-color-scheme
media feature within a media query to target devices based on the user’s preferred color scheme. For example:
@media (prefers-color-scheme: light) {
/* CSS styles for devices with
light color scheme preference */
}
@media (prefers-color-scheme: dark) {
/* CSS styles for devices with dark color scheme preference */
}
- How do you apply styles specifically for devices with a specific device aspect ratio?
You can use thedevice-aspect-ratio
media feature within a media query to target devices with a specific device aspect ratio. For example, targeting devices with a device aspect ratio of 16:9:
@media (device-aspect-ratio: 16/9) {
/* CSS styles for devices with specific device aspect ratio */
}
- How do you apply styles specifically for devices with a specific resolution?
You can use theresolution
media feature within a media query to target devices with a specific resolution. For example, targeting devices with a resolution of 300dpi:
@media (resolution: 300dpi) {
/* CSS styles for devices with specific resolution */
}
- How do you apply styles specifically for devices with a specific scan type?
You can use thescan
media feature within a media query to target devices with a specific scan type. For example, targeting devices with a progressive scan:
@media (scan: progressive) {
/* CSS styles for devices with progressive scan type */
}
- How do you apply styles specifically for devices with a specific aspect ratio and resolution?
You can combine thedevice-aspect-ratio
andresolution
media features within a media query to target devices with a specific aspect ratio and resolution.
For example, targeting devices with an aspect ratio of 16:9 and a resolution of 300dpi:
@media (device-aspect-ratio: 16/9) and (resolution: 300dpi) {
/* CSS styles for devices with specific aspect ratio and resolution */
}
- How do you apply styles specifically for devices with a specific aspect ratio and color gamut?
You can combine thedevice-aspect-ratio
andcolor-gamut
media features within a media query to target devices with a specific aspect ratio and color gamut.
For example, targeting devices with an aspect ratio of 16:9 and a wide color gamut:
@media (device-aspect-ratio: 16/9) and (color-gamut: p3) {
/* CSS styles for devices with specific aspect ratio and color gamut */
}
- How do you apply styles specifically for devices with a specific resolution and hover capability?
You can combine theresolution
andhover
media features within a media query to target devices with a specific resolution and hover capability.
For example, targeting devices with a resolution of 300dpi and hover capability:
@media (resolution: 300dpi) and (hover: hover) {
/* CSS styles for devices with specific resolution and hover capability */
}
- How do you apply styles specifically for devices with a specific orientation and pointer capability?
You can combine theorientation
andpointer
media features within a media query to target devices with a specific orientation and pointer capability.
For example, targeting devices in landscape orientation and with fine pointer capability:
@media (orientation: landscape) and (pointer: fine) {
/* CSS styles for devices in landscape orientation and with fine pointer capability */
}
- How do you apply styles specifically for devices with a specific color gamut and prefers reduced motion?
You can combine thecolor-gamut
andprefers-reduced-motion
media features within a media query to target devices with a specific color gamut and where the user prefers reduced motion.
For example, targeting devices with a wide color gamut and reduced motion preference:
@media (color-gamut: p3) and (prefers-reduced-motion: reduce) {
/* CSS styles for devices with wide color gamut and reduced motion preference */
}
- How do you apply styles specifically for devices with a specific display mode and prefers dark color scheme?
You can combine thedisplay-mode
andprefers-color-scheme
media features within a media query to target devices with a specific display mode and where the user prefers a dark color scheme.
For example, targeting devices in fullscreen mode and with dark color scheme preference:
@media (display-mode: fullscreen) and (prefers-color-scheme: dark) {
/* CSS styles for devices in fullscreen mode and with dark color scheme preference */
}
- How do you apply styles specifically for devices with a specific device width, prefers landscape orientation, and hover capability?
You can combine thedevice-width
,orientation
, andhover
media features within a media query to target devices with a specific device width, prefers landscape orientation, and hover capability.
For example, targeting devices with a device width of 768px, prefers landscape orientation, and hover capability:
@media (device-width: 768px) and (orientation: landscape) and (hover: hover) {
/* CSS styles for devices with specific device width, prefers landscape orientation, and hover capability */
}