我该如何做才能使此色带横幅的侧面不透明?

时间:2018-11-21 07:35:30

标签: html css ribbon banner

我想知道如何使底部色带显示色带的左侧和右侧?顶部显示完美,但是当我在页面上添加背景色时,功能区的左侧和右侧消失。

我已经坚持了一段时间,所以我想知道解决此问题的解决方案是什么?先感谢您!

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>

2 个答案:

答案 0 :(得分:0)

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

如果在部分中添加相对位置和z索引值,则这些值都将位于最前面。

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

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

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

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

答案 1 :(得分:0)

避免在主元素上施加任何影响并在伪元素上保留所有内容的技巧,然后您就可以简单地将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>