Home  >  Article  >  Web Front-end  >  What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden

What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden

yulia
yuliaOriginal
2018-09-06 18:07:432923browse

When we write pages, we often use hiding. What are the methods of hiding? What are the differences between them?
You can set the opacity value to 0, visibility to hidden or display to none. But each method has differences that allow us to make the right choice in a specific situation. The following article will talk to you about their differences, so that you can choose the appropriate method according to the occasion.

1.display:none
Set the display attribute to none. Using this attribute, hidden elements do not occupy any space. The effect of this method is as if the element does not exist at all, and the descendants of this element will also be hidden at the same time. That is, the element completely disappears from the page. In layman's terms, it cannot be seen or touched.
Example: Use style "display:none" to hide elements

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
   .a1{width: 100px;height: 100px;background: red;display: none;}
   .a2{width: 100px;height: 100px;background: yellow;}
  </style>
 </head>
 <body>
  <div class="a1">have a nice day</div>
  <div class="a2">have a nice day</div>
 </body>
</html>

Rendering:

What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden

2. Opacity
This attribute is to set the opacity of the object. When its transparency is 0, it disappears visually, but it still occupies that position. In layman's terms It's invisible but tangible. It also affects the layout of the web page. If an element is added with this attribute, its background and element content will also change accordingly.
Example: Use style "opacity: 0" to hide elements

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
   .a1{width: 100px;height: 100px;background: red;opacity: 0;}
   .a2{width: 100px;height: 100px;background: yellow;}
  </style>
 </head>
 <body>
  <div class="a1">have a nice day</div>
  <div class="a2">have a nice day</div>
 </body>
</html>

Rendering:

What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden

3. visibility: hidden
When the attribute value is set to hidden, the element will be hidden, but the space occupied by the object on the web page will not change. In layman's terms, it means that it is invisible but Touchable. And it affects the layout of the web page. This property is similar to the opacity property, but the only difference from opacity is that it does not respond to any user interaction.
Example: Use the style "visibility: hidden" to hide elements

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
   .a1{width: 100px;height: 100px;background: red;visibility: hidden;}
   .a2{width: 100px;height: 100px;background: yellow;}
  </style>
 </head>
 <body>
  <div class="a1">have a nice day</div>
  <div class="a2">have a nice day</div>
 </body>
</html>

Rendering:

What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden

The above introduces 3 types The hidden methods are different from each other. In actual projects, which one should be used depends on the situation.

The above is the detailed content of What are the ways to hide CSS? What is the difference between display: none, opacity: 0, visibility: hidden. 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