【问题标题】:How to partially color an HTML element's background from bottom to top using CSS? [duplicate]如何使用 CSS 从下到上对 HTML 元素的背景进行部分着色? [复制]
【发布时间】:2021-10-14 08:02:52
【问题描述】:

我已经设法使用以下方法从上到下实现了我想要做的事情:

/* I'm interested in filling with a solid color, but in order to partially fill the background, I seem to have to use a dummy gradient to make the color behave as an image */
.test {
  width: 50px;
  height: 100px;
  border: 5px solid black;
  background-image: linear-gradient(blue, blue);
  background-size: 100% 50%;
  background-repeat: no-repeat;
}
<div class="test"></div>

在这个例子中,有没有办法从下到上填充 div,使下半部分为蓝色,另一半为白色?

【问题讨论】:

标签: html css


【解决方案1】:

您可以将background: blue 设置为第一个属性并将线性梯度更改为white, white 的值以反转声明。

/* I'm interested in filling with a solid color, but in order to partially fill the background, I seem to have to use a dummy gradient to make the color behave as an image */
.test {
  background: blue;
  width: 50px;
  height: 100px;
  border: 5px solid black;
  background-image: linear-gradient(white, white);
  background-size: 100% 50%;
  background-repeat: no-repeat;
}
<div class="test"></div>

【讨论】:

    【解决方案2】:

    这是你的意思吗?您可以更改background-size: 100% 70% 来调整您希望它进入另一个空间的距离。

    .test {
      width: 50px;
      height: 100px;
      border: 5px solid black;
      background-image: linear-gradient(white, blue);
      background-size: 100% 70%;
      background-repeat: no-repeat;
      background-position: bottom;
    }
    <div class="test">
    </div>

    【讨论】:

    • 是的,只是没有实际的渐变,这里的关键是background-position: bottom,让它工作起来又好又容易
    • 哦,对不起!以为我们保持渐变
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2021-02-14
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2018-06-29
    相关资源
    最近更新 更多