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
– Clayton Furlanetto
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.– Oralista de Sistemas
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.– Dudaskank
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!
– Clayton Furlanetto
Our friend @Ŋvı killed the question, that’s what I wanted, thank you for your help.
– Clayton Furlanetto
Thanks for the information, I will study them to improve, thanks for the help.
– Clayton Furlanetto
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.
– Dudaskank