Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML Screen pixelDepth Property
The HTML Screen pixelDepth property returns the color resolution (bit depth) of the visitor's screen in bits per pixel. This property indicates how many bits are used to represent the color of each pixel, which determines the total number of colors the screen can display.
The pixel depth directly affects color quality − higher values mean more colors can be displayed. Common values include 8 bits (256 colors), 16 bits (65,536 colors), 24 bits (16.7 million colors), and 32 bits (16.7 million colors with transparency).
Syntax
Following is the syntax for accessing the pixelDepth property −
screen.pixelDepth
Return Value
The pixelDepth property returns an integer representing the number of bits per pixel. This is a read-only property that cannot be modified.
Example − Display Pixel Depth
Following example demonstrates how to retrieve and display the screen's pixel depth −
<!DOCTYPE html>
<html>
<head>
<title>HTML Screen pixelDepth Property</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
background-color: #f5f5f5;
}
.btn {
background: #007bff;
color: white;
border: none;
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin: 10px;
}
.btn:hover {
background: #0056b3;
}
.result {
font-size: 18px;
font-weight: bold;
margin: 20px 0;
padding: 15px;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<h1>HTML Screen pixelDepth Property</h1>
<button class="btn" onclick="showPixelDepth()">Show Pixel Depth</button>
<div class="result" id="output">Click the button to display pixel depth</div>
<script>
function showPixelDepth() {
var depth = screen.pixelDepth;
var colors = Math.pow(2, depth);
document.getElementById("output").innerHTML =
"Pixel Depth: " + depth + " bits per pixel<br>" +
"Maximum Colors: " + colors.toLocaleString();
}
</script>
</body>
</html>
The output displays both the pixel depth in bits and the maximum number of colors that can be represented −
Pixel Depth: 24 bits per pixel Maximum Colors: 16,777,216
Example − Complete Screen Information
Following example shows how to display comprehensive screen information including pixel depth −
<!DOCTYPE html>
<html>
<head>
<title>Complete Screen Information</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<h2>Screen Information Dashboard</h2>
<button onclick="displayScreenInfo()" style="padding: 10px 20px; background: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer;">Get Screen Info</button>
<div id="screenInfo" style="margin-top: 20px; padding: 15px; background: #f8f9fa; border-radius: 8px;">
Click the button to view screen details
</div>
<script>
function displayScreenInfo() {
var info = "<h3>Your Screen Properties:</h3>";
info += "<p><strong>Pixel Depth:</strong> " + screen.pixelDepth + " bits per pixel</p>";
info += "<p><strong>Color Depth:</strong> " + screen.colorDepth + " bits</p>";
info += "<p><strong>Width:</strong> " + screen.width + " pixels</p>";
info += "<p><strong>Height:</strong> " + screen.height + " pixels</p>";
info += "<p><strong>Available Width:</strong> " + screen.availWidth + " pixels</p>";
info += "<p><strong>Available Height:</strong> " + screen.availHeight + " pixels</p>";
document.getElementById("screenInfo").innerHTML = info;
}
</script>
</body>
</html>
This example displays complete screen information including pixel depth alongside other screen properties for comparison.
Pixel Depth vs Color Depth
While screen.pixelDepth and screen.colorDepth often return the same value, they serve slightly different purposes −
pixelDepth − The actual number of bits used per pixel in the screen's frame buffer.
colorDepth − The number of bits used to represent colors, which may be different from pixel depth on some systems.
Browser Compatibility
The pixelDepth property is widely supported across all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer. It has been part of the Screen interface since the early days of JavaScript.
Common Use Cases
Following are common scenarios where pixel depth information is useful −
Image optimization − Serving different image qualities based on screen capabilities.
Graphics applications − Adjusting rendering quality for optimal performance.
System diagnostics − Collecting display information for troubleshooting.
Analytics − Understanding user hardware capabilities for statistical analysis.
Conclusion
The screen.pixelDepth property provides valuable information about the user's display capabilities by returning the number of bits used per pixel. This read-only property is essential for applications that need to optimize content based on screen color resolution, with most modern displays supporting 24-bit or 32-bit color depth.
