Amazon Developer

as

Settings
Sign out
Notifications
Alexa
Amazon Appstore
Ring
AWS
Documentation
Support
Contact Us
My Cases
Develop
Test
Publish
Monetize
Engage users
Device specifications
Resources
Skip to main content

Introduction

You can use JavaScript and/or CSS media queries to detect and react to the device’s display size. For example, you can change the page’s background color based on the width and height of the screen. There are two different metrics available when measuring display size: screen size and viewport size.
  • Screen size refers to the size of the device’s physical screen.
  • Viewport size refers to the size of the window on the screen.
The ScreenSize sample demonstrates how to use JavaScript and CSS to detect and react to the device’s display size. The code for this sample is located in the following folder of the Amazon Web SDKs: Amazon-Web-SDKs/Webapps/examples/Cookbook/ScreenSize/ Download the Amazon Web SDKs from the SDK Download page (click the Web button).

Use JavaScript to Detect Display Characteristics

When using JavaScript to detect display size, there are two different objects that correspond to the two different display size metrics:
  • The screen object contains information about the screen size.
  • The window object contains information about the viewport size.
You can query the screen object for the following properties:

Use CSS to Detect Display Characteristics

Use CSS media queries to detect and respond to different display sizes. Use the following CSS selectors to detect window or screen size:

Example CSS Media Queries

To change the background color based on whether the device is currently in portrait or landscape, use the following sample code:
To change the background color based on the resolution (pixel density) of the output device, use the following sample code:
Last modified on October 29, 2020