html – 未设置不透明度时元素消失:0.99

前端之家收集整理的这篇文章主要介绍了html – 未设置不透明度时元素消失:0.99前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个响应式页面,其中包含带有文本和模糊背景的框.该页面可在 JSFiddle获取.

问题是:.content元素在不将其不透明度设置为0.99时不可见.为什么?

HTML

  1. <div class="content-Box">
  2. <div class="content-bg"></div>
  3. <div class="content">
  4. <p>Text with blurred background</p>
  5. <p>Text with blurred background</p>
  6. </div>
  7. </div>

CSS

  1. body {
  2. background-image: url('http://hazor.iki.fi/2003/juhannus/isot/DSCN9068-Maisema.jpg');
  3. background-color: black;
  4. background-repeat: no-repeat;
  5. background-position: center;
  6. background-attachment: fixed;
  7. background-size: cover;
  8. background-size: no-repeat fixed center center cover;
  9. overflow: hidden;
  10. }
  11.  
  12. .content-Box {
  13. position: relative;
  14. width: 300px;
  15. overflow: hidden;
  16. }
  17.  
  18. .content-bg {
  19. position: absolute;
  20. background-size: cover;
  21. background-image: url('http://hazor.iki.fi/2003/juhannus/isot/DSCN9068-Maisema.jpg');
  22. background-color: black;
  23. background-repeat: no-repeat;
  24. background-position: center;
  25. background-attachment: fixed;
  26. background-size: cover;
  27. background-size: no-repeat fixed center center cover;
  28. filter: blur(5px);
  29. -webkit-filter: blur(5px);
  30. }
  31.  
  32. .content {
  33. border-radius: 10px;
  34. z-index: 100;
  35. background-color: rgba(0,0.7);
  36. opacity: 0.99999; /* Does not work without this wtf */
  37. color: white;
  38. }
  39. .content :first-child { margin-top: 0px }

JS

  1. function updateBackground() {
  2. var contentBox = $(".content-Box");
  3. var bg = $(".content-bg");
  4. bg.css("left",-(contentBox.offset().left));
  5. bg.css("top",-(contentBox.offset().top));
  6. bg.width($(window).width());
  7. bg.height($(window).height());
  8. }
  9.  
  10. $(window).resize(function() {
  11. updateBackground();
  12. });
  13.  
  14. updateBackground();

解决方法

为什么没有不透明度的代码不起作用?

这是因为您的content元素似乎位于content-bg元素的后面. z-index没有效果,因为没有为其分配位置属性.

为什么在添加0.99的不透明度时它会起作用?

正如BoltClock在this answer中所提到的,添加小于1的不透明度会自动创建新的堆叠上下文(类似于将z-index添加到定位元素).这使内容元素前进,从而显示.

什么是理想的解决方案?

添加position:relative会使z-index按预期工作(这会使元素高于content-bg)并且可以解决问题.

  1. function updateBackground() {
  2. var contentBox = $(".content-Box");
  3. var bg = $(".content-bg");
  4. bg.css("left",-(contentBox.offset().left));
  5. bg.css("top",-(contentBox.offset().top));
  6. bg.width($(window).width());
  7. bg.height($(window).height());
  8. }
  9.  
  10. $(window).resize(function() {
  11. updateBackground();
  12. });
  13.  
  14. updateBackground();
  1. body {
  2. background-image: url('http://hazor.iki.fi/2003/juhannus/isot/DSCN9068-Maisema.jpg');
  3. background-color: black;
  4. background-repeat: no-repeat;
  5. background-position: center;
  6. background-attachment: fixed;
  7. background-size: cover;
  8. background-size: no-repeat fixed center center cover;
  9. overflow: hidden;
  10. }
  11. .content-Box {
  12. position: relative;
  13. width: 300px;
  14. overflow: hidden;
  15. }
  16. .content-bg {
  17. position: absolute;
  18. background-size: cover;
  19. background-image: url('http://hazor.iki.fi/2003/juhannus/isot/DSCN9068-Maisema.jpg');
  20. background-color: black;
  21. background-repeat: no-repeat;
  22. background-position: center;
  23. background-attachment: fixed;
  24. background-size: cover;
  25. background-size: no-repeat fixed center center cover;
  26. filter: blur(5px);
  27. -webkit-filter: blur(5px);
  28. }
  29. .content {
  30. position: relative; /* add this */
  31. border-radius: 10px;
  32. z-index: 100;
  33. background-color: rgba(0,0.7);
  34. color: white;
  35. }
  36. .content:first-child {
  37. margin-top: 0px
  38. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div class="content-Box">
  3. <div class="content-bg"></div>
  4. <div class="content">
  5. <p>Text with blurred background</p>
  6. <p>Text with blurred background</p>
  7. </div>
  8. </div>

猜你在找的HTML相关文章