The Forgotten Media Query

Media Queries have been a crucial part of front-end development since the emergence of responsive design. Previously, separate mobile versions of websites were common, often using a sub-domain such as m.example.com, which utilized separate HTML, CSS, and JavaScript. However, with the growing usage of mobile devices, the need for optimized websites across various screen sizes became increasingly important, leading to the adoption of responsive design and the widespread use of media queries.

Responsiveness can be achieved in various ways, both entirely on the client side or with some help from the server side. These methods include media queries, Flexbox, Grid Layout, viewport units, responsive images, responsive typography, and server-side techniques. The focus of this article, however, is on media queries, which are used to apply different styles to different devices or screen sizes based on conditions defined in the query.

For example, the following media query tests the maximum width of the viewport:

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

If the viewport width is less than or equal to 767 pixels, the styles inside the media query will be applied, in this case, setting the font size of the body element to 14px. Similarly, the following media query tests the maximum height of the viewport:

@media (max-height: 400px) {
  body {
    font-size: 14px;
  }
}

It is worth noting that media queries testing the height of the viewport are less commonly used than those testing the width. However, this approach can be useful for solving specific design problems, particularly when it comes to controlling the layout of a website based on the height of the screen

In addition to these two media queries, CSS also provides other options for testing various aspects of a device, such as an aspect ratio, orientation, and resolution of the screen, pointer. These can be defined using media queries such as:

@media (aspect-ratio: 16/9) {
  body {
    font-size: 14px;
  }
}

@media (orientation: portrait) {
  body {
    font-size: 14px;
  }
}

@media (orientation: landscape) {
  body {
    font-size: 16px;
  }
}

@media (resolution: 300dpi) {
  body {
    font-size: 14px;
  }
}

@media (pointer: coarse) {
  /* styles for touch devices */
}

@media (pointer: fine) {
  /* styles for mouse or pen devices */
}

These media queries demonstrate the various options available to developers when it comes to creating responsive websites, and highlight the importance of having a solid understanding of media queries and their capabilities.

This article was written to explore the various media queries that CSS offers, and to serve as a reminder of the tools available to developers in their toolkits. Thank you for reading.

References

https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

0
Subscribe to my newsletter

Read articles from Adegoke Temitope directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adegoke Temitope
Adegoke Temitope

Hello, my name is Temitope. I am a full stack developer with three years of industry experience. I have had the opportunity to work on a wide range of products and technologies. My expertise includes designing, managing, and building end-to-end frontend applications using React, Vue, React Native, Node.js, and various databases and web services. I have also worked with analytics and monitoring tools such as Mixpanel, Google Analytics, Sentry, and Microsoft Clarity, and recently developed an interest in GrowthBook. Throughout my career, I have prioritized developing reusable and scalable architectures, including mono repos. While I have a beginner's knowledge of DevOps and Machine Learning, I possess a usable knowledge of cloud services like AWS. I am comfortable working with agile methodologies and collaborating with cross-functional teams. I particularly enjoy working in small teams where I can contribute to various tasks. As a UX Engineer/Full-stack Engineer at Future Academy Africa, I am responsible for developing, managing, and maintaining applications for various aspects of e-learning and talent development. I have developed applications for payment management, content management, people management, and more. Working with my team, we have implemented monitoring and analytics tools such as Sentry and Microsoft Clarity in collaboration with the design teams to gain better insights into user behavior and improve user experience. If you are looking for a full stack developer with a proven track record of success in development and management, along with a good understanding of products, I would love to connect and discuss potential opportunities.