1

How to detect mobile width and change my css to it's current window width using jquery?

For Example

.page {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    float: left;
    margin: 40px 0;
    max-width: 480px;
    min-height: 720px;
    height: auto;
    min-width: 360px;
    padding: 10px;
    width: 100%;
}

and then I have this function but I am not sure if it's really working.

function responsiveFn() {
    width = $(window).width();
    document.getElementById('widthID').innerHTML = width;
    jQuery('.page').css({
        max-width: width
    });
}

Thanks for the answer in advance! :)

15
  • That should work fine except max-width inside css should be covered in ' ' !! what you are getting as now? Commented Aug 25, 2015 at 6:41
  • Mate, it's still not adjusting. I am using this for mobile. Actually I have Div (wrapper) inside div (index): <div id="wrap"> <div data-role="page" id="index" class="page"> #wrap { box-sizing: border-box; height: auto; overflow: hidden; width: 1200%; } Because it's a horizontal scrolling, I divided my pages in this 1200%. Commented Aug 25, 2015 at 6:50
  • When you are calling the above function? Commented Aug 25, 2015 at 6:51
  • Inside the head tags. Commented Aug 25, 2015 at 6:53
  • You need to trigger that function! Commented Aug 25, 2015 at 6:55

2 Answers 2

1

No need of using Javascript/jQuery, check the below points.

Suggestions:

  1. Use width in percentages so, when resized it'll auto adjust
  2. Use media queries for resolution specific styles
  3. When setting properties from Javascript, use quotes around property name or use camelcase. Ex. maxWidth, or 'max-width'

Example:

@media (max-width: 600px) {
    .page {
        width: 200px;
    }
}
  1. Create separate CSS file for various resolutions and load them for such resolutions

Example:

<!-- CSS media query on a link element -->
<link rel="stylesheet" media="(max-width: 600px)" href="mobile.css" />

Update

If you still want to use Javascript, call your function on window resize event.

window.onresize = responsiveFn;

Use max-width in quotes

jQuery('.page').css('max-width', width);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate, but tired of doing this one. I got this separated css for mobile.
@Ayuk Kindly check the updated answer, especially the bottom part
0

Please use media query for all the elements as per your requirement example

@media screen and (min-width: 320px) and (max-width:580px) {
    .btn-default {
        float: none;
        max-width: 100%;}
    .textcenter {
        text-align: center;
    }
}

As button and textcenter class will be changed with the mentioned property

Thanks

Comments

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.