Javatpoint Logo
Javatpoint Logo

JavaScript windows getComputedStyle() Method

A CSS Style Declaration object results from the getComputedStyle() function. It is provided elements of the CSS properties and values using the javascript windows getComputedStyle() function. The element displays various sources of styling using computed style in the tag.

Syntax

The following syntax shows the windows getComputedStyle() method using JavaScript.

OR

The following syntax shows the getComputedStyle() method.

Parameters:

Two arguments can be passed to the getComputedStyle() method:

  • You want to return the calculated styles is called an element. The method will return an error if you pass another type of node, such as a Text node.
  • The pseudoElement specifies the pseudo-element to match style. The default value is null, and the parameter is optional.

Return value

  • The CSS Style Declaration object's live style object element results from the getComputedStyle() method.
  • When the element's styles are modified, the style is updated automatically.

How it works

  • First, establish the message class's CSS rules in the HTML file's head section.
  • Second, specify a paragraph element with red content using the inline style. This one shall supersede the regulation outlined in the head section.
  • Third, obtain every calculated style associated with the paragraph element use the getComputedStyle() function.

Examples of getComputedStyle() in JavaScript

The following examples use the style tag and properties for the getComputedStyle() function. The CSS properties necessary for the getComputedStyle() method

Example 1: the following example shows a basic getComputedStyle() method. The padding, color, border, and background color shows using the javascript method. The CSS value is set using the style tag.

Output

The image shows the CSS style of the web page's function.

JavaScript windows getComputedStyle() Method

Example 2: the following example shows a basic getComputedStyle() method. The padding, color, border, and background color shows using the CSS style tag and without the CSS style tag. the method does not show the default style using the getComputedStyle() method.

Output

The image shows the CSS style of the web page's function.

JavaScript windows getComputedStyle() Method

Example 3: In the HTML file's head section, establish the CSS rules for each paragraph element's first letter.

The computed style of the pseudo-element can then be obtained using the getComputedStyle() function. The initial letter of the paragraph with the id has a font size of 16px.

Output

The image shows the CSS style of the web page's function.

JavaScript windows getComputedStyle() Method

Example 4: the following example shows a getComputedStyle() method with an inline style tag. The font size, color, border, width, and background color shows using the javascript method. The CSS value is set using the inline style tag.

Output

The image shows the inline CSS style of the web page's function.

JavaScript windows getComputedStyle() Method

Example 5: the following example shows a window.getComputedStyle() method with the style tag. The properties used in the page with the element and pseudo element using the javascript method. The CSS value is set using the style tag in the head section of the web page.

Output

The image shows the inline CSS style of the web page's function.

JavaScript windows getComputedStyle() Method

Example 6: the following example shows a window.getComputedStyle() method with the style tag. The properties used in the page with the element and pseudo null element using the javascript method. The CSS value is set using a style tag but does not need to use for the element.

Output

The image shows all the CSS styles of the web page's functions.

JavaScript windows getComputedStyle() Method

Conclusion

The getComputedStyle method uses to show the available CSS properties of the element. We can use the different properties for the style tag and display it as an output.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA