垂直线性渐变

时间:2018-10-12 20:54:25

标签: html css css3 linear-gradients

我正在尝试为页面上的侧边栏获得垂直效果。我尝试过deg选项,但它仍然显示水平线

.sidebar {
  position: relative;
  display: inline-block;
  padding: 15px 25px;
  background-image: linear-gradient(90deg, #1559EC, #1559EC);
  color: #fff;
  font-size: 36px;
  font-family: Arial;
  border-radius: 3px;
  box-shadow: 0px 1px 4px -2px #333;
  text-shadow: 0px -1px #333;
}

.sidebar:after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(100% - 4px);
  height: 50%;
  background: linear-gradient(rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.2));
}
<div class="sidebar">
  The quick brown fox
</div>

2 个答案:

答案 0 :(得分:2)

问题不是梯度,而是伪元素。渐变使用相同的颜色,因此角度无用。您需要的是反转伪元素上的高度/宽度值,并调整其倾斜方向。您还可以用简单的颜色替换主要元素的渐变:

.sidebar {
  position: relative;
  display: inline-block;
  padding: 15px 25px;
  background:#1559EC;
  color: #fff;
  font-size: 36px;
  font-family: Arial;
  border-radius: 3px;
  box-shadow: 0px 1px 4px -2px #333;
  text-shadow: 0px -1px #333;
}

.sidebar:after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 50%;
  height: calc(100% - 4px);
  background: linear-gradient(to right,rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.2));
}
<div class="sidebar">
  The quick brown fox
</div>

您可以像下面这样在主要元素上使用多个背景来简化它:

.sidebar {
  position: relative;
  display: inline-block;
  padding: 15px 25px;
  background:
   linear-gradient(to right,rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.2)) 2px 2px/50% calc(100% - 4px)no-repeat,
   #1559EC;
  color: #fff;
  font-size: 36px;
  font-family: Arial;
  border-radius: 3px;
  box-shadow: 0px 1px 4px -2px #333;
  text-shadow: 0px -1px #333;
}
<div class="sidebar">
  The quick brown fox
</div>

答案 1 :(得分:-1)

您要更改的渐变具有相同的两种颜色,因此看不到差异。使用生成器来生成所需内容的最简单方法是使用生成器,因为每个渲染引擎的代码略有不同。

最简单的关键字解决方案是使用“指向”而不是学位。见下文。第一个框是从上到下,第二个框是从左到右。

您的示例包含一个伪类(:after),添加了第二个渐变以创建硬线。您可以通过向渐变添加更多的光阑来达到类似的效果。

.box{
        width: 100px;
        height: 100px;
        margin-bottom: 20px
    }
.gradient1 {
    background: linear-gradient(to bottom, #8fc400, #29b8e5);
}
.gradient2 {
    background: linear-gradient(to right, #8fc400, #29b8e5);
}
.gradient3 {
    background: linear-gradient(to bottom, rgba(191,210,85,1) 0%,rgba(142,185,42,1) 50%,rgba(114,170,0,1) 51%,rgba(158,203,45,1) 100%);
} 
        <div class="box gradient1">
            
        </div>
        <div class="box gradient2">
            
        </div>
        <div class="box gradient3">
            
        </div>

相关问题