【问题标题】:How do I made it so that the sides of this ribbon banner isn't transparent?如何使此功能区横幅的侧面不透明?
【发布时间】:2018-11-21 07:35:29
【问题描述】:

我想知道如何使底部功能区显示功能区的左侧和右侧?顶部的显示非常好,但是当我向页面添加背景颜色时,功能区的左侧和右侧消失了。

我已经被这个问题困扰了一段时间,所以我想知道解决这个问题的解决方案是什么?先感谢您!

body {
  text-align: center;

}

section {
  background-color: aqua;
}

h6 {
  display: inline-block;
  font-size: 34px;
  line-height: 28px;
  color: #8699a0;
  text-shadow: 0 0 1px #758890;
  margin: 120px 0;
  font-family: arial, verdana;
  outline: none;
  padding: 14px 30px;
  position: relative;
  text-transform: uppercase;
  box-shadow:
    0 0 30px 0 rgba(0, 0, 0, 0.1),
    0 36px 0 -18px #b1e3e2;
}

/*the ribbon ends*/
h6:before {
  content: "";
  position: absolute;
  top: 18px;
  left: -15%;
  z-index: -1;
  width: 130%;
  /*Using the triangle logic - 2-sided borders and 0 height. That will create negative triangles on the left and right.*/
  height: 0;
  border: 28px solid rgba(0, 0, 0, 0);
  border-left: 28px solid #b1e3e2;
  border-right: 28px solid #b1e3e2;
}

h6:after {
  content:"";
  width: 100%;
  height: 0;
  position: absolute;
  top: 100%;
  left: 0;
  z-index: -1;
  border-top: 18px solid #99acb2;
  border-left: 18px solid transparent;
  border-right: 18px solid transparent;
}

h6, h6:before {
  background-image:
    /*2 gray borders*/
    linear-gradient(
      transparent 8%,
      rgba(0, 0, 0, 0.1) 8%,
      rgba(0, 0, 0, 0.1) 14%,
      transparent 14%,
      transparent 86%,
      rgba(0, 0, 0, 0.1) 86%,
      rgba(0, 0, 0, 0.1) 92%,
      transparent 92%
    ),
    /*white gloss gradient*/
    linear-gradient(
      rgba(255, 255, 255, 0.75),
      rgba(255, 255, 255, 0)
    ),
    /*thin stripes*/
    linear-gradient(
      45deg,
      transparent 40%,
      rgba(0, 0, 0, 0.1) 40%,
      rgba(0, 0, 0, 0.1) 60%,
      transparent 60%
      ),
    /*white base*/
    linear-gradient(white, white);
  background-size:
    cover, /*borders*/
    cover, /*white gloss*/
    4px 4px, /*thin stripes*/
    cover; /*base white*/
}

h6, h6:before, h6:after {
  box-sizing: border-box;
  /*fix to make the borders appear on the ribbon ends as well*/
  background-origin: border-box;
}
<h6>BUY ONLINE AND SAVE</h6>

<section>
  
<h6>BUY ONLINE AND SAVE</h6>
  
</section>

【问题讨论】:

    标签: html css ribbon banner


    【解决方案1】:

    问题是您将 h6:before 和 h6:after 元素设置为背景一层,并使用 z-index-1 将它们放在 h6 后面。

    如果你为你的部分添加一个相对位置和一个任意值的 z-index,它都会出现在前面。

    section {
      background-color: aqua;
      position: relative;
      z-index: 0; // works with any value
    }
    

    解决方案的重点是,通过将位置和 z-index 添加到您的部分,您将其定义为堆叠上下文根元素,而不是 HTML 根元素,自动移动任何子元素,即使其中的 z-index 为负它的前面。 简而言之,这是堆叠顺序:

    1. 堆叠上下文的根元素
    2. 定位的元素(及其 子)具有负 z-index 值(较高的值堆叠在 较低值的前面;具有相同值的元素被堆叠 根据 HTML 中的外观)
    3. 非定位元素(按 HTML 中的外观排序)
    4. z-index 值为 auto(按在 HTML)
    5. 具有正 z-index 的定位元素(及其子元素) 值(较高的值堆叠在较低值的前面;元素 具有相同值的根据在 HTML 中的外观堆叠)

    有关堆叠的更完整说明,请参阅以下文章 https://philipwalton.com/articles/what-no-one-told-you-about-z-index/

    【讨论】:

      【解决方案2】:

      避免对主元素应用任何效果并将所有内容保留在伪元素上的技巧然后您将能够简单地将z-index:0 添加到主元素以避免通过使所有内容属于相同的堆叠上下文来避免问题。

      这里是修改后的代码:

      body {
        text-align: center;
      
      }
      
      section {
        background-color: aqua;
      }
      
      h6 {
        display: inline-block;
        font-size: 34px;
        color: #8699a0;
        text-shadow: 0 0 1px #758890;
        margin: 120px 0;
        outline: none;
        padding: 14px 30px;
        position: relative;
        z-index:0;
        text-transform: uppercase;
        box-shadow:
          0 0 30px 0 rgba(0, 0, 0, 0.1),
          0 36px 0 -18px #b1e3e2;
      }
      
      h6:before {
        content: "";
        position: absolute;
        z-index: -2;
        top: 18px;
        bottom: -18px;
        left: -40px;
        right:-40px;
        background:
        linear-gradient(to top left,transparent 49.8%,#99acb2 50%) right 40px top 100%/20px 18px no-repeat,
          linear-gradient(to top right,transparent 49.8%,#99acb2 50%) 40px 100%/20px 18px no-repeat,
          linear-gradient(#b1e3e2,#b1e3e2) center/calc(100% - 120px) 100% no-repeat,
        
          linear-gradient(rgba(0, 0, 0, 0.1),rgba(0, 0, 0, 0.1)) 0 8%/100% 6% no-repeat,
          linear-gradient(rgba(0, 0, 0, 0.1),rgba(0, 0, 0, 0.1)) 0 92%/100% 6% no-repeat,
          linear-gradient(rgba(255, 255, 255, 0.75),rgba(255, 255, 255, 0)) no-repeat,
          linear-gradient(45deg, transparent 40%,rgba(0, 0, 0, 0.1) 40%,
                  rgba(0, 0, 0, 0.1) 60%,transparent 60%) left/4px 4px,
          #fff;
      }
      
      h6:after {
        content:"";
        position: absolute;
        right:0;
        left: 0;
        top:0;
        bottom:0;
        z-index: -1;
        background:
          linear-gradient(rgba(0, 0, 0, 0.1),rgba(0, 0, 0, 0.1)) 0 8%/100% 6% no-repeat,
          linear-gradient(rgba(0, 0, 0, 0.1),rgba(0, 0, 0, 0.1)) 0 92%/100% 6% no-repeat,
          linear-gradient(rgba(255, 255, 255, 0.75),rgba(255, 255, 255, 0)) no-repeat,
          linear-gradient(45deg, transparent 40%,rgba(0, 0, 0, 0.1) 40%,
                  rgba(0, 0, 0, 0.1) 60%,transparent 60%) left/4px 4px,
          #fff;
      }
      
      h6, h6:before, h6:after {
        box-sizing: border-box;
      }
      <h6>BUY ONLINE AND SAVE</h6>
      
      <section>
        
      <h6>BUY ONLINE AND SAVE</h6>
        
      </section>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-15
        • 2017-06-20
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多