【问题标题】:Vertical Linear Gradient垂直线性渐变
【发布时间】:2018-10-12 20:54:24
【问题描述】:

我正在尝试为页面上的侧边栏获得垂直效果。我试过 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>

【问题讨论】:

标签: html css linear-gradients


【解决方案1】:

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

.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>

【讨论】:

    【解决方案2】:

    您尝试更改的渐变具有相同的两种颜色,因此您不会看到差异。制作你想要的东西的最简单方法是使用生成器,因为每个渲染引擎的代码都有点不同。

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

    您的示例有一个伪类 (: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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2018-09-11
      • 1970-01-01
      • 2011-07-07
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多