Declaring CSS properties to display images across multiple screen sizes - html

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.

Related

How to properly do multiple CSS media queries

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
}

Mobile/Desktop media query not working on cell phone

Last night I worked on making my site mobile optimized. When a user is on a device with less than 684px it should show the mobile nav div and hide the sidebar div. I have tested this as working on my computer (by shrinking chrome like an accordion). I also checked https://www.google.com/webmasters/tools/mobile-friendly/?url=www.kisnardonline.com to see my status, but it says my site is not mobile friendly(I realize my links together is likely valid/viewport too). I will make the links more spread once I get it working overall. Thanks for any help!
Here is my code(http://www.kisnardonline.com/wp-content/themes/mytheme/style.css):
#wrapper {
display: block;
min-width: 684px;
max-width: 1200px;
width: 98%;
margin: 0px auto; }
#media screen and (max-width: 684px) {
#wrapper {
min-width: 0px; /* show under 684px - mobile */
}
}
#content {
width: 74%;
float: right; }
#media screen and (max-width: 684px) {
#content {
width: 100%; /* show under 684px - mobile */
}
}
#mobilenav {
display: none;
margin: 15px auto;
padding: 10px 10px 10px 10px;
border-radius:20px;
border:3px solid #a1a1a1;
background:#1C2939; }
#media screen and (max-width: 684px) {
#mobilenav {
display: block; /* show under 684px - mobile */
}
}
#sidebar {
display: block;
width: 23%;
min-width: 170px;
max-width: 210px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
float: left; }
#media screen and (max-width: 684px) {
#sidebar {
display: none; } /* hide under 684px - mobile */
}
Here is my desktop (greater than 684px view):
Here is my desktop (less than 684px view):
Here is my mobile view(Samsung Galaxy S6):
For responsive pages you need to add a viewport tag which tells the device how to display the content:
<meta name="viewport" content="width=device-width, initial-scale=1">
For your other question, the "request desktop site" won't work the way you have it set up. That feature is for if you have 2 separate sites(one for mobile and one for desktop). For instance the mobile site would be m.kisnardonline.com and would have different files than the desktop site.

DIV won't hide on mobile devices using media query

I am trying to make a certain page on our website mobile-friendly by hiding two large divs that aren't supposed to display as they are a) interactive and feature hover effects, and b) break up the flow of the page.
Currently testing on my LG G2 in Chrome, using
#media only screen and (max-width : 768px) {
div.example {
display: none !important;
visibility: hidden;
}
}
However it has no effect at all.
The CSS for "example" is:
div.example {
background-image: url('http://www.example.com/example.jpg'); border: 3px solid #ecf0f1;
height: 941px;
width: 1209px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
}
What am I doing wrong? As you can see by the dimensions they are too big for a standard mobile device... the page looks perfect on iPad & desktop resolutions, and even the forced requested desktop site version displays fine in mobile.
Any help appreciated, cheers.
Resolution of LG G2 is 1080 x 1920, which is larger than max-width: 768px. Your media query failed. Use:
#media only screen and (max-width : 1080px) {
}
for portrait orientation of the device. Even better, you can make use of: -webkit-device-pixel-ratio to target devices with different pixel ratio.
FYI, LG G2 has webkit-device-pixel-ratio: 3 (see Reference)
Update: Here is a general media query for high pixel ratio devices:
https://gist.github.com/marcedwards/3446599
<!doctype html>
<html>
<head>
<style>
#media only screen and (max-width : 768px) { div.example { display: none; } }
div.example {
background-image: url('http://scholarship-positions.com/internships/wp-content/uploads/2014/12/google.jpg');
background-repeat: no-repeat;
border: 3px solid #ecf0f1;
height: 941px;
width: 1209px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
}
</style>
</head>
<body>
<div class="example"></div>
</body>
</html>

Iphone responsive website issue

I am creating a new website, here is the www.qldmetals.com
Everything seems to be fine on website except its responsiveness on iPhone. In iPhone the logo sits over the navigation menu. I tried the following media query, but it doesn't seems to be working for me i.e
#media only screen
and (min-device-width : 320px)
and (max-device-width : 568px) { .logo a.brand { display: block !important; }}
I'm confuse, Is this error because of display method or anything else (like I have to use media query for any other class)
I appreciate your help and your valuable time.
Thank you.
I have seen your website and after that i have created some of my own style element which i am sharing it's only for media screen max width 480px
Following Code
#media (max-width:480px){
.logo a.brand {
display: block !important;
height: auto;
line-height: 30px;
margin: 0;
overflow: hidden;
padding: 0;
width: auto;
}
.logo a.brand img {
float: none;
height: auto;
margin: 2px 8px 2px 0;
width: 100%;
}
header .logo{
float: none;
max-height: none;
text-align: center;
width: 100%;
}
}

Responsive Menu Error

Hello when I go and view my website my mobile view for my menu is still showing. I want it to go back to normal. after 768px;
I have looked every where can find out why its doing it like that.
http://codepen.io/mwbcomputers/pen/jvpcq
You have display: none; within your media query for 1024
#media only screen and (max-width: 1024px) {
#container {
width: 90%;
margin-left: auto;
margin-right: auto;
}
.nav {
display: none;
}
// more css
Remove the following code from the media query
.nav {
display: none;
}