0

To Change the Div width according to window size, I googled and attempt with below JQuery script (combined answer from stackoverflow posts):

JQuery script:

<script>  
function checkWindowSize() {  
    if ($(window).width() > 1800) {  
        $('#divwidth').addClass('large');  
    }  
    else {  
        $('#divwidth').removeClass('large');  
    }  
}  
$(window).resize(checkWindowSize);     
</script>  

CssClass:

#divwidth{  
    width: 1200px;
}  
#divwidth .large{  
    width: 2200px;
} 

And here's my html using the class:

<div style="text-align: left; margin-left:auto;margin-right:auto;  overflow-x:auto;" ID="divwidth">

<!-- my gridview here -->
</div>  

It doesn't seems to work to set the Div width. changing giving script type "text/Javascript" does not help either.

4
  • I believe this is another CSS Specificity problem. Commented Jan 21, 2016 at 18:41
  • @Draco18s thx i've added css tag Commented Jan 21, 2016 at 18:42
  • I also linked that page because that page may help you to solve the problem on your own. #divwidth is more specific than .large so .large is being ignored. Commented Jan 21, 2016 at 18:45
  • You could handle the screen size with the viewport meta tag. This tag is very useful when designing a responsive website. Can be combined with relative measurements like 100% or 5em to control the experience on different sized devices. Commented Jan 21, 2016 at 19:06

3 Answers 3

3

I don't think you need jQuery for this. You could set your width to percent like in this jsfiddle https://jsfiddle.net/n3gw5613/

.divWidth {
    float:left;
    width:50%;
    margin:0 25%;
    padding:15px;
    background:#aa0000;
    color#000;
}

Or use media queries like in this jsfiddle https://jsfiddle.net/90jaxah9/1/

.divWidth {
    float:left;
    width:350px;
    margin:0 25%;
    padding:15px;
    background:#aa0000;
    color#000;
}
@media only screen and (max-width:320px) {
   .divWidth {
       width:100px;
       margin:0;
    }
}

Drag and resize your browser to see the change

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

3 Comments

The moment i change the width from a fixed number to %, it expands as the gridview I had is longer than the page's width.
How about css media queries like in the seconds example?
The css media queries wasn't working at the time i attempted with my code. However it works after I tried with Ivan's code and then I realize it would work with your 2nd answer as well. Wish I could accept both answers
2

The problem you are experiencing with @kpucha's anwer is that you implemented his advice incorrectly. In yor CSS there is still a whitespace (' ') changing the meaning of CSS. Your code is:

#divwidth .large{  
    width: 2200px;
} 

Where it should be:

#divwidth.large{  
    width: 2200px;
} 

The difference is that your rule applies to descendants of #divwidth with the class large. Instead it should apply to #divwidth elements that also have the classlarge.

The best way however would be a CSS media query like so:

#divwidth {
    width: 1200px;
}
@media (min-width:1800px) {
    #divwidth {
        width: 2200px;
    }
}

This would result in the div being 1200px in width when the viewport is smaller than 1800px.

2 Comments

Indeed I didn't see the white space -_-
Thanks! using Css media query does quickly solve the problem
2

You have a problem with the CSS selector.

You have this:

.large #divwidth{  
    width: 2200px;
} 

You are saying that you want the element with the id divwidth WITHIN THE ELEMENT with the class large

You need this:

#divwidth.large{  
    width: 2200px;
} 

This way you are saying that you want the element with id divwidth AND the class large

5 Comments

I updated my code (as well as the post here) and no changes is made
Look here jsfiddle.net/tv0gLaLb it is working, maybe you are not triggering the event, see the comments in the code.
Thanks Kpucha, I checked and you're correct that the function didn't trigger.... I assume it should trigger the moment window (webpage) is resize? Thou CSS media query solved the issue, i'm curious why my jscript does not fire off
Yes the event .resize() is triggered when the window size change. If you load the js but don't change the size of the window don't recognize and event of resizing and don't trigger the function.
Something is preventing me from calling the function. A simple alert() wouldn't run when window size changed. I will look into it now that you confirmed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.