How Can I Check an Element\'s CSS `display` Property Value (block or none) with JavaScript?

Barbara Streisand
Release: 2024-11-23 10:15:12
Original
627 people have browsed it

How Can I Check an Element's CSS `display` Property Value (block or none) with JavaScript?

Checking Element CSS Display with JavaScript

Can you determine if an element's CSS display property is set to "block" or "none" using JavaScript?

Answers:

Using Computed Style for Inherited or Styled Elements:

As mentioned by sdleihssirhc, if the element's display is inherited or defined by a CSS rule, you need to retrieve its computed style:

return window.getComputedStyle(element, null).display;
Copy after login

Using the Style Property for Inline or JavaScript-Set Styles:

Elements have a style property that provides information about their inline styles or styles set using JavaScript:

console.log(document.getElementById('someIDThatExists').style.display);
Copy after login

This will provide you with a string representing the current display value.

The above is the detailed content of How Can I Check an Element\'s CSS `display` Property Value (block or none) with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template