CSS media queries have revolutionized web design. This straightforward piece of code enables designers to craft impressive, practical layouts. Now websites appear fantastic on every screen.

css media query used to build a responsive website

First introduced in the late 1990s, media queries are an essential tool for any designer. In this article, we'll explore how you can utilize media queries in CSS to optimize your website's user experience.

Download Now: 25 HTML & CSS Hacks

Let's delve into it.

Table of Contents

A Brief Look at the Origins of Media Queries

CSS media queries were introduced in 1998 but gained popularity in the early 2010s with the proliferation of mobile devices. Ethan Marcotte's 2010 article, "Responsive Web Design," was a game-changer. It proposed the utilization of media queries to construct flexible, adaptive layouts capable of adjusting to various screen sizes and devices.

CSS3's 2011 release brought significant enhancements to media queries. This update introduced new features such as min-width, max-width, device-width, aspect-ratio, and pointer.

Nowadays, media queries are indispensable in web design. They equip designers with unparalleled authority over their website's appearance and usability.

Sites can now boast dynamic layouts that can adapt to the ever-changing digital landscape. By targeting specific devices, media queries have made websites more accessible.

Understanding the Concept of a media query in CSS

Utilizing media queries, you can elevate your CSS styling by implementing it based on a device's attributes, such as screen resolution or browser viewport width. By leveraging this technology, you can create responsive websites that exhibit outstanding performance across all devices.

Media queries offer a plethora of exciting possibilities for web developers, including the capacity to conditionally apply styles. You can pinpoint specific media for HTML elements using the media attribute.

Through the employment of Window.matchMedia() and EventTarget.addEventListener() methods, you can test and monitor media states. This grants you unparalleled control over your website's appearance and functionality.

Optimal Situations for Utilizing CSS Media Queries

In this era of digital advancement, users anticipate websites to adapt to all their devices. This is where Responsive Web Design (RWD) enters the scene. This design approach reacts to the user's behavior and environment in accordance with screen size, platform, and orientation.

By embracing flexible grids, layouts, images, and CSS media queries, RWD establishes a dynamic, adaptable design that automatically adjusts to fit any device.

Imagine browsing a website on your laptop, and then smoothly switching to your iPad and witnessing it seamlessly tailor itself to your device. With RWD, your website can achieve just that!

Furthermore, RWD can even consider the user's device settings, such as their VPN for iOS on their iPad. In brief, this approach provides a seamless user experience.

The utilization of RWD obviates the necessity for separate design and development phases for each new gadget in the market.

Examples of CSS Media Queries in Action

The popular travel website Airbnb exemplifies the benefits of media queries. So let's delve deeper into how Airbnb harnesses CSS media queries to deliver a visually impressive and responsive website.

Media Query for Desktop

The desktop version of the Airbnb website showcases stunning imagery and effortless navigation within a sophisticated multi-column structure. The navigation menu and user-friendly search bar are prominently positioned, enabling users to quickly filter their preferences.

As the screen size decreases, the website adapts to a single-column layout. The images become smaller yet maintain their high quality. The navigation menu becomes more concise while remaining user-friendly.

Image Source

Media Query for Tablet

The Airbnb website optimizes its layout for touch interaction on tablet devices. The navigation menu occupies less screen space while maintaining easy accessibility. The search bar also relocates to the top of the page.

The listing images are scaled down to fit the smaller screen size while preserving their quality, providing users with a visually stunning experience.


Image Source

Media Query for Mobile

Airbnb's mobile website design prioritizes user experience and accessibility. The navigation menu is simplified to a hamburger icon. The search bar is positioned front and center for quick result filtering.

High-quality images are scaled down but easily browsable through swipeable galleries. The vertical scrolling layout enables seamless exploration with just one hand. The outcome is a user-friendly, enjoyable browsing experience on the go.


Image Source

Implementing Media Queries in CSS

CSS media queries enable you to apply specific styles to different screen sizes or devices.

Below, we will elaborate on the following aspects of employing media queries:

  • Syntax.
  • Breakpoints.
  • Screen sizes.
  • Media types.
  • Media features.
  • Test and refine.

Syntax

In this example, we have two sets of CSS rules; one for desktop screens and another for mobile screens. The CSS rules for desktop screens are defined outside of any media query and are applicable to all screen sizes.

The CSS rules for mobile screens are defined within a media query that targets screens with a maximum width of 768px. The syntax of the media query is as follows:

The @media rule signifies that it is a media query. The "only" keyword specifies that this query should solely apply to screens that fulfill the specified conditions. In this case, we are targeting screens that have the screen media type and a maximum width of 768px.

Inside the curly braces, we define the CSS rules that should be applied to screens that fulfill the specified conditions. In this example, we are modifying the background color, font size, and text color of the body element for mobile screens.

Media Query in CSS, picture examples of media queries — Desktop: @media screen and (min-width: 1024px); Tablet: @media screen and (min-width: 768px) and (max-width: 1023px); Smartphone: @media screen and (max-width: 767px)

Breakpoints

In this example, we have defined three different sets of styles for three different screen sizes or device types:

  • Mobile devices.
  • Tablets.
  • Desktops/laptops.

We have utilized media queries to target specific screen sizes and apply the appropriate styles.

The first set of styles, outside of any media query, applies to all screen sizes. We have set the font size to 16px and the line height to 1.5.

The second set of styles, within a media query targetting a maximum width of 575px, addresses mobile devices. We have reduced the font size to 14px and the line height to 1.4 to enhance text readability on smaller screens.

The third set of styles, within a media query targetting a minimum width of 576px and a maximum width of 991px, caters to tablets. We have restored the font size and line height to their default values of 16px and 1.5, respectively.

The fourth set of styles, within a media query targetting a minimum width of 992px, focuses on desktops and laptops. We have increased the font size to 18px and the line height to 1.6 to take advantage of the larger screen size.

Image Source

Screen Sizes

In the example below, we have defined styles for a .some-class selector for three different screen sizes:

The styles for desktop screens are defined first, and then media queries are used to adjust the font size and color for smaller screens. The @media rule specifies the maximum width of the screen for each breakpoint.

 

Media Types

CSS media types are employed to define different styles for different types of devices or media. The most common media types are screen, print, and speech.

The screen media type is utilized for computer screens, smartphones, tablets, and similar devices. It can be utilized to define styles for different screen sizes or resolutions.

The print media type is used when a web page is being printed. It can be used to define styles explicitly for print, such as hiding specific elements or adjusting the layout.

The speech media type is applied to screen readers and other devices that read aloud the content of a web page. It can be used to define styles for users who rely on screen readers or similar technology.

To apply styles for a specific media type, you can utilize the @media rule followed by the media type in parentheses. Refer to the examples below.

Media Features

In this code snippet, each @media rule targets specific CSS media features. The first two @media rules focus on screen width, with the first one targeting screens with a width of 600px or less, and the second one targeting screens with a width of 601px or greater. The third @media rule addresses screens with a width ranging between 601px and 900px.

The fourth @media rule targets screen orientation, specifically screens in portrait mode. Lastly, the fifth @media rule focuses on screens with a high resolution, with support for both -webkit-min-device-pixel-ratio and min-resolution media features.

 

Test and Refine

Once you have written your media queries, it is crucial to test them on a variety of devices and screen sizes.

Start by utilizing your browser's built-in developer tools, which allow you to simulate different screen sizes and devices. Additionally, you can test your media queries on physical devices to ensure they function as intended.

During the testing process, pay attention to any layout or display issues, such as elements that are cut off or overlapping. Based on your testing, you may need to refine your styles or adjust your breakpoints.

When testing and refining your media queries, consider the user experience. Ensure that your content is easily readable and accessible on all devices, while any interactive elements are simple to use and navigate.

Commencing Your Journey with CSS Media Queries

Media queries comprise an indispensable element of web design. Designers can create dynamic, adaptable layouts that cater to diverse devices. Now, it's your turn to foster a seamless, accessible user experience.

coding-hacks


Source