How to express a width open interval with css media query? - css

I have 3 open intervals of screen width now. They are (0,600], (600,800) and [800,1000] and each has a diffferent css style. For example:
#media screen and (max-width: 600px) {
background: #000
}
#media screen and (min-width: 800px) and (max-width: 1000px){
background: #eee
}
But how should I express (600,800) with ccs media query ?

#media screen and (max-width: 600px) {
background: #000
}
#media screen and (min-width: 800px) and (max-width: 1000px){
background: #eee
}
#media screen and (min-width: 600px) and (max-width: 800px){
background: #fff
}
You can use 3rd media query.

Start with a default value, which is the value of the mobile first. Then modify the values from a minimum with and up:
HTML:
CSS:
.bg {
width: 100vw;
height: 100vh;
background-color: #000;
}
#media screen and (min-width: 600px){
.bg {
background-color: steelblue
}
}
#media screen and (min-width: 800px){
.bg {
background-color: tomato
}
}
#media screen and (min-width: 1000px){
.bg {
background-color: #2c3e50;
}
}

#media (max-width: 1000px) {
background: #eee;
}
#media (max-width: 800px) {
background: #fff;
}
#media (max-width: 600px) {
background: #000;
}
This order will execute based on screen size and CSS will be applied accordingly.

You can define these interval for media queries link below:
#media screen and (min-width: 801px) and (max-width: 1000px){
background: #eee
}
#media screen and (min-width: 601px) and (max-width:800px){
background: #cccccc
}
#media screen and (max-width: 600px) {
background: #000
}
Now
The first media query will work from 0px to 600px
The second one will work 601px from to 800px
And the Third one will work from 801px to 1000px

Related

Why my media query for 768px and 576px not working

I am trying media queries, but they won't work on 768px and 576px. I tried the minimum width but it also does not work.
.changing-color {
background-color: powderblue;
width: 100vw;
height: 30vh;
font-size: 3vw;
}
#media screen and (max-width: 1400px) {
.changing-color {
background-color: chartreuse;
}
}
#media screen and (max-width: 1200px) {
.changing-color {
background-color: blueviolet;
}
}
#media screen and (max-width: 992px) {
.changing-color {
background-color: brown;
}
}
#media screen and (max-width: 768px) {
.changing-color {
background-color: darkorange;
}
}
#media screen and (max-width: 576px) {
.changing-color {
background-color: darkkhaki;
}
}
<div class="changing-color"></div>
Your CSS is correct, it works in this pen. Maybe you are not resizing your screen? Because that is exactly what these media-queries are for.
This snippet below is limited in width, so it will show probably darkorange depending on how you are viewing this page. On mobile it might even show darkkhaki.
.changing-color {
background-color:powderblue;
width: 100vw;
height: 30vh;
font-size: 3vw;
}
#media screen and (max-width: 1400px){
.changing-color {
background-color:chartreuse;
}
}
#media screen and (max-width: 1200px){
.changing-color {
background-color:blueviolet;
}
}
#media screen and (max-width: 992px){
.changing-color {
background-color:brown;
}
}
#media screen and (max-width: 768px){
.changing-color {
background-color:darkorange;
}
}
#media screen and (max-width: 576px){
.changing-color {
background-color:darkkhaki;
}
}
<div class="thing changing-color"></div>
Have you added a viewport meta tag like this ?
<meta name="viewport" content="width=device-width,initial-scale=1">
This is needed because by default, most mobile browsers lie about their viewport width.
Reference Beginner's Guide to Media Queries

when i set #media between 600~767,100~599 there is missing on 599.xx

Now I'm learning about CSS
when I type like this
.pc{
color: red;
font-size: 50px;
background-color: pink;
}
#media (min-width: 600px) and (max-width: 767px) {
.pc {
color: blue;
font-size: 20px;
background-color: yellow;
}
}
#media (min-width: 100px) and (max-width: 599px) {
.pc {
color: green;
font-size: 10px;
background-color: gray;
}
}
for example
in the 599.XXXpx (599.123, 599.284)
At this point
the color is go back to red and pink
How can I solve this?
Most of browsers will not display fraction pixel. A pixel is a smallest unit to display. So you do need to be worry about the breakpoint you mentioned. It is not phisically happens.
In essence, I would recommend you to use the same number for both media queries, and then order the rules so the one that you want to win goes later.
If you would like to keep it blue and yellow, then you will have to change the order of the rules:
#media (min-width: 100px) and (max-width: 600px) { /* … */ }
#media (min-width: 600px) and (max-width: 767px) { /* … */ }
But if you'd like to keep the green and gray colors, keep the current order:
#media (min-width: 600px) and (max-width: 767px) { /* … */ }
#media (min-width: 100px) and (max-width: 600px) { /* … */ }

Why are my media queries responsive on my monitor, but not responsive on my phone or ipad?

solanosprinklers.net
Please refer to the site. I have different color settings so I can see where the resolution changes when decreasing the window size on my PC. Check it out, it responds. The problem is that the phone and tablet are both not responding. What am I missing?
Here are the media queries in the CSS file
#media (min-width: 1025px) and (max-width: 1199px) {
#new_customer {background: green; margin-left:-20% !important; }
}
#media (min-width: 768px) and (max-width: 1024px) {
#new_customer {background: black; margin-left:-20% !important; }
}
#media (max-width: 767px) {
#new_customer {background: red; margin-left:8% !important; }
}
#media (max-width: 480px) {
#new_customer {background: blue; margin-left: 21% !important; }
}

Why the css not applied when orientation change?

I am using media query. I have device having width 360, height:640. Actually I add border to check the css applied or not when orientation change.
But it only show border red when I change the orientation. It is not showing other border why?
#media screen and (max-width: 360px), screen and (max-height: 640px)and (orientation: portrait) {
.subcontent_h {
height: 200px;
overflow: auto;
margin-top: 10px;
border: 1px solid deeppink !important;
}
}
#media screen and (max-width: 640px) , screen and (max-height: 360px)and (orientation: landscape) {
.subcontent_h {
height: 200px;
overflow: auto;
margin-top: 10px;
border: 1px solid red !important;
}
}
Order of Specification: As a last resort, when all other conflict
resolution specifications cannot determine which style should take
precedence, the last style specified will be the style used.
.subcontent_h{
height: 200px;
overflow: auto;
margin-top: 10px;
}
#media screen and (max-width: 640px) , screen and (max-height: 360px)and (orientation: landscape) {
.subcontent_h {
border: 1px solid red;
}
}
#media screen and (max-width: 360px), screen and (max-height: 640px)and (orientation: portrait) {
.subcontent_h {
border: 1px solid black;
}
}
DEMO
#media screen and (max-width: 360px), (min-device-width: 360px) and (max-device-width: 640px) and (orientation : portrait) {
/* CSS */
}
#media screen and (min-width: 360px) and (max-width:640px), (min-device-width: 360px) and
(max-device-width: 640px) and (orientation : landscape) {
/* CSS */
}
First Use Modernizer to check weather your device support media queries
Make sure you have added below meta tag in head section
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
Try Below way
#media screen and (max-width: 360px) and (max-height: 640px)
{
.subcontent_h {
height: 200px;
overflow: auto;
margin-top: 10px;
border: 1px solid deeppink !important;
}
}
#media screen and (max-width: 640px) and (max-height: 320px)
{
.subcontent_h {
height: 200px;
overflow: auto;
margin-top: 10px;
border: 1px solid red!important;
}
}

Multiple #media tags not working

#media only screen and (min-width: 767px) and (max-width: 2000px) {
html { background-color: green; }
}
#media only screen and (min-width: 481px) and (max-width: 766px) {
html { background-color: green; }
}
#media only screen and (min-width: 321px) and (max-width: 480px) {
html { background-color: green; }
}
#media only screen and (max-width: 320px) {
html { background-color: green; }
}
html {
background-color: blue;
}
I'm using opera, 1920x1080 screen. The first #media tag works, the background changes to green when opera is at 100% zoom.
Changing zoom to 90% makes the background blue already...and it stays blue the whole time even at 10% zoom. Why is that so?
The first #media tag seems to be working, the others don't. And even so the first tag doesn't work properly (90% * 1080px > 767px; so the color should be green while at 90% zoom but it's not).
Move your single html definition to the top, you can also reduce the media queries to just use max
html {
background-color: blue;
}
#media only screen and (max-width: 2000px) {
html { background-color: green; }
}
#media only screen and (max-width: 766px) {
html { background-color: red; }
}
#media only screen and (max-width: 480px) {
html { background-color: black; }
}
#media only screen and (max-width: 320px) {
html { background-color: white; }
}
http://jsfiddle.net/Y5tLf/

Resources