Home  >  Article  >  Web Front-end  >  How to use the display attribute of CSS to hide and display div elements

How to use the display attribute of CSS to hide and display div elements

PHPz
PHPzOriginal
2023-04-21 11:20:452140browse

Hide or show specific elements on the page often use the CSS display attribute. In this article, we will focus on how to use the display attribute of CSS to hide and show div elements.

1. The display attribute of CSS

The display attribute of CSS has many values, among which the commonly used values ​​are as follows:

  • none: hidden element and frees the space occupied by the element.
  • block: Display the element as a block-level element.
  • inline-block: Display the element as an inline block-level element.
  • inline: Display the element as an inline element.
  • table: Display the element as a table element.
  • table-row: Display the element as a table row element.
  • table-cell: Display the element as a table cell element.

2. Use the display attribute to hide and show the div element

  1. Hide the div element

Use the display attribute to hide and release the div element The space occupied by the element:

div {
  display: none;
}
  1. Display div element

Use the display attribute to display the div element as a block-level element:

div {
  display: block;
}

Use the display attribute Display a div element as an inline block-level element:

div {
  display: inline-block;
}

Use the display attribute to display a div element as an inline element:

div {
  display: inline;
}

Use the display attribute to display a div element as a table element:

div {
  display: table;
}

Use the display attribute to display the div element as a table row element:

div {
  display: table-row;
}

Use the display attribute to display the div element as a table cell element:

div {
  display: table-cell;
}

3. Use JavaScript to control the div Hiding and showing elements

If you want to control the hiding and showing of div elements when the user clicks a button, you can use JavaScript to achieve this.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>显示/隐藏div元素</title>
  <style>
    #myDiv {
      width: 200px;
      height: 200px;
      background-color: #ccc;
      display: block;
    }
  </style>
</head>
<body>
  <button onclick="toggleDiv()">显示/隐藏div元素</button>
  <div id="myDiv"></div>
  <script>
    function toggleDiv() {
      var div = document.getElementById("myDiv");
      if (div.style.display === "none") {
        div.style.display = "block";
      } else {
        div.style.display = "none";
      }
    }
  </script>
</body>
</html>

In the example, when the user clicks the button, the div element with the ID myDiv is obtained through JavaScript, and it is determined whether the display style attribute of the element is none. If so, it is set to block. Otherwise set it to none. This implements the hiding and display of div elements.

Summary

Through the introduction of this article, we can find that the display attribute of CSS is a very important attribute used to control the display and hidden state of elements on the page. Whether you are directly controlling the state of an element through CSS or implementing interactive functions through JavaScript, it is very necessary to master the use of the display attribute.

The above is the detailed content of How to use the display attribute of CSS to hide and display div elements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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