css – 显示背景图像的透明边框

前端之家收集整理的这篇文章主要介绍了css – 显示背景图像的透明边框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个:

我想实现这个目标:

我有一个大的外部div(红色背景)和一个较小的内部div(绿色背景).小div有边框,我希望边框显示为透明以显示背后的背景.这是用HTML / CSS实现的吗?

解决方法

您可以使用伪元素实现显示背景图像的透明边框.

红色背景是伪元素的边框,透明边框由元素背景和伪元素边框之间的间隙创建:

DEMO

  1. body{
  2. background:url('https://farm4.staticflickr.com/3771/13199704015_72aa535bd7.jpg');
  3. background-size:cover;
  4. }
  5. .big{
  6. margin:50px;
  7. padding:50px;
  8. min-height:500px;
  9. overflow:hidden;
  10. }
  11. .big p{
  12. position:relative;
  13. z-index:1;
  14. }
  15. .small{
  16. position:relative;
  17. background:teal;
  18. width:150px;height:150px;
  19. margin:25px;
  20. z-index:0;
  21. }
  22. .small:before{
  23. content:'';
  24. position:absolute;
  25. top:-5025px; left:-5025px;
  26. width:200px; height:200px;
  27. border:5000px solid rgba(255,255,0.8);
  28. background:none;
  29. }
  1. <div class="big">
  2. <p>content here</p>
  3. <div class="small"></div>
  4. <p>content here</p>
  5. </div>

输出

您也可以使用Box-shadow而不是border,这样就不必为伪元素的顶部/左侧定位使用负值.浏览器支持isn’t as good作为边框:

  1. body{
  2. background:url('https://farm4.staticflickr.com/3771/13199704015_72aa535bd7.jpg');
  3. background-size:cover;
  4. }
  5. .big{
  6. margin:50px;
  7. padding:50px;
  8. min-height:500px;
  9. overflow:hidden;
  10. }
  11. .big p{
  12. position:relative;
  13. z-index:1;
  14. }
  15. .small{
  16. position:relative;
  17. background:teal;
  18. width:150px;height:150px;
  19. margin:25px;
  20. z-index:0;
  21. }
  22. .small:before{
  23. content:'';
  24. position:absolute;
  25. top:-25px; left:-25px;
  26. width:200px; height:200px;
  27. Box-shadow: 0px 0px 0px 5000px rgba(255,0.8);
  28. background:none;
  29. }
  1. <div class="big">
  2. <p>content here</p>
  3. <div class="small"></div>
  4. <p>content here</p>
  5. </div>

猜你在找的CSS相关文章