:: after中与div中相同的线性渐变背景

时间:2018-07-14 20:05:23

标签: html css background element linear-gradients

早上好, 我希望背景与标题相同,线条渐变在哪里。

html, body { margin: 0; background:  #d8dfe9;}
header{position: relative; height: 90px; width: 100%; background: linear-gradient(to right, #045FB4 0%,#00BFFF 100%);}
header:after {content: ''; position: absolute; left: calc(50% - 4.8px); top: 90px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid blue; clear: both; z-index: 1;}
main {position: relative; overflow: auto; ; height: 100vh;}
main #timeline {position: relative; width: 100%; height: 70px; background: white}
<header>
  
</header>
<main>
  <div id="timeline">

  </div>
</main>

请,你能帮我吗?

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是使用clip-path

html,
body {
  margin: 0;
  background: #d8dfe9;
}

header {
  position: relative;
  height: 90px;
  width: 100%;
  background: linear-gradient(to right, #045FB4 0%, #00BFFF 100%);
  -webkit-clip-path: polygon(0% 0%, 100% 0%, 100% calc(100% - 10px), calc(50% + 10px) calc(100% - 10px), 50% 100%, calc(50% - 10px) calc(100% - 10px), 0% calc(100% - 10px));
clip-path: polygon(0% 0%, 100% 0%, 100% calc(100% - 10px), calc(50% + 10px) calc(100% - 10px), 50% 100%, calc(50% - 10px) calc(100% - 10px), 0% calc(100% - 10px));
}
<header>

</header>

或者,如果您想获得比clip-path更好的支持,但又没有透明度,则可以使用更多的梯度:

html,
body {
  margin: 0;
  background: #d8dfe9;
}

header {
  position: relative;
  height: 90px;
  width: 100%;
  background: linear-gradient(to right, #045FB4 0%, #00BFFF 100%) fixed;
  position:relative;
}
header:before {
  content:"";
  position:absolute;
  width:20px;
  height:10px;
  top:100%;
  right:calc(50% - 10px);
  background:
   linear-gradient(to bottom left,transparent 49%,#d8dfe9 50.5%) left/50% 100% no-repeat,
   linear-gradient(to bottom right,transparent 49%,#d8dfe9 50.5%) right/50% 100% no-repeat,
   linear-gradient(to right, #045FB4 0%, #00BFFF 100%) fixed;
}
<header>

</header>