HTML Screen colorDepth Property

The HTML Screen colorDepth property returns the bit depth of the color palette for displaying images in bits per pixel. This property indicates how many bits are used to represent the color of each pixel on the screen, which determines the maximum number of colors that can be displayed simultaneously.

The colorDepth property is part of the Screen object and provides information about the display capabilities of the user's monitor. Common values include 8, 16, 24, and 32 bits per pixel.

Syntax

Following is the syntax for the colorDepth property −

screen.colorDepth

Return Value

The property returns an integer representing the color depth in bits per pixel. Common return values are −

  • 8 bits − 256 colors
  • 16 bits − 65,536 colors (High Color)
  • 24 bits − 16,777,216 colors (True Color)
  • 32 bits − 16,777,216 colors with alpha channel
Color Depth Values 8 bits 256 colors 16 bits 65,536 colors 24 bits 16.7M colors 32 bits 16.7M+ alpha

Basic Example

Following example demonstrates how to retrieve and display the color depth of the screen −

<!DOCTYPE html>
<html>
<head>
    <title>Screen colorDepth Property</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; text-align: center;">
    <h2>Screen Color Depth Information</h2>
    <button onclick="showColorDepth()" style="padding: 10px 20px; font-size: 16px; cursor: pointer;">Show Color Depth</button>
    <div id="result" style="margin-top: 20px; font-size: 18px; font-weight: bold;"></div>
    
    <script>
        function showColorDepth() {
            const depth = screen.colorDepth;
            document.getElementById("result").innerHTML = "Color Depth: " + depth + " bits per pixel";
        }
    </script>
</body>
</html>

The output displays the color depth when the button is clicked −

Color Depth: 24 bits per pixel

Detailed Screen Information Example

Following example shows comprehensive screen information including color depth −

<!DOCTYPE html>
<html>
<head>
    <title>Complete Screen Information</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
    <h2>Complete Screen Information</h2>
    <button onclick="getScreenInfo()" style="padding: 8px 16px; margin-bottom: 20px; cursor: pointer;">Get Screen Info</button>
    <div id="screenInfo" style="background: #f5f5f5; padding: 15px; border-radius: 5px;"></div>
    
    <script>
        function getScreenInfo() {
            const info = `
                <strong>Screen Properties:</strong><br>
                Width: ${screen.width} pixels<br>
                Height: ${screen.height} pixels<br>
                Available Width: ${screen.availWidth} pixels<br>
                Available Height: ${screen.availHeight} pixels<br>
                <strong>Color Depth: ${screen.colorDepth} bits per pixel</strong><br>
                Pixel Depth: ${screen.pixelDepth} bits per pixel
            `;
            document.getElementById("screenInfo").innerHTML = info;
        }
    </script>
</body>
</html>

This example displays complete screen information including the color depth highlighted in bold.

Practical Use Case

Following example shows how to use colorDepth to adjust image quality based on screen capabilities −

<!DOCTYPE html>
<html>
<head>
    <title>Adaptive Image Quality</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; text-align: center;">
    <h2>Adaptive Image Quality Based on Color Depth</h2>
    <div id="recommendation" style="margin: 20px; padding: 15px; border-radius: 5px;"></div>
    
    <script>
        function checkColorDepth() {
            const depth = screen.colorDepth;
            let recommendation = "";
            let bgColor = "";
            
            if (depth <= 8) {
                recommendation = "Low color depth (" + depth + " bits). Use optimized images with reduced colors.";
                bgColor = "#ffebee";
            } else if (depth <= 16) {
                recommendation = "Medium color depth (" + depth + " bits). Standard quality images recommended.";
                bgColor = "#fff3e0";
            } else {
                recommendation = "High color depth (" + depth + " bits). High-quality images supported.";
                bgColor = "#e8f5e8";
            }
            
            document.getElementById("recommendation").style.backgroundColor = bgColor;
            document.getElementById("recommendation").innerHTML = recommendation;
        }
        
        // Check color depth when page loads
        window.onload = checkColorDepth;
    </script>
</body>
</html>

This example automatically detects the color depth and provides appropriate recommendations for image optimization.

Browser Compatibility

The screen.colorDepth property is widely supported across all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer. It has been part of the JavaScript specification since the early versions and provides consistent results across different platforms.

Conclusion

The screen.colorDepth property is useful for determining the color capabilities of the user's display. It returns the number of bits per pixel used for colors, helping developers optimize their content for different screen types. Most modern displays return 24 or 32 bits per pixel, indicating support for millions of colors.

Updated on: 2026-03-16T21:38:54+05:30

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements