CSS media queries: max-width OR max-height

Cover Image for CSS media queries: max-width OR max-height
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Mastering CSS Media Queries: The Power of max-width VS max-height

Welcome to Tech Blog, where we unlock the mysteries of coding, one emoji at a time! Today, we're diving into the fascinating world of CSS media queries and answering a question that has puzzled many web developers: Can we use "OR" logic in media queries? Let's find out! 🕵️‍♀️

The Dilemma: Multiple Conditions in CSS Media Queries 🤔

Imagine you're designing a responsive website and want to apply a specific style when the screen width is below 995 pixels OR the screen height is below 700 pixels. The logical thing to do would be to use an "OR" operator, right? Unfortunately, CSS doesn't work that way, as shown in the code snippet below:

@media screen and (max-width: 995px OR max-height: 700px) {
  /* This doesn't work */
  ...
}

Understanding the Problem: max-width VS max-height 📏

Before we jump into the solutions, it's crucial to understand the difference between max-width and max-height in media queries. 📐

  • max-width: This media feature checks if the viewport width is equal to or less than the specified value. It primarily caters to responsive designs that adapt to different screen sizes.

  • max-height: On the other hand, max-height compares the viewport height with the specified value. This is particularly useful when dealing with content that needs to be styled differently based on vertical space.

Solution 1: Separate Media Queries 🕹️

The simplest solution to our problem is to use separate media queries, each targeting a specific condition. By doing this, we can achieve the desired behavior without the need for "OR" logic. Check out the code snippet below:

/* Apply styles when max-width is 995px */
@media screen and (max-width: 995px) {
  ...
}

/* Apply styles when max-height is 700px */
@media screen and (max-height: 700px) {
  ...
}

See? By creating two separate media queries, we're able to handle both conditions effectively. 😎

Solution 2: Combine Multiple Media Queries with Comma Separation 🧩

While solution 1 is straightforward, it can result in repetitive code and unnecessary duplication. To optimize our CSS, let's explore a more concise approach using comma separation.

/* Apply styles when max-width is 995px OR max-height is 700px */
@media screen and (max-width: 995px), screen and (max-height: 700px) {
  ...
}

Voilà! By separating the media queries with commas, we can achieve the desired "OR" logic in a clean and efficient way. 🌟

Your Turn: Embrace the Power of Responsive Styling! 💪

Now that you've unlocked the secret behind combining conditions in CSS media queries, it's time to put your newfound knowledge into action! Experiment with these techniques in your next web project and witness the magic of responsive styling unfold. 🎩✨

If you still have any questions or want to share your experiences, leave a comment below. Let's start a conversation and learn from each other! 🗣️💡

Until next time, happy coding! 🚀👩‍💻👨‍💻

P.S. Don't forget to follow us on Twitter and subscribe to our newsletter for more tech tips, coding hacks, and emoji-filled content! 💌🐦


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello