1

I have a site that I am developing, that uses a grid system In IE7, when the window is resized, one column "pops" down below the other. This is not a massive issue, but as there is no margin at the top/left, the layout is fairly unreadable.

So how can I apply a class to a

<div>

only when the window (viewport?) is resized to below a certain width?

eg

width > 500px = class="smallerwindow"

I then can style that class appropriately.

I have jQuery, modernizr etc.

Thanks,

Harley

1
  • 3
    You may want to take a look at Respond.js (github.com/scottjehl/Respond) that enables media query support for IE6-8. Commented Oct 30, 2012 at 14:39

3 Answers 3

2

As per my comment above, you can use respond.js to enable media query support for IE6-8.

Conditionally include the respond.js script:

<!--[if lte IE 8]>
<script type="text/javascript" src="~/Scripts/respond.min.js"></script>
<![endif]-->

NOTE: Recommended to include respond.js in head as will impact dom rendering

Then define your CSS as follows:

.myWindow { width: 500px; }

@media (min-width:1200px) {
    .myWindow { width: 400px; }
}

@media  (min-width: 768px) and (max-width:979px) {
    .myWindow { width: 300px; }
}

Just some sample viewport sizes and widths; tweak accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorted! Was already a responsive grid, so only needed a copy,copy paste... Thanks
1

I would do something like this..

$(window).resize(function() {

    if((window).width() > 500 && !$("div").hasClass("smallerwindow")){

        $("div").addClass("smallerwindow");

    } else if((window).width() < 500 && $("div").hasClass("smallerwindow")) {

        $("div").removeClass("smallerwindow");

    }

});

Check for the window size being greater than 500, then if the div dosen't have the class add it, else if the window size is smaller than 500 and the div has the class remove it.

1 Comment

Thanks... Seems robust. Used respond.js though.
0

jQuery:

if ($(window).width() > 500) {
   $("#myDiv").attr("class","smallerWindow");
}

This checks the Viewports width, and resets the current 'class' attribute of

<div id='myDiv'> </div>

2 Comments

Thanks, but I used the below respond.js solution. Will come in useful again, I'm sure. :)
that's a more 'responsive design' and better architected solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.