I am trying to create @media code to make my website mobile friendly by getting rid of my header image and nav bar but it’s not working and I can’t see what the problem is.
the area for the header that I want to not display in mobiles. There’s no div at the end as it closes the page’s container.
<div class="container">
<div class="grid-container">
<div class="grid-item1">
<a href="index.html"> <img src="rabbits-logo.svg" alt="EcoBunny's bunny logo" class="logo"></a>
<!-- style="height: 150px; width: 170px;"></a>
height:="150" width="170"> -->
</div>
<div class="grid-item2">
<img src="new-head-3.jpg" alt="photo of top part of a house for ecobunny.co.uk EPC and retrofit assessments"class="hd-photo" >
here is the css code for mobiles:
@media screen and(max-width: 800px){
.logo {display: none;}
.hd-photo {display: none;}
/* I tried this as it is the div that logo is in. It didn't work*/
.grid-item1 img {display: none;}
}
out of desperation I changed the screen size from px to rem. That didn’t work either.
Out of frustration, I changed the rem from 50 to 5000. It worked.
I then changed the max-width to 67rem (1000px) that also worked on my laptop simulators but not on my phone. My phone is 720px (45rem).
I’ve tried cleaning out the cache, memoery etc on my phone but am still not able to get rid of the images.
Hello, make sure your HTML is correctly structured and contains all the necessary elements. It seems you have a small code snippet, so make sure it is within a proper HTML structure.
I doubt your phone is 720px (css pixels). Its more likely to be half that assuming double density. My iphone is 375px css pixels but its resolution is actually double that. Mobile devices map a css pixel to one or more of their device resolution pixels in order to get the crisp displays.
You need to use the css px measurements in your media queries but of course the layout should have changed at 720px and smaller if implemented correctly.
Before you do that you need to confirm that you have added the viewport meta tag that Sam mentioned as the one you had was wrong and the media queries will not work in the same way for mobile devices.
I also notice that your site starts to break at 1112px because your big header image isn’t scaling with the page. If you add this code then it will scale up and down much better.
.grid-item2 img{
width:100%;
height:auto;
}
These days you need to be device agnostic and instead concentrate on when the design needs to change rather than some ‘guessed’ device size as there are hundreds and hundreds of devices around now and all at different sizes. What you need to do is open the browser on your desktop and slowly close it 1px at a time until you get down to 320px. If at any point during that process you notice that something is misaligned or a horizontal scrollbar appears on the viewport then at that point you need to add the media query and make it fit better.
With a few well chosen media queries and a responsive approach you end up catering for all known device and all unknown devices without knowing anything about them.
Lastly, I notice that on your online site you still have the old viewport meta tag in place so you haven’t yet updated the changes. If you add the code and the media query to your live site then we can see first hand where its going wrong
I am working on a test page at the moment . When I’ve got that right I will upload it to my website. Hopefully. it will save a lot of mess and frustration!
I did have px but I noticed on some blog sites that they use rem. I’ll go back to px.
I’ll work through your advice and see how things go.
I wouldn’t call myself a programmer. I used to program in PASCAL, CoBOL and C but that was more than 30 years ago.
As I have the time and between jobs, I thought that I would have a look at HTMl and CSS as I think that it’s better than using drag and drop site builders.
The threshold for the banner seems much too low, and you can change the logo width to something like this to keep it within the box until the 600px thresh hold is hit.
You could also move the header image to a background-image (which is more appropriate semantically), and set the size to contain and it’ll fill the space until your threshold is filled.
This will resize the banner more effectively and size it accordingly. This will require you to change how the menu is built though as it now overlays the background, where you’ll need to place it better (using flex/grid would probably be the easiest way to do that)
A quick play gets you close. To get to this point, you’ll need to add:
A position:relative to the .grid-item2 class
A position: absolute to the .nav-bar class
A display: flex to the top-nav class
.grid-item2 {
background-image: url(https://ecobunny.co.uk/new-head-3.jpg);
background-size: contain;
position: relative;
}
.nav-bar {
position: absolute;
bottom: 0; /* sets it to the bottom of the grid-item2 container */
}
.top-nav {
display: flex; /* this will allow you to get rid of all the floats too */
}