How to properly do multiple CSS media queries - html

I've been trying to implement some media queries for a single page, basically, I want to hide and resize some elements based on the viewport dimension.
The issue I'm currently running into is that for some reason the 2nd media query does not seem to trigger.
as I understand for media queries to run I need to add the meta field (that's already done). And that they should follow a cascade order...so from max resolution to min resolution.
for some reason, only the first query is triggering and the second is not.
as I understand the "woman-image" class should be hidden when I reach a 1100px width,
and "h2.fin-text-navy2" should become yellow...It never happens...
I really appreciate the help on this.
#media all and (max-width: 1400px) {
h1.fin-text-navy {
font-size: 500%;
line-height: .9;
text-align: center;
padding-left: 0px;
padding-top: 50px;
margin-bottom: 25px;
}
.woman-image {text-align: left;}
h2.fin-text-navy2{
font-size: 250%; text-align: center;
line-height: 1.5;
padding-left: 0px;
margin-bottom: 50px;
}
.grid-container {
grid-template-columns: .8fr .5fr;
grid-auto-rows: minmax(500px, auto);
}
.buttonCenter{ width: 90%; padding: 16px; font-size: 35px; }
}//end media
#media all and (max-width: 1100px) {
.woman-image{ display: none; }
h1.fin-text-navy {
font-size: 500%;
line-height: .9;
text-align: center;
padding-left: 0px;
padding-top: 50px;
margin-bottom: 25px;
}
h2.fin-text-navy2{
font-size: 200%;
text-align: center;
line-height: 1.5;
padding-left: 0px;
margin-bottom: 50px;
color: yellow;
}
.grid-container {
grid-template-columns: .8fr 0fr;
grid-auto-rows: minmax(500px, auto);
}
.buttonCenter{ width: 90%; padding: 16px; font-size: 35px; }
}//end media

The problem seems to be one of a simple error in your CSS.
There are a couple of 'comments' end media which start with a double slash. This is not correct CSS. If those (in this case in particular the first one) are removed then the second media query works.
It can be worth putting your code through a validator - in this case I used the W3C validator and it came up with the errors clearly showing the lines they occured on.
It's also worth lookiing in the browser dev tools to see exactly what CSS is being picked up and used on an element.
Incidentally, the code worked fine without the meta field that you mentioned (at least on Edge/Chrome Windows10).

Try this
first of all as you well said you need the meta viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
Then in your CSS, I recommend adding all the styles for the biggest format available without media queries, and then create a media query for each desired format:
/*All sizes and big format (desktop)*/
.woman-image {text-align: left;}
/*Tablets*/
#media (max-width:1024px){
.woman-image {text-align: right;}
}
/* Tablet Portrait */
#media (max-width:768px){
.woman-image {text-align: center;}
}
/*Mobile */
#media (max-width:640px){
.woman-image {display: none;}
}
In the previous example the element with class .woman-image will have the following behavior:
Desktop: text-align: left;
Tablet: text-align: right;
Tablet Portrait: text-align: center;
Mobile: display: none;

You should try using min-width: 1100px in your first media query and use max-width: 1100px in your second media query.
Because if you are using max-width: 1400px than it means that "If your screen size is 1400px or less, than do the following task" , that's why for your screen's every value less than 1400px you are seeing your first media query at work.
Whereas if you use min-width:1100px in the first place than, than it would mean that "If your screen size is 1100px or more, than do the following". And than you should use max-width:1100px. so in this case 1100px will be a threshold value.
#media all and (min-width: 1100px){
//...do the task for 1100px and more
}
#media all and (max-width: 1100px){
//...do the task for 1100px and less
}

Related

Media queries on mobile do not work even after the viewport meta tag

I realize this is a duplicate, but the solutions I have tried do not work. For some reason, after adding the viewport meta tag, my media queries do not work on mobile. Does anyone know why?
Edit: The reason why was because my screen wasn't zoomed in at 100% width.
Here is the code:
#media only screen and (max-width: 1260px) {
.container {
margin-top: 300px;
}
h2 {
font-size: 3vw;
width: 800px;
}
p {
font-size: 2.5vw;
width: 800px;
}
}
#media only screen and (max-width: 550px) {
h2 {
width: 330px;
font-size: 5vw
}
p {
font-size: 4vw;
width: 330px;
}
}
Thanks.
I posted this a few months ago and now I realized it was because my screen was zoomed in. If you have this problem, make sure your window is set to 100% zoom.

When applying media queries, nothing is getting adjusted

I'm extremely confused because I am pretty certain that I have it set up correctly. It doesn't set the font to 10 pixels on my phone and when I adjust the screen. Am I missing something?
Here's the code:
.above-email-box {
/* aligns text to the center with position absolute */
text-align: center;
position: absolute;
margin-top: 90px;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
font-family: 'Glory', sans-serif;
font-family: 'Klee One', cursive;
font-size: 30px;
}
#media only screen (min-width: 768px) {
.above-email-box {
font-size: 10px;
}
}
Thanks.
.above-email-box {
/* aligns text to the center with position absolute */
text-align: center;
position: absolute;
margin-top: 90px;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
font-family: 'Glory', sans-serif;
font-family: 'Klee One', cursive;
font-size: 30px;
}
#media only screen and (max-width: 768px) {
.above-email-box {
font-size: 10px;
}
}
<div class="above-email-box"> Demo Text</div>
Logical operators
The logical operators not, and, and only can be used to compose a complex media query. You can also combine multiple media queries into a single rule by separating them with commas.
You have to define max-width & min-width consciously, sometime it can be confusing, min-width means when you add styles into it, it display the styles after the min-width condition is met, if you define min-width:500px your styles are applicable on screens width 500px and above not below
In order to run the media query for mobile device you need to change min-width to max-width like below
#media (max-width: 768px) {
.above-email-box {
font-size: 10px;
}
}
When your screen width is below 768px it will change font-size to 10px

Declaring CSS properties to display images across multiple screen sizes

I am using media queries to adjust the size of images according to the screen size they will be displayed at. I am creating 5 breakpoints.
Here is a link to what I have created so far, and here is a link to the repository of the files for the project.
I'm using Google Chrome DevTools to check how the project looks.
Everything runs well on desktop, iPad Pro, and all phone screens as you can see here:
iPad Pro display
Pixel 2 display
But one of the iPad options does not get rendered properly when I use media queries. Here is the anomaly: the image is not sized property and gets stretched.
iPad display
If I fix the iPad, the media query for iPad Pro becomes unresponsive and gets automatically displayed too wide. If I fix the iPad Pro, then the iPad one becomes unresponsive and gets displayed too wide or not at all.
I am using Visual Studio Code and there I have created three CSS files: index.html along with main.css, main1.css and main2.css.
To do that with the image, this is my html code:
<img class="img" src="logo1.png"
alt="My business' website logo"/>
<img class="imagerkan" id="imagery" src="logo1.png"
alt="My business' website logo"/>
This is my CSS at main.css:
#media screen and (min-width: 85.375rem) {
.img {
display: block;
margin-top: 7%;
margin-left: auto;
margin-right: auto;
height: 29.875rem;
width: 31%;
border-radius: 1.25rem;
}
#imagery {
display: none;
}
.imagerkan {
display: none;
}
}
#media screen and (max-width: 64rem) {
.img {
display: block;
margin-top: 21%;
margin-left: 17%;
margin-right: auto;
height: 11.875rem;
width: 68%;
border-radius: 1.25rem;
}
#imagery {
display: none;
}
.imagerkan {
display: none;
}
}
However, when I get to this next part, everything crumbles for one of the iPads:
main1.css:
#media only screen and (min-width: 64rem) and (max-width: 85.375rem) {
#imagery {
display: block;
margin-top: 14%;
margin-left: auto;
margin-right: auto;
height: 29.875rem;
width: 61%;
border-radius: 1.25rem;
}
.img {
display: none;
}
.imagerkan {
display: none;
}
}
This produces my intended outcome on iPad Pro.
But when I want to use another media query so my image displays correctly on iPad, the 768 x 1024 size, nothing I do works. I get the distorted image posted earlier.
The only reason I created an additional css file was because I was having the same problem with the iPad Pro version when I was including the media query for it in main.css. But when I created main1.css and placed my media query there for iPad Pro there, it worked.
At first I had the media query for iPad the 768 x 1024 size, in main1.css,
but thinking that just like having created main1.css for the iPad Pro
media query fixed the problem for it, I made main2.css for the iPad
media query. This time it did not work.
Here's main2.css.
#media only screen and (min-width: 51.5rem) and (max-width: 64rem) {
(824PX , the largest of the 7 phone sizes) (1024PX)
.imagerkan {
display: block;
margin-top: 14%;
margin-left: auto;
margin-right: auto;
height: 29.875rem;
width: 5%;
border-radius: 1.25rem;
}
.img {
display: none;
}
#imagery {
display: none;
}
}
I tried changing the media query for all phones in main.css to max 51.5 rem but this did not fix the problem either. One of the iPad media queries becomes unresponsive.

Setting a minimum width for responsive Bootstrap layout

I would like my Bootstrap layout to not act responsive when the width is 768px or smaller. You can view my current application HTML / CSS here, https://codepen.io/anon/pen/xXdYNa. I have had to make some changes to some of the standard Bootstrap styles so it might make things a bit more tricker.
Does anyone have any tips on how to achieve this by using CSS overrides, similar to what I have done here:
#media (min-width: 768px) {
.main {
padding-left: 250px;
padding-top: 89px;
}
}
Thanks!
How about just giving .container-fluid a min-width with 768px?
EDIT:
Adding the following worked for me:
.container-fluid {
min-width: 768px;
}
.main {
padding-left: 250px;
padding-top: 89px;
}
A min-width in combination with removing the media query which is around your .main should work.
https://codepen.io/anon/pen/EwmEXr
#media only screen and (max-width: 768px) {
.main {
padding-left: 0px;
padding-top: 0px;
}
}

Don't use specific CSS on smaller screens

I have some CSS that makes the whole site content have a marging to the left and right with 5em.
.content {
margin-left: 5em;
margin-right: 5em;
}
However, I want the margin to NOT work(not have a margin) or only have a margin with 1em when I enter the site on a mobile phone or touchpad. I have tried the following code but nothing happens. More specificly, I want this to be activated, and have no margin or only margin on 1em, on the screens that uses the initial scale I have set on every page. And I suppose that its only phones and pads.
#media screen
{
.content {
margin-left: 1em;
margin-right: 1em;
}
}
#media print
{
.content {
margin-left: 1em;
margin-right: 1em;
}
}
#media screen,print
{
.content {
margin-left: 1em;
margin-right: 1em;
}
}
You can use a media query with a specified width to achieve that :
#media (max-width: 640px){
.content {
margin-left: 1em;
margin-right: 1em;
}
}
See here common device width to choose the width you want : http://mydevice.io/devices/
Also don't forget to include the viewport meta in your <head></head> tag to make it works on your phone :
...
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
...
The syntax that you are using for media query is incorrect.
One must specify the width at which the media query will trigger in other words the width on the screen at which the code inside the media queries that will overwrite the default code.
the syntax for the #media property is
`#media not|only *mediatype* and (*media feature*) {
CSS-Code;
}`
So you must use this code to achieve the desired results :
#media (max-width: 667px){
.content {
margin-left: 1em;
margin-right: 1em;
}
}