The concept of min/max-width media query lacks grammatical coherence

I am struggling to grasp the concept of min-width and max-width media queries.

If I were to create a media query, I would want it to look something like this (in pseudo-code)....

if(screen.width < 420)
{
     ApplyStyle();
}

The idea of using terms like min and max in relation to width doesn't quite click for me, as setting a 'min-width' on a div element seems more like a command than a question.

Although I understand that when my screen size falls below 420px, the following occurs...

@media screen and (max-width:420px) {


}

But why does CSS need to check this if I have already specified the maximum width? Shouldn't it already be known?

It's possible that I am missing some grammatical or contextual understanding here. Can someone please clarify?

Answer №1

min-width in media queries does not correspond to the min-width property you set on elements; these are two distinct concepts.

Within media queries, min-width: X is considered true if the viewport width is greater than or equal to X, essentially functioning as screen.width >= X. Conversely, max-width would then be analogous to screen.width <= X

To me, it seems logical to interpret

@media screen and (max-width:420px)
as a screen with a maximum width of 420px, encompassing anything from 0 to 420px

Answer №2

Here is a straightforward illustration to guide you...

Consider a scenario where a website has the following media queries:

/* #1- Large desktop */
@media (min-width: 980px) { ... }

/* #2- Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { ... }

/* #3- Landscape phone to portrait tablet */
@media (max-width: 767px) { ... }

/* #4- Landscape phones and down */
@media (max-width: 480px) { ... }

If the browser screen size is 1200px, query #1 will be met since the minimum width required is 980px for it to take effect.

Now, let's resize the browser to as small as 250px... query #4 will apply because the maximum limit is 480px.

Below are simplified explanations of the queries..

@media (min-width: 980px) { ... }

Display if the screen size is 980px or larger

@media (min-width: 768px) and (max-width: 979px) { ... }

Display if the screen size is between 768px and 978px

@media (max-width: 767px) { ... }

Display if the screen size falls within 481px to 767px range

@media (max-width: 480px) { ... }

Display if the screen size is 480px or smaller

By utilizing these queries, you can ensure that one of them is always applicable, guaranteeing a desired outcome.

Answer №3

It can be confusing because there is a CSS property called min-width and also a media query with the same name:

@media (min-width: 420px) {...} /* The media query is predetermined based on screen size */

.element { min-width: 420px; ...} /* This sets the minimum width for the selected element */

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

steps to overlay an image over another in bootstrap:

I am trying to place an image over another image. Here is the code I have used: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8> <meta name="viewp ...

Enhancing the Search Form Minimum Width in Bootstrap 4 Navbar

Looking for a way to create a search form with a width of 100% and a minimum width within a Bootstrap 4 navbar? Check out my code snippet here. <nav class="navbar navbar-expand-md navbar-light bg-faded"> <a href="/" class="navbar-brand">Brand& ...

Resizing a scrollable list using React Refs and UseLayoutEffect

Essentially, my issue stems from having a scrollable list with a set maximum height of '100vh'. I also have a detail card beside the list that contains accordion components. When one of these accordions is expanded, it increases the height of the ...

Place the emoji where the cursor is located

My query was already posted on stack overflow but unfortunately, I did not receive a response. The issue revolves around 2 links named "add emoji 1" and "add emoji 2". As mentioned earlier, my question can be accessed here: Insert smiley at cursor positio ...

Adaptable Collection of 4 Images

I am facing a challenge in replicating eBay's 'Today' featured seller layout with 4 square images creating one box (refer to the image). I am using Bootstrap for this task, and I am finding it difficult to comprehend how to achieve this. I h ...

Restricting the number of lines within a paragraph in Angular 2

Is there a method to limit the number of lines in a <p> tag and add an ellipsis (...) at the end? Using character count for truncation doesn't work well as the width of the element varies according to device screen size. ...

The lightbox is triggered by clicking on a different div to display its content

Great news - my lightbox is up and running! Each image on my website corresponds to a different organization, and clicking on the image opens a specific lightbox. My question is: how can I have a lightbox configured so that only the trigger image is displ ...

Display an image when the link is hovered over within a table

I'm looking for a way to display a scanned receipt image when hovering over a link within a table. I've written some code but it's not working as expected. Can anyone help me figure out what's wrong? Here is the current state of my cod ...

Interested in mastering BootStrap for Angularjs?

As a PHP developer with a newfound interest in JavaScript, I took it upon myself to learn AngularJS and jQuery. However, I recently discovered that simply mastering Angular is not enough - Bootstrap is also necessary. My only issue is my fear of CSS; handl ...

Why is my Bootstrap table failing to be responsive?

I'm currently using Bootstrap 4 to incorporate a table into my website and encountering difficulties in making it responsive on mobile phones. Unfortunately, when viewing the table on a phone, it appears very zoomed out and not user-friendly. Can anyo ...

Issue encountered while trying to modify the color of a placeholder

Whenever I attempt to modify the placeholder's color: ::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } : ...

I can't seem to figure out why this isn't functioning properly

Upon examining the script, you'll notice the interval() function at the very bottom. The issue arises from bc-(AEfficiency*100)/5; monz+((AEfficiency*100)/5)((AFluencyAProduct)/100); For some reason, "bc" and "monz" remain unchanged. Why is that so? T ...

Creating a multiline textarea with ellipsis using ReactJS

Looking to create a component with a multiline textfield that displays ellipsis (...) for text overflow beyond 2 lines. How can I achieve this through CSS only without modifying the actual stored text? Utilizing the React component found at: link Appreci ...

What is the reason for index.html requesting the css or js modules as if they were directories when loading?

I am currently developing a server using expressjs: 'use strict'; var express = require('express'); var logger = require('morgan'); var path = require('path'); var bodyParser = require('body-parser'); va ...

Incorporating Image Caching Optimizations in CSS

My current dilemma I currently implement Cache Busting for my CSS files like this: echo "&lt;link href='stylesheet.css?" . filemtime('stylesheet.css') . "' />" My objective Now I want to achieve a similar approach for th ...

Changing the jQuery mobile side panel from push to overlay can be achieved by modifying the display settings when transitioning from a mobile view to a

I'm currently working on a project using jQuery Mobile (JQM) and jquery.mobile-1.4.5. My goal is to create a responsive side menu panel with a fixed header that works well on both mobile and tablet devices. For the design, I want the panel width to b ...

Enhancing the internal styling of ngx-charts

While working on the ngx-charts Advanced Pie Chart example, I noticed that the numbers in my legend were getting cut off. Upon inspecting the CSS, I discovered that a margin-top of -6px was causing this issue: https://i.stack.imgur.com/oSho1.png After so ...

Where can I locate a specific child element in this scenario?

Currently, I am exploring the possibilities of integrating AngularJS into my application and have encountered a question regarding the click event implementation. Within my HTML code: <div ng-click='clickMe()' ng-controller='testCtrl&ap ...

Tips for converting plain text output into HTML tags

I recently set up a contact form 7 on my website, and I'm trying to customize the message that appears after submission. However, I'm running into an issue when attempting to split the message into two different colors. I created a span class to ...

Instructions on implementing a floating label on a select2 dropdown menu

I am currently utilizing the select2 plugin for my select dropdowns on a webpage. I have multiple select dropdown boxes and I need to implement a floating label specifically for the selected dropdown. Despite my efforts and searches, I have not been able ...