Home > Web Front-end > Front-end Q&A > How to show and hide div elements using CSS

How to show and hide div elements using CSS

PHPz
Release: 2023-04-21 10:23:11
Original
1002 people have browsed it

In web design and development, controlling the display and hiding of elements is a very important task. CSS provides a set of properties and methods to implement this function, the most commonly used of which are the display attribute and visibility attribute. This article will introduce how to use CSS div elements to achieve show and hide functions.

1. Display attribute

The display attribute is used to control whether the element is displayed. This attribute has multiple values, the most commonly used ones are block, inline and none. Their functions are as follows:

  1. block: render the element as a block-level element, that is, displayed on its own line. By default, div elements are block-level elements.
  2. inline: Render the element as an inline element, that is, displayed within the same line, but not on its own line. For example, elements such as a and span are inline elements by default.
  3. none: Hides the element but does not take up space. At this time, the element will neither be displayed on the page nor affect the layout.

To implement the display and hide function, we can do it by changing the value of the display attribute. For example, to hide a div element, you can use the following CSS style:

.hidden {
    display: none;
}
Copy after login

First define a div in the HTML page:

<div id="myDiv">这是一段要隐藏的内容</div>
Copy after login
Copy after login

Then define a show-hidden style in CSS:

.showDiv {
    display: block;
}

.hideDiv {
    display: none;
}
Copy after login

Use the following code in JavaScript to switch between display and hide:

var myDiv = document.getElementById('myDiv');
myDiv.classList.toggle('hideDiv');
myDiv.classList.toggle('showDiv');
Copy after login

In the above code, the display attribute of the div element is switched between hideDiv and showDiv styles through the toggle method, so You can achieve show and hide effects.

2. Visibility attribute

The visibility attribute is used to control whether an element is visible. This attribute also has multiple values, the most commonly used of which are visible and hidden. Their functions are as follows:

  1. visible: The element is visible, default value.
  2. hidden: The element is not visible, but takes up space.

Unlike the display attribute, the visibility attribute does not change the layout and position of the element. If you just want an element to be invisible but take up space, you can use the visibility:hidden attribute.

For example, here is a style that wants to hide a div element:

.visibility-hidden {
    visibility: hidden;
}
Copy after login

Define a div element in HTML and set an ID for it:

<div id="myDiv">这是一段要隐藏的内容</div>
Copy after login
Copy after login

In JavaScript Use the following code to achieve hiding and display:

var myDiv = document.getElementById('myDiv');
myDiv.classList.toggle('visibility-hidden');
Copy after login

In the above code, the visibility attribute of the div element is switched between visibility-hidden and the style without the class name through the toggle method, so that the display can be achieved and hidden effects.

3. Conclusion

The above is how to use CSS to control the display and hiding of div elements. Both methods can achieve similar effects, but in actual situations it is necessary to choose the appropriate method according to the specific application scenario. Also controlled through JS, more flexible and precise operations can be achieved.

The above is the detailed content of How to show and hide div elements using CSS. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template