Link to home
Create AccountLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

How to detect screen resolution and size

Is there a way to detect a website users screen resolution or size?

client wants site to display better in a 1024 wide window, how tough to do a screen res detect and have the site left justify for those who are below 1280X1024?
ASKER CERTIFIED SOLUTION
Avatar of nap0leon
nap0leon

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Robert Granlund

ASKER

@nap0leon:

Thank you for the response.  How can I turn that into an if statement using 1280x1024 ?

Thank you in advance.
<script type="text/javascript">
if(screen.width=='1280' && screen.height=='1024')
{
// do something
alert('boo');
}
</script>

Open in new window

Avatar of nap0leon
nap0leon

Are you looking to alter the CSS for the page to make it fit inside a smaller width?

Let's presume that your current site has a 100 pixel left margin that you are wanting to remove for lower-res users - the following will set the left-margin to only 10 pixels for such users.
<html>
<head>
</head>
<body style="margin:0 0 0 100px;">
<h1>Hello</h1>
</body>
<script>
if (screen.width < 1280){ document.body.style.margin="0 0 0 10px";}
</script>
</html>

Open in new window

I don't think you want to us screen.width and screen.height it does not account for tool bars and sidebars.
Better to use:

<script>
function adjustwidth()
{
   if (window.screen.availWidth <1280)
    {
      document.body.style.width = 1280 + 'px';
      document.body.style.margin=0;
     }
}
onload=adjustwidth;
onresize =adjustwidth;
</script>

Open in new window