0

I have HTML div with width of 190 px that's working normal for desktop but for mobile its not , is there a way to tell if connection is from desktop take this value else take this value ?

this is my code :

document.querySelector('.article').style.width ='190px';
sayalok
  • 830
  • 3
  • 15
  • 27
salmanaacvf
  • 151
  • 2
  • 17
  • You might want to check device resolution, not type. – Prajwal Jan 18 '19 at 12:10
  • 9
    Meet [Media Queries](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries) – Mitya Jan 18 '19 at 12:11
  • You can check out this reference https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device-in-jquery – Varun Setia Jan 18 '19 at 12:12
  • To clarify, if you set the width on the .article class instead of adding it inline, you can use a media query to state that .article has to be 190px when the screen is bigger than x and that .article is like 100% width when the screen is smaller than X. – Shilly Jan 18 '19 at 12:17

3 Answers3

7

In your css file

// Desktop
.article {
    width: 100px;
}


// Mobile
@media only screen and (max-width: 768px) {
    .article {
        width: 50px;
    }
}

This are Media Queries.

In the first lines, we don't have any limitation, but then you override the current value ONLY when the width of the screen is lower than 768px

Eugenio Valeiras
  • 900
  • 1
  • 8
  • 27
  • 3
    At a high level I think this is a good answer, so +1 -- but I would suggest starting with the mobile (small screen) styles as the default and then using the media query (min-width:769px) to change styles on larger screens. The current example means that the smaller device is required to parse all the styles, while the larger device isn't working as hard. Admittedly with one small media query you would never see a performance hit, but starting mobile-first is MUCH easier than refactoring to mobile first if your stylesheet is much larger someday. – ryantdecker Jan 18 '19 at 12:51
0

Just use media Queries which is the best option to make the things responsive . Otherwise you can have a look at flexbox which is awesome flexbox

0

Use Media Queries

Using a breakpoint of 576px targets all kinds of mobile devices.

// This is the default size
.article {
   width: 100px;
}


// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { 
  width: 200px;
}

The other answer targets tablets too. But if you want to target mobile only, use my solution.

mahan
  • 4,950
  • 1
  • 12
  • 42