【问题标题】:Setting linear gradient height AND width设置线性渐变高度和宽度
【发布时间】:2018-01-04 03:12:56
【问题描述】:

我知道您可以使用设置线性渐变的宽度

    .grey-block { background: linear-gradient(to right, #f9f9f9 0%, #f9f9f9 35%, white 35%, white 100%); }

还有身高

    .grey-block { background: linear-gradient(to bottom, #f9f9f9 0%, #f9f9f9 65%, white 65%, white 100%); }

但是,有没有一种方法可以使用同一条 css 线同时设置高度和宽度?

【问题讨论】:

标签: css linear-gradients


【解决方案1】:

为了澄清,问题中的代码不是设置渐变的heightwidth。它正在调整色标,结果是一个灰色的矩形。

为了调整渐变的实际尺寸,我们需要使用background-size属性(以及background-repeat)来设置渐变的heightwidth

通过background-size 控制渐变的尺寸,我们可以将 CSS 重写为如下:

.grey-block {
  background-color: white;
  background-image: linear-gradient(#f9f9f9, #f9f9f9);
  background-size: 35% 65%;
  background-repeat: no-repeat;
}

我们正在定义纯色的“渐变”并限制它的大小。 background-repeat 被禁用,因此它只会渲染一个灰色块。

.grey-block {
  background-color: white;
  background-image: linear-gradient(#f9f9f9, #f9f9f9);
  background-size: 35% 65%;
  background-repeat: no-repeat;
}


/* non-relevant styles */
body {
  background-color: #222;
}
.grey-block {
  height: 200px;
  width: 200px;
}
<div class="grey-block"></div>

【讨论】:

    【解决方案2】:

    您可以指定角度。这应该可以解决问题。

    .grey-block { background: linear-gradient( 135deg, #f9f9f9 0%, #f9f9f9 65%, white 65%, white 100%); }
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2013-10-22
      • 1970-01-01
      • 2011-01-14
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多