【问题标题】:Linear gradient with custom size具有自定义大小的线性渐变
【发布时间】:2020-11-13 16:03:28
【问题描述】:

我有这个设计:

它已经使用 html/css 创建,但我需要删除 1 和 5 的额外线条。 这是通过添加绝对位置元素来创建灰线来实现的,但容器点的大小是响应式的。

我的想法是为每个容器创建一个背景线性渐变,如下所示:

所有人

background: linear-gradient(to right, grey 100%, transparent 0);

第一个:

background: linear-gradient(to left, grey 50%, transparent 0);

最后一个:

background: linear-gradient(to right, grey 50%, transparent 0);

但我不知道如何使它变小:

【问题讨论】:

    标签: css


    【解决方案1】:

    假设所有项目的宽度相等,那么渐变应该从 1 的中点开始,到 5 的中点结束,这意味着它应该从 10% 标记开始,到 90% 标记结束,类似于 @ 987654321@.

    要限制背景的大小,可以使用background-size: 100% [DESIRED_HEIGHT]来控制。假设您希望栏的厚度为 16px,那么您可以使用background-size: 100% 16px。将其与 background-repeat: no-repeat 结合使用可避免渐变重复,将其与 background-position: center center 结合使用可使其居中。

    参见概念验证示例:

    body {
      font-family: Arial, sans-serif;
    }
    
    ul {
      list-style: none;
      padding: 0;
      margin: 0;
      display: flex;
      align-items: center;
      justify-content: space-around;
      
      background-image: linear-gradient(to right, transparent 10%, grey 10%, grey 90%, transparent 90%);
      background-size: 100% 8px;
      background-repeat: no-repeat;
      background-position: center center;
    }
    
    ul.colors {
      background-image: linear-gradient(
        to right,
        transparent 10%,
        red 10%,
        red 30%,
        orange 30%,
        orange 50%,
        green 50%,
        green 70%,
        blue 70%,
        blue 90%,
        transparent 90%
      );
    }
    
    ul li {
      display: block;
      width: 48px;
      height: 48px;
      border: 1px solid #999;
      border-radius: 50%;
      background-color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    <strong>Plain</strong>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>
    <br /><br />
    <strong>Colors</strong>
    <ul class="colors">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    【讨论】:

    • 我希望我能给你 100 分。谢谢!
    • @Dani 其实可以的,这里叫赏金。
    • 我觉得我还做不到
    • @Dani 别担心,把 100 分留给你以后真正需要赏金的问题;)
    • 仍然不知道那在哪里,但没关系:D
    【解决方案2】:

    另一个想法是考虑第一个和最后一个项目的负边距等于大小的一半:

    body {
      font-family: Arial, sans-serif;
    }
    
    ul {
      list-style: none;
      padding: 0;
      margin: 0 24px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      background:linear-gradient(grey 0 0) center/100% 10px no-repeat;
    }
    
    
    ul li {
      display: block;
      width: 48px;
      height: 48px;
      border: 1px solid #999;
      border-radius: 50%;
      background-color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    ul li:first-child {
      margin-left:-24px;
    }
    ul li:last-child {
      margin-right:-24px;
    }
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    【讨论】:

    • 当容器内的元素是动态的时,你不能有一个固定的边距。主 div 是一个 display: flex,它们会根据它增长
    • @Dani euh,我在你的问题中没有看到这个要求,你接受了使用 fixed width 的答案
    • 正确,但我没有提到容器也有固定宽度。它反应灵敏。不,另一种解决方案只有灰色条的固定高度,这正是我正在寻找的。另外,看看你的结果,它没有为问题提供任何解决方案,我仍然看到灰色的大条,这就是我所拥有的
    • @Dani 栏的固定高度很容易(检查更新)我认为你的主要问题是修剪每一侧的额外 gey 部分,这是我关注的。
    猜你喜欢
    • 1970-01-01
    • 2012-03-27
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多