The Wayback Machine - https://web.archive.org/web/20241127000441/https://www.geeksforgeeks.org/how-to-get-the-device-screhow-to-get-the-width-of-device-screen-in-javascript-en-width-in-javascript/
Open In App

How to get the width of device screen in JavaScript ?

Last Updated : 26 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an HTML document that is running on a device and the task is to find the width of the working screen device using JavaScript. 

Example 1: This example uses window.innerWidth to get the width of the device screen. The innerWidth property is used to return the width of the device. 

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to get the width of device 
        screen in JavaScript?
    </title>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        Click on Button to Get the 
        Width of Device's Screen
    </h3>

    <button onclick="GFG_Fun()">
        Click Here
    </button>

    <p id="GFG"></p>

    <!-- Script to display the device screen width -->
    <script>
        let elm = document.getElementById("GFG");

        function GFG_Fun() {
            let width = window.innerWidth;
            elm.innerHTML = width + " pixels";
        }
    </script>
</body>

</html>

Output:

Example 2: This example uses document.documentElement.clientWidth method to get the width of device screen. 

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to get the width of device
        screen in JavaScript?
    </title>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        Click on Button to Get the
        Width of Device's Screen
    </h3>

    <button onclick="GFG_Fun()">
        Click Here
    </button>

    <p id="GFG">
    </p>

    <!-- Script to display the device screen width -->
    <script>
        let elm = document.getElementById("GFG");

        function GFG_Fun() {
            elm.innerHTML = document.
                documentElement.clientWidth + " pixels";
        }
    </script>
</body>

</html>

Output:



Previous Article
Next Article

Similar Reads

How to get the height of device screen in JavaScript ?
Given an HTML document which is running on a device. The task is to find the height of the working screen device using JavaScript. Prerequisite - How to get the width of device screen in JavaScript ? Example 1: This example uses window.innerHeight property to get the height of the device screen. The innerHeight property is used to return the height
2 min read
How to get the diagonal length of the device screen using JavaScript ?
Knowing the width and height of a browser window helps a web developer to enhancing the user experience. It can help in improving browser animations and the relative positioning of divisions and containers. Javascript provides a window object that represent an open browser window. It provides the various properties which define the dimensions of yo
3 min read
How to Detect the Device is an Android Device using JavaScript?
Detecting an Android device using JavaScript helps improve web experiences. By checking the user agent string, developers can identify Android devices and apply specific features or styles. This method ensures compatibility and optimizes functionality for different platforms ApproachUse the navigator.userAgent property to get the value of the user-
2 min read
How to detect touch screen device using JavaScript?
Sometimes you might be looking for some features to include into your web-app that should only be available to devices with a touch screen. You may need this detection while introducing newer smarter controls for touch screen users in the game app or a GPS and navigation application. While there are many JavaScript libraries such as Modernizer, jQu
3 min read
How to detect touch screen device using CSS ?
In this article, we will learn how to detect touchscreen devices using CSS. In a website, it becomes important to detect the pointing device used by the user. For example, if the user uses a finger as the pointing device (which has less accuracy on the screen due to more screen-finger contact area) then we should increase the size of various elemen
2 min read
What is the difference between “screen” and “only screen” in media queries?
Media query is used to create responsive web design. It means that the view of web page differ from system to system based on screen or media types. screen: It is used to set the screen size of media query. The screen size can be set by using max-width and min-width. The screen size is differ from screen to screen. Syntax: @media screen and (max-wi
2 min read
How to get the Android version of your device using JavaScript?
The task is to detect the Android version of the user with the help of JavaScript. Two approaches are discussed here, the first example uses the RegExp and the second uses indexOf method to search the keyword 'android'. Note: Both codes will only work when you run them on Android devices. Approach 1: In this approach, we will use navigator.useragen
3 min read
HTML DOM Screen width Property
The Screen width property is used for returning the total width of a user's screen. It returns the total width of the screen in pixels. Syntax: screen.widthReturn Value: A Number, representing the total width of the user's screen, in pixels Below program illustrates the Screen width Property : Checking the total width of a user's screen. C/C++ Code
1 min read
How to fit text container width dynamically according to screen size using CSS ?
In this article, we have given a text paragraph and the task is to fit the text width dynamically according to screen size using HTML and CSS. Some of the important properties used in the code are as follows- display-grid: It helps us to display the content on the page in the grid structure.grid-gap: This property sets the minimum amount of gap bet
7 min read
HTML width/height Attribute vs CSS width/height Property
In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea
3 min read
Difference Between css(‘width’) and width() methods In jQuery
In jQuery, we have two ways to change the width of any HTML element. The jQuery css() method and width() method, below both the methods are described properly with the example. jQuery CSS('width') Method: It is a method present in jQuery which is used to get or set the property on the matched element. It is a property used to get or set the width o
3 min read
How to add Background-Color for Text Width Instead of Entire Element Width using CSS ?
In this article, we will see how to add background color for the text width instead of the entire element width using CSS. The display: inline or display: inline-block property is used to set the background color of the text width. Syntax: display: inline; display: inline-block; Approach: We will create an element and then apply the display: inline
2 min read
How to make a grid with two columns where 1st column has 20% of width and 2nd one 80% width in Tailwind CSS ?
Tailwind CSS simplifies the process of making complex UI designs. It has a lot of predefined styles in form of classes, which can be applied to any HTML element in the code. Also, one advantage of Tailwind CSS is that during the production build, it analyzes the HTML components, and removes any unused CSS, hence decreasing the overall size of the C
4 min read
What is the difference between 'width: 100%' and 'width: 100vw' in CSS ?
The CSS width Property is used to set the width of the text and images. The width can be assigned to the text and images in the form of pixels(px), percentages (%), centimeters (cm), etc. The width: 100% sets an element's width relative to its containing block, making it responsive within its parent. The width: 100% responds to the container's size
1 min read
How to get the Element's Actual Width and Height in JavaScript?
In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purposes. Below are the approaches to get the element's a
6 min read
How to get the image size (height & width) using JavaScript?
To get the size of an image (height and width), Element.clientHeight and Element.clientWidth properties are used. Element.clientHeight: We can access the inner height of an element with this property. This height includes the padding but not the margin and border. Element.clientWidth: We can access the inner width of an element with this property.
2 min read
How to get the Width and Height of an Image using JavaScript?
Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height. Syntax for width:let width = this.width;Syntax for height:let height = this.height;Example 1: This example selects the image and then uses this.width and this.height method to get the
2 min read
How to get the Width of Scroll bar using JavaScript?
Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth - clientWidth.Example 1: This example implements
3 min read
How to detect whether the website is being opened in a mobile device or a desktop in JavaScript ?
Using CSS Media Queries, we can easily know which user is currently viewing our website on which device (using min-width and max-width). It is only limited to styling web pages, but we can control the functionality of the website according to the user's device using the navigator userAgent Property in JavaScript. We can get information about the us
2 min read
Detect a device is iOS or not using JavaScript
In order to detect a device whether it is iOS or not. We're going to Navigator platform and Navigator userAgent property. Navigator userAgent property This property returns the value of the user-agent header which is sent by the browser to the server. Returned value, have information about the name, version, and platform of browser. Syntax: navigat
2 min read
How to detect whether a device is iOS without using Regular Expression in JavaScript?
The task is to detect whether the device is iOS or not without using RegExp with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use navigator.platform property to check for the particular keywords which belongs to iOS devices using indexOf() method. Example: <!DOCTYPE html> <html> <head>
2 min read
How to get the size of screen, current web page and browser window using jQuery?
jQuery can be used to get the dimensions of the current page. The dimensions include the screen, viewport and document height, and width. Getting the window dimensions: Window size is the size of the browser window. This is the size that changes when the browser is resized. The windows height and width can be accessed with the height() and width()
2 min read
How to dynamically get the content width of a div using AngularJS ?
In this article, we will see how to dynamically get the content width of a div using AngularJS, along with understanding its basic implementation through the examples. The content width of a div can dynamically get using clientWidth and scrollWidth properties depending upon the user's requirement. If a user wants to know the space required by actua
2 min read
How to get inner width (excluding the border) of an element using jQuery ?
We can get the inner width of any element using the jQuery innerWidth() method. The innerWidth() method returns the inner width of the first matched element including padding but not border. Syntax: $(selector).innerWidth() So the inner width of an element is shown in the below image. The inner width of an element is the sum of the width of an elem
1 min read
How to Get Window Width and Height In React Native ?
In this article, we'll explore two different approaches to obtaining the screen width and height in a React Native application. Scree­n dimensions, including width and height, play a vital role in de­signing user interfaces that adapt se­amlessly to different de­vices and orientations. By understanding these dimensions, deve­lopers empower themselv
3 min read
How to get the width and height of an element in jQuery ?
In this article, we will find the width and height of an element in jQuery. There are three ways available in jQuery that we can use to find the height and width of an element. Table of Content Using the width() and height() methodsUsing the innerWidth() and innerHeight() methodsUsing the outerWidth() and outerHeight() methodsUsing the width() and
3 min read
How to Get divs to Fill up 100% of the Container Width without Wrapping in CSS Flexbox?
Creating a layout where &lt;<div>&gt; elements fill the full width of their container without wrapping can be a little complex in web design. CSS flexbox makes this easier by allowing us to control the alignment and sizing of elements. In this article, we are going to discuss how to use Flexbox to achieve this layout. Flexbox is a pow
3 min read
How To Get The Height And Width Of An Image Using ReactJS?
When building web applications, dynamically handling images and their dimensions is a common requirement. In ReactJS, determining the dimensions of an image (i.e., its height and width) can be useful for responsive design, image manipulation, and other dynamic operations. This article will guide you on how to get the height and width of an image in
4 min read
How to Get the Width of Hidden Element in jQuery?
An HTML element can be hidden with the help of the .hide() jQuery function or we can hide easily by making visibility equals hidden in CSS. We can easily find the width of this hidden element in jQuery. Two kinds of width are defined with every HTML element i.e, innerWidth and the outerWidth of the element: innerWidth: This width is considered when
3 min read
How to detect a mobile device in jQuery?
We can use JavaScript window.matchMedia() method to detect a mobile device based on the CSS media query. This is the best and easiest way to detect mobile devices. Syntax: window.matchMedia(); Example-1: Program run on desktop. <!DOCTYPE html> <html lang="en"> <head> <title> jQuery Detect Mobile Device </title
1 min read
three90RightbarBannerImg