Media Queries and Responsiveness questions

My site in progress: www.gptrsn.com/code/test.html

I used some responsive grid framework originally to get started but have tweaked so many things I’m not sure where to start.

I have a media query that at 480px the site is full width of the boxes. On the web you will see at a certain breakpoint (480px) the 4 columns collapse into one. (Header and footer are obviously not responsive at all)

On my phone in landscape and portrait the query telling it go 100% width at 480 is making the screen flush with the sides of the grid. On mobile it’s showing the full 4 column 4 row site as it would look on the web, but I would prefer it to be 1 column on the phone.

Ideally I would like to break from 4 columns to 2 columns to 1 - http://colly.com/ this site is a perfect example of how I’d like it to work.

My width of my grid is wider than theirs so the breakpoints would be a little different.

My questions are: what do I put for media queries to get this effect? Do I need to alter the CSS for the Divs and Spans of the columns or just put in new media queries?

I’d also like to have a media query that I could have a alternate mobile style/breakpoint style for the header and footer that is more mobile friendly.

I’m new to coding so I’m sure there could be some redundant and wonky coding in there. I’d like to use strictly CSS if possible.

Thanks in advance!

Hi,

First things first and mobile devices will not obey your media queries until you tell them to do so and that is done by using the viewport meta tag which tells mobiles that you will be handling the width of the layout.


<meta name="viewport" content="width=device-width, initial-scale=1.0">

Once you have that in place then the media queries will kick in just like they do on the desktop.

Don’t code with fixed widths but code with fluid design in mind. For example your layout width should be max-width:1080px and not width:1080px. This allows it to appear at the width you wanted on larger screens but then to scale smaller for smaller devices.

Forget devices altogether and just look at your design. If you close the window (when you have the fluid width in place) there will come a point where the design looks awkward and doesn’t fit. It’s at these points that you insert your media query and make your adjustments. Do this along the whole range of big to small and you will collect all devices on the way automatically. With careful planning you will only need a few breakpoints.

For example for your middle section I would do this.



.section,header{width:100%;max-width:1080px}
.col.span_1_of_4 img {
 width:100%;
 height:auto;
}

@media screen and (max-width:760px) {
nav,.logo {float:none;margin:20px 0;}
}
@media screen and (max-width:660px) {
.group{text-align:center}
.col.span_1_of_4{
float:none;
display:inline-block;
vertical-align:top;
width:40%;
margin:0 10px 10px;
}
}

@media screen and (max-width:460px) {
.col.span_1_of_4{
display:block;
width:250px;
margin:0 auto 10px;
}
}


These are just arbitrary breakpoints suited to your design only and you need know nothing about device widths as such,

Add that code to the end of your css so that it over-rides what went before and you will see how immediately you have catered for every device from 250px to 1080px. It will need further tweaks of course but the basics are there. You probably also want to use classes rather than styling things like section and header directly because they may be used elsewhere.

Also consider using a click menu for your nav rather than a hover menu becomes mobiles hate hover (because there is no such thing) and although some mobiles can imitate hover a little it very often fails or is awkward to use. Look at using some js for the smaller viewports for the menu to make the menu a click menu or just make it a click menu from the start.

Usually on mobile you hide the menu and display the hamburger icon instead anyway as shown on the bootstrap home page (just close the window smaller until the nav disappears and is replaced with the hamburger icon).

Ah, thank you so much! With just the changes you gave me I got it very close to how I want it. I will play with the breakpoints. Can I do the hamburger icon effect without js? I’d like to keep the site pure CSS if possible. Thanks again.

I’ve been playing with the code you gave me but I can’t seem to find where to define the breakpoints exactly –

For example I’d like the first breakpoint to be 4 columns turns into 2 at when the screen is 995px wide

I’d then like the 2 columns to collapse into 1 column at around 541px wide. www.colly.com is the functionality I’m going for.

In the code you gave me which works pretty well:

@media screen and (max-width:760px) {
nav,.logo {float:none;margin:20px 0;}
}
@media screen and (max-width:660px) {
.group{text-align:center}
.col.span_1_of_4{
float:none;
display:inline-block;
vertical-align:top;
width:40%;
margin:0 10px 10px;
}
}

@media screen and (max-width:460px) {
.col.span_1_of_4{
display:block;
width:250px;
margin:0 auto 10px;
}
}

I don’t know where to define the breakpoints exactly, when I resize to 460, 760 and 660 I don’t see anything happen so I’m not sure what those max width-tags are doing. Not sure if that makes any sense, if you could just tell me where I put the px width for the breakpoints I want. Thank you

Hi,

You are missing the point a little and although www.colly.com does act responsively it is effectively a series of fixed width designs which is ultimately more work than it should be.

If you had copied the code I gave you exactly your design would automatically have scaled throughout the whole range without overlap. I made sure that your images scaled down with the screen size so that you don’t get any overlap as in your current attempt and then at suitable breakpoints I changed the percentages around to change the column numbers to suit. It makes for a much smoother experience and caters for all without trying too hard.

You can’t have any fixed widths greater than your breakpoints because then its not responsive and indeed mobile devices will shrink your layout to accommodate the large element thus breaking the mobile view completely.

I’m not sure what you are doing with all those images anyway as they should simply be in a list and not nested sections ( a section denotes a new section not a single element). You also seemed to have grouped 4 images at a time which makes it awkward (impossible) to get three across.

Just use a simple linear list.

<ul>
<li><img></li>
<li><img></li>

etc…

Then you can allow them to wrap into whatever width is available.

For example if you hadn’t grouped your design then you could use code like this to create 4,3,2,1 columns as the width gets smaller.



@media screen and (max-width:1080px) {
.section,header {width:800px}
.col.span_1_of_4{
 width:30%;
 float:none;
 display:inline-block;
 vertical-align:top;
 margin:0 10px 10px;
}
}
@media screen and (max-width:800px) {
.section,header {width:580px}
.col.span_1_of_4{
 width:45%;
}
}

@media screen and (max-width:580px) {
.section,header {width:auto}
.col.span_1_of_4{
 width:250px;
}
}

So what this code is doing is that as it nears your width of 1080px it reduces the wrapper widths to a size that will accommodate 3 across and then changes the width of each item so that only 3 fit in a row. The same thing happens as the screen gets smaller and approaches the new width it then triggers another change in the wrappers width and resizes the items so that only 2 will fit across and so on… This is what I meant when I said you are basically just creating a series of fixed widths unlike the more fluid approach in my first example.

So to recap:

If you want fixed widths then you must do this:

Say your default width is 1080px - the you set a media query at 1080px (or 1079px if you don’t want it to break until the actual limit) and in that media query you reduce the widths of any wrappers that have been set to 1080px and you make their new width wide enough to accommodate three of your items across. This means that your span_1_4 must also be set to be a third of the width and not the 25% (approx that it was set before).

Then you just do the same as the screen width approaches this new width (say 800px) then trigger another wrapper width change and an item width change also. Continue until you can only fit one column across.

You will need to reorganise the grouping of those images though because you don’t want nested blocks of 4. Keep it simple and a linear list is less html and easier to manage. (You can group the whole thing in a section if needed.)

Ah thank you so much, I did use your code earlier but I didn’t put it far enough into the CSS, I put it at the very bottom now as you originally said and now see it working. I will play with it some more.

So to recap you are suggesting instead of the divs and nested columns I rewrite the HTML for the grid in a simple UL/LI list?

And you’re saying I could use a media query similar to this:

@media screen and (max-width:760px) {
nav,.logo {float:none;margin:20px 0;}
}

To define a different style for the header at a certain breakpoint/mobile view (i.e. hamburger icon replaces all the links at a certain breakpoint and is shown on mobile instead of the hover links)

I really appreciate your help, I’m new to coding so I’m kind of just learning as I go along so bear with me. I’m sure I’m making certain things harder than they should be. I will keep looking into your advice.

Thanks again!

For those images which are all the same size a linear list would be more semantic as they are a list of images and not each a new section. I don’t see the need for a grid for such a simple structure of those images especially when you have them in groups of 4 (unless that was a coding error on your part).

And you’re saying I could use a media query similar to this:

@media screen and (max-width:760px) {
nav,.logo {float:none;margin:20px 0;}
}

Yes just change things around to suit the smaller or larger viewport widths as required. The exact width you do this will vary depending on your design so forget about devices for now.

To define a different style for the header at a certain breakpoint/mobile view (i.e. hamburger icon replaces all the links at a certain breakpoint and is shown on mobile instead of the hover links)

If you don’t want to use JS then instead of the hamburger menu display the real menu but have all the sub options open. Usually each menu line would be 100% wide and you can do this simply by not floating them and then setting them to display:block and width auto (usually it better to centre the text also). The sub menus can be put back in the flow (not position absolute and not hidden) and you can style them in a suitable looking sub menu type colour :). This means that mobile users can navigate easily as hover menus are a nightmare on mobile.

I really appreciate your help, I’m new to coding so I’m kind of just learning as I go along so bear with me. I’m sure I’m making certain things harder than they should be. I will keep looking into your advice.

No worries, a lot of people still get this wrong and to be honest we are all learning the best approaches and pitfalls almost daily :slight_smile: