Understanding CSS Media Queries

by | Mar 6, 2024 | Design, WordPress

CSS media queries are a powerful tool that allow you to apply different styles based on the characteristics of the device or viewport. They enable you to create responsive designs that adapt to different screen sizes, resolutions, and device types (desktop, tablet, mobile, etc.).

Syntax

The basic syntax for a media query is as follows:

css

@media [media-type] ([media-feature-rule]) {
/* CSS rules go here */
}
  • @media is the at-rule that starts a media query.
  • media-type (optional) specifies the type of media the styles should apply to, such as screen, print, or all (default).
  • media-feature-rule defines the conditions or rules that must be met for the styles to apply. Common rules include min-width, max-width, orientation, and others.

Common Use Cases

  1. Responsive Web Design

Media queries are essential for creating responsive websites that adapt to different screen sizes. Here’s an example of how to apply different styles based on the viewport width (see more on breakpoints below):

css

/* Styles for screens smaller than 600px (mobile devices) */
@media screen and (max-width: 599px) {
body {
font-size: 14px;
}
.container {
width: 100%;
}
}
/* Styles for screens between 600px and 900px (tablets) */
@media screen and (min-width: 600px) and (max-width: 899px) {
body {
font-size: 16px;
}
.container {
width: 80%;
}
}/* Styles for screens 900px and wider (desktops) */
@media screen and (min-width: 900px) {
body {
font-size: 18px;
}
.container {
width: 60%;
}
}

  1. Printing Styles

Media queries can be used to apply specific styles when a page is printed to paper:

css

/* Styles for printing */
@media print {
body {
font-size: 12pt;
color: black;
background-color: white;
}
.no-print {
display: none;
}
}
  1. Orientation-specific Styles

You can target styles based on the device’s orientation (portrait or landscape) using the orientation media feature:

css

/* Styles for portrait orientation */
@media (orientation: portrait) {
.container {
flex-direction: column;
}
}
/* Styles for landscape orientation */
@media (orientation: landscape) {
.container {
flex-direction: row;
}
}

Common CSS Breakpoints

When creating responsive designs with media queries, it’s essential to consider the screen sizes of popular devices and web browsers. By targeting specific breakpoints, you can ensure that your website or application displays correctly across a wide range of devices.

Desktop Browsers

For desktop browsers, the most common breakpoints are:

  • 1920px: This breakpoint targets large desktop screens and high-resolution displays. It’s a good practice to start with a wide layout and scale down for smaller screens.
  • 1366px: This breakpoint covers most modern desktop and laptop displays with a resolution of 1366×768 pixels or higher.
  • 1024px: This breakpoint is suitable for older desktops and laptops with smaller screen resolutions.

css

/* Styles for large desktop screens */
@media screen and (min-width: 1920px) {
/* CSS rules */
}
/* Styles for modern desktops and laptops */
@media screen and (max-width: 1919px) and (min-width: 1367px) {
/* CSS rules */
}/* Styles for older desktops and laptops */
@media screen and (max-width: 1366px) and (min-width: 1025px) {
/* CSS rules */
}

Tablets and Mobile Devices

For tablets and mobile devices, the most common breakpoints are:

  • 768px: This breakpoint targets most tablets in landscape orientation and some smaller desktop screens.
  • 600px: This breakpoint is a common choice for targeting smaller tablets and larger mobile devices in landscape orientation.
  • 480px: This breakpoint is suitable for most smartphones in landscape orientation.
  • 320px: This breakpoint covers most smartphones in portrait orientation, including older and lower-resolution devices.

css

/* Styles for tablets in landscape orientation */
@media screen and (max-width: 1024px) and (min-width: 769px) {
/* CSS rules */
}
/* Styles for smaller tablets and larger mobile devices in landscape */
@media screen and (max-width: 768px) and (min-width: 601px) {
/* CSS rules */
}/* Styles for smartphones in landscape orientation */
@media screen and (max-width: 600px) and (min-width: 481px) {
/* CSS rules */
}

/* Styles for smartphones in portrait orientation */
@media screen and (max-width: 480px) {
/* CSS rules */
}

It’s important to note that these breakpoints are general guidelines and may vary depending on your specific project requirements, target audience, and device usage patterns. It’s always a good practice to test your website or application on actual devices and make adjustments as needed.

Additionally, consider using responsive design techniques like fluid grids, flexible images, and media queries to create a seamless experience across different devices and screen sizes.

Best Practices

  • Use relative units like em, rem, or vw/vh for better responsiveness.
  • Start with mobile-first or desktop-first approach based on your target audience.
  • Combine media queries with CSS techniques like flexbox and grid for efficient layouts.
  • Prefer min-width over max-width for better future-proofing.
  • Test your media queries on various devices and screen sizes.

CSS media queries are a powerful tool that enable you to create responsive and adaptive designs. By understanding their syntax and use cases, you can create websites and applications that provide an optimal user experience across a wide range of devices and screen sizes.

WordPress Drama – The Latest Update

If you don't know what WordPress is or the recent discussions about it, let's talk. WordPress is a widely used platform that allows people to easily create and manage websites, and lately, there have been some important debates about its future direction that are...

Retiring Web Designers: Secure Your Legacy and Earn Recurring Income with Web321

After years of crafting beautiful websites and building strong client relationships, you’re considering retirement. It’s a significant decision that brings both excitement for the future and concern for the clients you’ve supported over the years. What will happen to them once you step away? How can you ensure they’re in good hands without abandoning them?

ECommerce Transaction Alternatives For WordPress.

When choosing an alternative to WooCommerce, consider specific needs, such as the type of products you’re selling, payment requirements, and desired features. Each of these plugins offers unique strengths that may better suit your ecommerce goals.

Setting Up A Custom Color Scheme For Divi

I have attached the file that I used my case.One Divi feature is”Color Scheme.” It presets for green, orange, pink, red and its default blue color schemes that influence the colors of borders, buttons and other accents on a baseline Divi theme. What happens if none of...

Webflow Price Increases

Webflow's recent price increase of approximately 44% for its CMS plans has sparked significant reactions among its user base, particularly among freelancers, agencies, and developers. Here are some key points and side effects based on user feedback: User Reactions and...

NXDOMAIN and DNS_PROBE_FINISHED_NXDOMAIN

An NXDOMAIN error, which stands for “Non-Existent Domain,” occurs when a domain name cannot be resolved by DNS servers. This error is common in various contexts, including WordPress sites.

9 Trends & Flexes In 2024 Web Design

The landscape of web design continues to evolve, embracing new technologies and design philosophies that enhance user experience, accessibility, and visual appeal.

Give Marketers A Perk: A Marketing Job WITHOUT Website Management

With all of the talk of workplace overload and the heavy job requirements, taking something off of a marketer’s plate provides two big wins: it give the marketer more breathing room to accomplish the important things in their job; and it offers the business owner some continuity on the public face of their brand– their website. Here’s our exhaustive list of ways a WordPress maintenance plan can help a marketer do their job better.

The Juggling Act of Priorities for a WordPress Developer

Being a WordPress developer is akin to performing a delicate juggling act. With numerous tasks requiring attention, from translating visual designs into functional websites to ensuring optimal performance and security, managing priorities becomes crucial. We put a lot of effort putting together an in-depth look at how a WordPress developer can effectively balance these responsibilities.

SEO In the World of Artificial Intelligence

Getting An Audience via Search Engine Optimized Content There's a new sheriff in town. Its name is "Search Generative Experience." For a while now, Google has been moving from being the world's biggest index of web pages do being the destination unto itself. Its...

Stripe vs Square vs Paypal

Here’s a quick rundown of these three major ecommerce providers and how they compare to each other. In my 25+ years of ecommerce rollouts, I have dealt with Stripe, Square and Paypal implementations as well as deployments with other providers like Authorize.net, Beanstream (WorldPay), Moneris and others. I really think the three in this article can satisfy most use cases.

Five Tips For AI In Content Creation

There’s a lot to consider about the practical aspects of creating content with AI but also the creative collaboration between human intelligence and artificial intelligence.

Spring into 2024: 30 Great Backlink Sources

Creating backlinks, which are links from one website to another, is a fundamental aspect of search engine optimization (SEO). They act as a "vote of confidence" from one site to another, indicating to search engines that the content is valuable, credible, and useful....