如何使用线性梯度实现两个选项卡

时间:2019-04-22 12:00:06

标签: html css linear-gradients

有两个选项卡,每个选项卡的背景颜色是线性的,并且两个都有边框半径。它们如下所示:

enter image description here

enter image description here

每个标签的背景是

linear-gradient(to right, #FF7243, #FF563E);

代码如下:

.tab-container {
  width: 200px;
  display: flex;
  height: 40px;
}

.tab-item {
  flex: 1;
  background: linear-gradient(to right, #FF7243, #FF563E);
  border-top-right-radius: 10px;
  border-top-left-radius: 10px
}
<div class="tab-container">
  <div class="tab-item"></div>
  <div class="tab-item"></div>
</div>

由于边界半径,两个选项卡之间将出现空白,那么如何通过选项卡开关更改空白部分的背景颜色?

2 个答案:

答案 0 :(得分:2)

使两个标签重叠,如下所示。您也可以在不需要时删除半径。

.tab-container {
  width: 200px;
  display: flex;
  height: 40px;
  margin:10px;
}

.tab-item {
  flex: 1;
  background: linear-gradient(to right, #FF7243, #FF563E);
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
}

.tab-item:first-child {
  margin-right:-10px;
}

.active {
 z-index:1; /*active always on the top*/
  background: linear-gradient(to right, purple, blue);
}
/*when the first child is not active no need top-right radius*/
:not(.active):first-child {
  border-top-right-radius: 0;
}
/*when the last child is not active no need top-left radius*/
:not(.active):last-child {
  border-top-left-radius: 0;
}
<div class="tab-container">
  <div class="tab-item active"></div>
  <div class="tab-item"></div>
</div>
<div class="tab-container">
  <div class="tab-item"></div>
  <div class="tab-item active"></div>
</div>

答案 1 :(得分:-1)

.tab-container { width: 200px; display: flex; height: 40px; } .tab-item { flex: 1; background: linear-gradient(to right, #FF7243, #FF563E); border-top-right-radius: 10px; border-top-left-radius: 10px }

<div class="tab-container"> <div class="tab-item"></div> <div class="tab-item"></div>