有哪些⽅式(CSS)可以隐藏⻚⾯元素?

x33g5p2x  于2022-04-17 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(346)

一、写在前面
css存在哪些方式可以隐藏页面的元素的方法,下面将对其进行总结。
二、总结方法
2.1、opacity:0

  1. <style>
  2. .first {
  3. visibility: hidden;
  4. }
  5. .last {}
  6. </style>
  7. <body>
  8. <div>
  9. <h1 class="first">我是第一行</h1>
  10. <h1 class="last">我是第二行</h1>
  11. </div>
  12. <script>
  13. let first = document.querySelector('.first')
  14. let last = document.querySelector('.last')
  15. first.addEventListener('click', () => {
  16. console.log('first')
  17. })
  18. last.addEventListener('click', () => {
  19. console.log('last')
  20. })
  21. </script>
  22. </body>

opacity: 0:将元素的透明度设置为0,就看起来隐藏了,但是任然可以占据空间且可以交互。
2.2、visibility: hidden

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .first {
  10. visibility: hidden;
  11. }
  12. .last {}
  13. </style>
  14. </head>
  15. <body>
  16. <div>
  17. <h1 class="first">我是第一行</h1>
  18. <h1 class="last">我是第二行</h1>
  19. </div>
  20. <script>
  21. let first = document.querySelector('.first')
  22. let last = document.querySelector('.last')
  23. first.addEventListener('click', () => {
  24. console.log('first')
  25. })
  26. last.addEventListener('click', () => {
  27. console.log('last')
  28. })
  29. </script>
  30. </body>
  31. </html>

visibility:hidden和opacity:0一样仍然占据空间,但是看不多,并且无法进行交互。
2.3、overflow:hidden

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .main {}
  10. .main .box {
  11. width: 100px;
  12. height: 100px;
  13. border: 1px solid #000;
  14. overflow: hidden;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="main">
  20. <div class="box">
  21. 哈哈哈哈哈哈
  22. 哈哈哈哈哈哈
  23. 哈哈哈哈哈哈
  24. 哈哈哈哈哈哈
  25. 哈哈哈哈哈哈
  26. 哈哈哈哈哈哈
  27. 哈哈哈哈哈哈
  28. 哈哈哈哈哈哈
  29. </div>
  30. <h1>大家好</h1>
  31. </div>
  32. </body>
  33. </html>

overflow:hidden,只隐藏溢出部分,并且不占据空间,也无法交互。
2.4、display:none
这个比较熟悉,从文档流中去除,不占据空间同时也无法交互。
2.5、z-index: -999

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. margin: 0px;
  11. padding: 0px;
  12. }
  13. .box1 {
  14. width: 200px;
  15. height: 200px;
  16. background-color: red;
  17. position: absolute;
  18. z-index: -999;
  19. }
  20. .box2 {
  21. width: 200px;
  22. height: 200px;
  23. background-color: green;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="box1"></div>
  29. <div class="box2"></div>
  30. </body>
  31. </html>

z-index:-999理是将层级放到底部,这样就被覆盖了,看起来隐藏了
2.6、transform: scale(0,0)

  1. <!DOCTYPE html>
  2. <html lang="cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .box1 {
  10. width: 200px;
  11. height: 200px;
  12. background-color: red;
  13. transform: scale(0, 0);
  14. }
  15. .box2 {
  16. width: 200px;
  17. height: 200px;
  18. background-color: green;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box1"></div>
  24. <div class="box2"></div>
  25. <script>
  26. let box1 = document.querySelector('.box1')
  27. let box2 = document.querySelector('.box2')
  28. box1.addEventListener('click', () => {
  29. console.log('box1')
  30. })
  31. box2.addEventListener('click', () => {
  32. console.log('box2')
  33. })
  34. </script>
  35. </body>
  36. </html>

transform: scale(0,0) :进行平面缩放,将大小缩小为0,但是任然占据空间,无法交互。

相关文章