Set media queries for different devices

Media queries are a powerful tool in CSS3 that allow you to tailor website styles based on the user's device and screen size. This ensures a seamless and optimized experience across desktops, tablets, and smartphones.

What is a Media Query?

A media query is a CSS technique used to apply different styles to a webpage based on various conditions such as the characteristics of the user's device, screen size, orientation, and more.

@media media type and (condition: breakpoint) { // CSS rules }

By using media queries, web developers can create responsive designs that adapt seamlessly to different viewing environments, ensuring optimal presentation and user experience across a wide range of devices and screen sizes. Media queries are written using the @media rule in CSS and allow for conditional styling based on factors like screen width, height, resolution, and even features like whether the device is in landscape or portrait mode.

@ Media Rule

The @media rule in CSS is used to apply styles based on certain conditions, typically relating to the characteristics of the device or viewport where the webpage is being displayed. It allows developers to create responsive designs by specifying different styles for different media types, such as screen, print, or speech.

@media () { // CSS rules }

Within a @media rule, you define one or more media queries enclosed in curly braces {}, which contain the CSS styles that should be applied if the conditions of the media query are met.

Media queries typically include conditions like screen width, height, orientation, resolution, and more, providing fine-grained control over how content is displayed across various devices and environments. This enables developers to optimize layouts and styling for different screen sizes and device types, ensuring a consistent and user-friendly experience across a wide range of devices.


Parentheses, or round brackets, are commonly used in CSS within media queries to encapsulate the conditions for which certain styles should apply.

@media (max-width: 768px) { .text { font-size: 16px; } }

For example, in the syntax @media (max-width: 768px), the parentheses (max-width: 768px) contain the condition that specifies the maximum width of the viewport for the enclosed styles to take effect. Similarly, parentheses are used in other contexts within CSS, such as in function calls, mathematical expressions, and property values. They help to delineate different parts of the code and clarify the intended meaning or functionality.

Media Types

Media types in CSS are used to specify the type of device or media for which the styles are intended to apply. If we don’t apply a media type, the @ media rule selects all types of devices by default.

Here are some common media types along with examples:


@media screen { /* Styles for screens */ }

This media type is used for devices with a screen, such as computer monitors, tablets, and smartphones. Styles within this media query apply when the webpage is viewed on a screen.


@media print { /* Styles for print */ }

Styles within this media query apply when the webpage is printed. This can be used to define styles specifically for printed documents, such as adjusting margins, hiding non-essential content, or modifying font sizes for better readability on paper.


@media speech { /* Styles for speech */ }

This media type is used for speech synthesizers or screen readers. Styles within this media query apply when the content is spoken, allowing developers to adjust the presentation for accessibility purposes, such as removing visual styling that may not be relevant to users relying on auditory cues.

@media screen { body { font-family: Arial, sans-serif; background-color: #f0f0f0; } }
@media print { body { font-family: Times, serif; background-color: transparent; } }
@media speech { body { font-family: Verdana, sans-serif; background-color: white; color: black; } }

In this example, different font families and background colors are specified for the screen, print, and speech media types, ensuring optimal readability and presentation across different mediums.


Breakpoints in responsive web design refer to specific points at which the layout of a webpage changes to accommodate different screen sizes or device types. These breakpoints are defined using CSS media queries and are typically based on common device widths or resolutions.

Breakpoints are implemented using CSS media queries. Each breakpoint is defined within a media query, specifying the conditions under which certain styles should apply based on the width of the viewport. By defining breakpoints at key widths, developers can ensure that the layout adjusts smoothly to provide readability and usability across different devices, from large desktop monitors to small smartphones.

/* Small devices (phones) */ @media only screen and (max-width: 600px) { /* Styles for small screens */ }
/* Medium devices (tablets) */ @media only screen and (min-width: 601px) and (max-width: 1024px) { /* Styles for medium screens */ }
/* Large devices (desktops) */ @media only screen and (min-width: 1025px) { /* Styles for large screens */ }

In this example, three breakpoints are defined based on common device widths:

  1. Small devices: Up to 600px wide, typically targeting smartphones.
  2. Medium devices: Between 601px and 1024px wide, typically targeting tablets.
  3. Large devices: 1025px and wider, typically targeting desktops.

Set media queries for different devices

The core syntax for a media query involves the @media rule followed by media features in parentheses and curly braces containing the specific styles for that condition.

@media media_feature { /* Styles for the specified media feature */ }

Common Media Features

max-width and min-width: These target devices based on their screen width in pixels. For example, styles within @media (max-width: 768px) apply to screens smaller than 768px wide (typically smartphones).

Examples :

Responsive Layout

@media screen and (max-width: 768px) { .container { flex-direction: column; } .sidebar { order: 2; } .main-content { order: 1; } }

This example adjusts the layout of a webpage when the screen width is 768 pixels or less, switching from a horizontal layout to a vertical layout by changing the flex-direction property of the container.

Adjusting Text Size for Mobile

body { font-size: 16px; } @media (max-width: 768px) { body { font-size: 14px; } }

orientation: Targets portrait or landscape modes on mobile devices. Use portrait or landscape values.

Rotating Image Layout for Tablets

img.product-image { width: 100%; } @media (orientation: landscape) { img.product-image { width: 50%; float: left; } }

resolution: Targets devices based on their pixel density (e.g., dppx). Useful for high-resolution displays.

High-Resolution Images for Retina Displays

.logo { background-image: url("logo.png"); } @media (resolution: 2dppx) { .logo { background-image: url("logo_retina.png"); } }

Hiding Elements

@media screen and (max-width: 768px) { .sidebar { display: none; } }

This example hides the sidebar on screens with a width of 768 pixels or less, providing a cleaner layout for smaller devices.

Background Image Adjustment

@media screen and (max-width: 768px) { .hero-section { background-image: url('small-background.jpg'); } }

In this case, a smaller background image is used for the hero section on smaller screens to optimize loading times and reduce bandwidth usage.

Adjusting Navigation Menu

@media screen and (max-width: 768px) { .nav-menu { flex-direction: column; } .nav-item { margin-bottom: 10px; } }

Here, the navigation menu items are stacked vertically instead of horizontally on smaller screens, making them easier to tap with a finger on touch devices.

Best Practices

Follow a mobile-first approach

Adopting a mobile-first approach involves designing and developing a website starting from the smallest screen sizes, such as those of mobile devices, and then gradually adding features and enhancements for larger screens. This approach ensures that the core content and functionality are optimized for mobile users, who may have limited screen space and slower connections, before considering the needs of users on larger screens. By prioritizing smaller screens first, developers can create leaner, more efficient designs that deliver a better experience across all devices, regardless of screen size or capabilities.

Define breakpoints, key screen sizes where layout changes occur (e.g., 320px, 768px, 1024px):

Breakpoints are specific points in the CSS code where the layout of a webpage changes to adapt to different screen sizes or device types. These breakpoints are typically based on common screen widths and are defined using CSS media queries. Examples of common breakpoints include 320px (for small mobile screens), 768px (for tablets), and 1024px (for desktops). By defining breakpoints at key screen sizes, developers can ensure that the layout adjusts smoothly and responsively to provide an optimal viewing experience across a wide range of devices.

Use media queries responsively, adapting different elements like text, images, and navigation

Media queries are CSS techniques used to apply different styles based on various conditions, such as screen width, height, and orientation. When used responsively, media queries enable developers to adapt different elements of a webpage, such as text, images, and navigation menus, to ensure readability, usability, and visual appeal across different screen sizes and devices. For example, text sizes may be adjusted for smaller screens, images may be resized to fit the viewport, and navigation menus may be reorganized for better accessibility on touch devices. By using media queries responsively, developers can create flexible and adaptive designs that provide a consistent user experience across various devices.

Test your website across various devices and use browser developer tools

Testing your website across various devices is essential to ensure that it functions correctly and looks good on different screen sizes and device types. This can be done using physical devices, browser emulators, or online testing tools. Additionally, browser developer tools can be used to visualize media query activations, allowing developers to see how their CSS rules are being applied at different screen sizes and breakpoints. By testing across various devices and using developer tools to visualize media queries, developers can identify and fix any layout or styling issues, ensuring that their website provides a consistent and optimal experience for all users.

Additional Features:

To create responsive and adaptable web designs, combine media features with logical operators like AND and OR, allowing precise control over when styles are applied based on multiple conditions. Additionally, target specific media types such as print using @media print, enabling tailored styling for different output mediums. For more advanced targeting, utilize media features like device-aspect-ratio to address specific device characteristics and orientations, ensuring optimal presentation across a diverse range of devices and environments. By utilizing these techniques, developers can craft layouts that seamlessly adapt to various screen sizes and device types, providing users with an optimal browsing experience regardless of their device or viewing context.


Media queries in CSS allow for the adaptation of webpage layouts and styles to different screen sizes by defining conditions based on factors like screen width, height, and orientation. By utilizing media queries, developers can create responsive designs that dynamically adjust elements such as text size, image layout, and navigation menu placement to ensure optimal usability and visual appeal across a variety of devices and screen resolutions. This flexibility enables websites to provide a consistent and user-friendly experience regardless of the device being used to access them.