By Sergey Skudaev
Some times, you need to create a web page that fit to a visitor browser width. I will show you how to read a visitor screen resolution and depending on its width use different css style file, so that web page width would vary for different visitors.
I use JavaScript to write resolution to cookie and PHP to select the right css style file
You can change resolution of your screen couple of times and see the effect.
I use different page background color for different resolutions to easily see when css file is changing. You must close browser and open again to reset cookie, other wise resolution variable will not change.
My PC maximal resolution is 1360, so I could not test my code for biger resolution. You can do it on your own.
<?php
session_start();
$screen_resolution=0;
if (!isset($_SESSION[iterate]))
{
if(!isset($HTTP_COOKIE_VARS["users_resolution2"]))
//means cookie is not found set it using Javascript
{
$_SESSION[iterate]=1;
?>
<script language="javascript">
<!--
writeCookie();
function writeCookie()
{
var the_cookie = "users_resolution2="+ screen.width +"x"+ screen.height;
document.cookie=the_cookie
location ='<?php echo $_SERVER['PHP_SELF'];?>';
}
//-->
</script>
<?php
}
else{
$screen_resolution = $HTTP_COOKIE_VARS["users_resolution2"];
}
}
else{
$screen_resolution = $HTTP_COOKIE_VARS["users_resolution2"];
}
settype($screen_resolution, "integer");
//echo $screen_resolution;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>How to read visitor screen resolution to best fit web page</title>
<meta name="description" content="How to read visitor screen resolution to best fit web page.">;
<meta name="keywords" content="PHP, css, screen resolution">;
<meta name="owner" content="Serge Skudaev">;
<meta name="author" content="Sergey Skudaev">;
<meta http-equiv="content-language" content="eng-US">;
<meta http-equiv="content-Type" content="text/html; charset=ISO-8859-1">;
<meta name="robots" content="index,follow">;
<meta name="revisit after" content="2 weeks">;
<!-- End of Meta Tags -->
<?php
if($screen_resolution ==1024)
{
print('<link href="style1024.css" rel="stylesheet" type="text/css" media="Screen">;');
}
elseif(($screen_resolution > 1024)&&($screen_resolution < 1360))
{
print('<link href="style1280.css" rel="stylesheet" type="text/css" media="Screen">;');
}
elseif(($screen_resolution > 1359)&&($screen_resolution < 1439))
{
print('<link href="style1360.css" rel="stylesheet" type="text/css" media="Screen">;');
}elseif(($screen_resolution > 1439)&&($screen_resolution < 1599)
{
print('<link href="style1440.css" rel="stylesheet" type="text/css" media="Screen">;');
}
elseif(($screen_resolution > 1599)&&($screen_resolution < 1900))
{
print('<link href="style1600.css" rel="stylesheet" type="text/css" media="Screen">;');
}
elseif($screen_resolution > 1900)
print('<link href="style1920.css" rel="stylesheet" type="text/css" media="Screen">;');
?>
</head>
<body>
<div id="page">
<div id="header"><br>;
</div>
<div id="content">
<div id="middle_right">
<div id="middle">
<h1 align="center">How to read visitor screen resolution to best fit web page.</h1>
<p>By Sergey Skudaev</p>
<?
?>
Our dog needs urgent surgery, and the cost is overwhelming.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!
Oscar Story.
Oscar wasn’t just any puppy—he was a gift from a mother who trusted us with her smallest one.
For five years, my wife worked at the Indian Medical Center in Arizona, deep in Navajo Nation. Near her clinic, she often saw a homeless dog wandering the area. Over time, she began feeding her, and the dog grew fond of her. Then, one day, that same dog brought her newborn puppies to my wife—as if proudly showing them off.
Among them was the smallest, most delicate pup. My wife couldn’t resist. She brought him home, and we named him Oscar.
Oscar thrived in the house provided by the medical center, enjoying the big backyard where he lived. I built him a sturdy wooden doghouse, and we often took him on walks along the Window Rock Trail. He became our adventure companion, making the vast desert feel like home.
After my wife’s contract ended, we moved back to Florida, bringing Oscar with us. He adjusted to his new surroundings, but he never lost his adventurous spirit.
Now, Oscar faces a tough challenge—he needs urgent surgery, and the cost is overwhelming. We want to give him the best care possible, just as he’s given us years of joy and loyalty.
Any help, big or small, would mean the world to us. Thank you for supporting Oscar on his journey to recovery!