Code to show screen resolution

Asked

Viewed 2,230 times

2

I’m testing the site I’m creating on several devices to see the responsiveness, because I have 5 css one for each screen resolution and would like to create a code within a div that shows the resolution of the device. The information that manufacturers pass on from the devices seems not to be what it really is. When my site loaded on my device, this div with the code would inform me the screen resolution, so I can see where I should change because, I use several css, depending on the resolution. I know this is possible via js, I just don’t know how to do :)

3 answers

2

Object screen

I believe you’re searching for the object screen in javascript. It gives you the screen resolution where the window is.

// escrevendo a resulução no console
console.log('Total width/height: ' + screen.width + 'x' + screen.height);
// escrevendo dentro de um div
document.getElementById('idDoDivQueMostraResolucao').innerHTML = 'Total width/height: ' + screen.width + 'x' + screen.height;

Media Query

However, you don’t need to use this, you can put the call media query in CSS itself. This approach also has the advantage of working when the user is javascript disabled. So you won’t need a CSS file for each resolution either.

@media screen and (min-width: 480px) {
  body {
    background-color: lightgreen;
  }
}

Responsive images

For images, there are still other modes, introduced in HTML5, which do not use CSS or Javascript, leaving everything to the browser.

srcset and sizes

The new tag attributes img, srcset and sizes, are used by the browser to select the best image depending on the resolution. In the example below, the browser downloads only the image that best fits a given scenario:

<img srcset="elva-fairy-320w.jpg 320w,
             elva-fairy-480w.jpg 480w,
             elva-fairy-800w.jpg 800w"
     sizes="(max-width: 320px) 280px,
            (max-width: 480px) 440px,
           800px"
     src="elva-fairy-800w.jpg" alt="Elva dressed as a fairy">

With these attributes in place, the browser will:

  • Observe the width of your device (screen).
  • Find what media condition in the size list (atributo sizes) is the first to be true.
  • Observe the size of the slot provided with this media query.
  • Load the referenced image into the srcset list that best matches the chosen size.

picture

This tag is used, for example, in cases where an image looks good on the desktop in landscape mode, but on a mobile phone in portrait mode is not cool. Here you create another image for portrait mode that looks better.

<picture>
  <source media="(max-width: 799px)" srcset="elva-480w-close-portrait.jpg">
  <source media="(min-width: 800px)" srcset="elva-800w.jpg">
  <img src="elva-800w.jpg" alt="Chris standing up holding his daughter Elva">
</picture>

In older browsers, new attributes and tags are just ignored.

More information

  • As my site is large and has many images I thought it best to create a css for each resolution instead of adding media querie for each item I want to change. As for the media querie and the css I’m quiet, it’s all right here. What I need is a js that shows the resolution of the device I am accessing my site, I already have a file created function.js with all the js on the site, I didn’t understand how I added the.log console you put, I want the resolution to appear inside the div I created with name #resolucao

  • Como meu site é grande e tem muitas imagens achei melhor criar um css pra cada resolução em vez de ficar adicionando media querie pra cada item que quero mudar. Over time you will see that it was less effort to use media queries.

  • That one console.log() writes on the javascript console of the browser, you access it by pressing F12 and clicking on the console tab. I’ll give you an adjustment to see how you’d look putting it in a div. On the various images, there you can use up other means, which I will also add in the answer... wait a minute there.

  • What I want is not this, I want this js to access through mobile, tablet, notbook I know what the resolution of the device I am accessing, understand? I want you to tell me the resolution of the device I am accessing the site. I want a code that returns the resolution of the device. I think it’s possible, I just have no idea how to do!

  • Our friend @Ŋvı killed the question, that’s what I wanted, thank you for your help.

  • 1

    Thanks for the information, I will study them to improve, thanks for the help.

  • I think I got a little carried away... but see the comment in the accepted answer, on the notebook or desktop that mode is not very reliable.

Show 2 more comments

2


You can get the screen resolution with window.innerWidth, and play that value within the div with:

document.querySelector("#resolucao").innerHTML = window.innerWidth;

document.querySelector("#resolucao").innerHTML = window.innerWidth;
<div id="resolucao"></div>

  • that’s just what I wanted. Thank you very much!

  • The attributes window.innerWidth and javascript window.innerHeight show the internal size of the browser window. If you change the window size, these values will change, that is, the desktop will not work. See if screen.width and screen.height doesn’t look better for you

  • @Dudaskank Exactly, but as it is working with window resolutions to apply in the behavior of the elements, what matters in this case is the resolution of the window, and not the whole screen. 'Cause he used the wrong term :)

0

With jquery

With this solution in addition to grabbing the width of the device will load the appropriate CSS

Library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Script

function adjustStyle(width) {

  //largura do dispositivo
  width = parseInt(width);
  //imprime a largura na div de id sizeWidth
  $( "#sizeWidth" ).text(width);

  //apenas um exemplo
  if (width < 701) {
    $("#size-stylesheet").attr("href", "style700.css");
  } else if (width < 900) {
    $("#size-stylesheet").attr("href", "style900.css");
  } else {
     $("#size-stylesheet").attr("href", "styleMaior900.css"); 
  }
}

$(function() {
  adjustStyle($(this).width());
  $(window).resize(function() {
    adjustStyle($(this).width());
  });
});

HTML

<link id="size-stylesheet" rel="stylesheet" type="text/css" href="" />
<div id="sizeWidth"></div>
  • I do it with css <!--Estilização para displays com até 768px-->&#xA; <link rel="stylesheet" type="text/css" href="css/estilo-768.css" media="all and (min-width:481px) and (max-width:768px)">. I find it easier because I don’t know much about js, what’s the difference of how I use it for this?

  • So you say that tenho 5 css uma para cada resolução de tela dai vc no if Else determines for which screen width will print on the href tag <link id="size-stylesheet" rel="stylesheet" type="text/css" href="" />the corresponding css

  • 1

    @Clayton Furlanetto, your mode does not depend on javascript or jquery, in case the user has disabled javascript your mode is still working. It’s also less code for maintenance after.

  • 1

    Thinking this way, it can also have user style sheet (higher priority). Advanced computer users can display web pages using their own custom sheet style, dai Baubau

Browser other questions tagged

You are not signed in. Login or sign up in order to post.