What are the ways CSS is hidden? Display:none,opacity:0,visibility:hidden, what's the difference?

Source: Internet
Author: User
When we write a page, we often use the hidden, hidden methods, what are they different?
You can set the opacity value to 0, set the visibility to hidden, or display set to none. But each approach has its differences, which allow us to make the right choices on a particular occasion. The following article will talk about their differences, so that we can choose the right way according to the occasion.





1.display:none
Set the Display property to none, using this property, the hidden element does not occupy any space. In this way the effect is as if the element does not exist at all, and the descendant elements of this element are also hidden. That is, the element disappears completely on the page, and in layman's terms it cannot be seen or touched.
Example: Using 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>


2, opacity

The property is to set the opacity of the object. When his transparency is 0, it visually disappears, but he still occupies that position, which is generally invisible but tangible. And for the layout of the web page, the elements of the attribute are added, and its background and element content will also change.
Example: Hide the element with the style "opacity:0"

<!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>


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 webpage has not changed. In general, it is invisible but can be touched. And for the layout of the 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: Hide the element with the style "visibility: hidden"

<!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>


The above describes three hidden methods, each of which is different. In actual projects, which one should be used depends on the situation.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.