Altering Styles Depending On Device Width Bug

Hiya Everyone,

So I have two ways things are altered upon window resize;

Via Jquery

$(window).resize(function () {
      var PageWidth = $('body').innerWidth();
      if (PageWidth > 979) {
      } else {

This is used because the person may expand content then resize, it needs to reset the top navigation.


@media screen and (max-width: 979px) {
      /** THE CSS STYLES **/
@media screen and (min-width: 980px) {
      /** THE CSS STYLES **/

My friend is testing my website progress on his Blackberry z10 and on upon page flip, there has been many problems of which I believe is due to the above methods where on some browsers in my methods they include the scrollbar and some do not.

if (PageWidth > 979) {

If I reduce this by 20px making it 959 it works but breaks other devices such as my computers Google Chrome.

I’m at a bit of a loss and I am unsure what to do.

Best Regards,


This is one of the problems with chasing devices and a simpler approach would be to adjust the design at the point the menu needs adjusting. However if instead you reversed the logic and targeted your resize at less than 980px then you would automatically collect 979px and 959px.

I don’t however see the need for the javascript resize as you can control the menu through css and let the css media queries do the work. The javascript should do the opening and closing of the menu as required but no need to to do a resize if you have code it correctly.

Here’s a basic example I just pulled from a page of mine. The mobile nav appears at the smaller width as required at the point the nav needs to change. The js just handles the menu opening and closing but the media queries handle the styling at the different sizes.

Because JQuery’s slide and fade functions implement inline css and therefore upon resize, I need this to reset to default.

I’m having a particularly difficult bug where on my friends Blackberry z10 loading in portrait he sees the mobile version, however clicking on it shows the none mobile version of which he should only see in landscape mode.

Yes if you look at my code in my example the !important over-ride in the media query takes care of any inline styles.

I’m having a particularly difficult bug where on my friends Blackberry z10 loading in portrait he sees the mobile version, however clicking on it shows the none mobile version of which he should only see in landscape mode.

If you can send me the link to the page (or PM it to me if you want it private) then I’l take a look and see if I can recommend a different approach. Almost always these device chasing set ups fail at some stage or in particular devices which is why I advise not coding for devices but coding the design to fit all window sizes instead. The breakpoints are controlled by the design and not the device.

How do I implement this? I just tried adding ‘!important over-ride’ to my media queries and it looked odd…

Define odd :slight_smile:

It looks ok to me (apart from the accordion style at smaller screens but that’s a design question rather than an issue).

Where’s your viewport meta tag ?

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

Without the meta tag mobile devices will just scale pages to fit and you will not get what you expect. The meta tag tells mobile devices not to ‘scale layouts to fit’ and that you will be controlling the design for smaller screens instead. If you omit the meta tag them mobile devices assume that the design is 980px width and will scale 980px to fit in the smaller viewports which is a very hit and miss affair and you end up with layouts too small to read.

Pleas don’t do things like this:

html, body {
	padding: 0;
	margin: 0;
	width: 100%;
	min-height: 2000px;

The 100% is redundant and stops you applying padding to the body unless you change the box model and also no one wants a 2000px vertical scroll when they don’t need one. :slight_smile:

The only rules you really want on the html element is to negate margin,padding and maybe height:100% for certain layouts.

If you apply a background image to the html element then the body shrinks to content height so the benefit you may think you are getting from using a spare element is lost. These days you can use multiple images on the body element for modern browsers and the body will paint full height without any special rules as they automatically propogate to html.

This is what I tried;

@media screen and (max-width: 979px) !important over-ride {

I’m now guessing you meant this?

display:none !inportant;

But where I added these, it broke my accordion.

This fixed the mobile issue :slight_smile:

No that’s invalid :slight_smile:

I’m now guessing you meant this?

display:none !inportant;

Close :slight_smile: It should be !important (you had as typo which would invalidate the rule). Of course where you apply it will refer to your code and structure but as you can see in my demo I applied it inside the media query for large screens to make sure that any inline css was over-ruled. There is no need for the js resize at all and indeed will complicate the issue with both js and media queries to accommodate.

This fixed the mobile issue :slight_smile:

Some progress at last :slight_smile:

#TopNavigation ul li ul {
	display: none !important;
	position: absolute;
	width: 916px;
	height: 200px;
	left: 50%;
	margin-left: -461px;
	background: #f7dd71;
	border-left: 1px solid #d1a800;
	border-right: 1px solid #d1a800;
	border-bottom: 1px solid #d1a800;
	border-bottom-right-radius: 10px;
	border-bottom-left-radius: 10px;
	-webkit-border-bottom-left-radius: 10px;
	-webkit-border-bottom-right-radius: 10px;
	-moz-border-radius-bottomleft: 10px;
	-moz-border-radius-bottomright: 10px;

Upon having it on the mobile version expanded and then resizing, this does remove what I was doing using JQuery, however this makes it so that you can no longer drop the menu.

All is working now thanks to mqGenie, all I’m stuck with now is centering my images in my links on the mobile version.

Try the following changes:

#TopNavigation ul li {
    [color=red]width:100%;[/color]  /* unnecessary */