【问题标题】:How to draw a perfect diagonal with linear-gradient如何用线性渐变绘制完美的对角线
【发布时间】:2020-03-21 21:06:04
【问题描述】:

我正在尝试使用linear-gradient 绘制一个像样的对角线,但是当容器很小时我不知道该怎么做> 我正在尝试获得一个适合 10x10px 容器的对角线,看起来像这样:

这是我最好的尝试。

div {
  background: linear-gradient(50deg, transparent 4px, red 4px, red 5px, transparent 5px) no-repeat 0px 25px / 10px 10px;
  display:block;
  width:100px;
  height:100px;
}
<div></div>

我做错了什么?

【问题讨论】:

  • 你可能想使用这个工具:colorzilla.com/gradient-editor
  • 你试过45deg而不是50deg吗?
  • 是的,我尝试了很多角度,结果都一样。

标签: css linear-gradients diagonal


【解决方案1】:

您可以使用to [side] [side] 线性渐变,它是透明的,除了对角线的厚度,如下面的 sn-p 所示。

边框仅用于演示,实际上并不是渐变工作所必需的。

div {
  display: block;
  width: 100px;
  height: 100px;
  border: 1px solid;
  margin: 10px;
}
.border-2px {
  background: linear-gradient(to bottom left, transparent calc(50% - 2px), red calc(50% - 1px), red calc(50% + 1px), transparent calc(50% + 2px)) no-repeat 0px 0px / 100px 100px;
}
.border-1px {
  background: linear-gradient(to bottom left, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px)) no-repeat 0px 0px / 100px 100px;
}
.border-1px.small {
  height: 10px;
  width: 10px;
  background: linear-gradient(to bottom left, transparent calc(50% - .5px), red 50%, transparent calc(50% + .5px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-2 {
  height: 10px;
  width: 10px;
  background: linear-gradient(to bottom left, transparent calc(50% - 1px), #EEE calc(50% - .5px), red 50%, #EEE calc(50% + .5px), transparent calc(50% + 1px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-3 {
  background: linear-gradient(to bottom left, transparent calc(50% - .5px), red 50%, transparent calc(50% + .5px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-4 {
  background: linear-gradient(to bottom left, transparent calc(50% - 1px), #EEE calc(50% - .5px), red 50%, #EEE calc(50% + .5px), transparent calc(50% + 1px)) no-repeat 0px 0px / 10px 10px;
}
<div class='border-2px'></div>
<div class='border-1px'></div>
<div class='border-1px small'></div>
<div class='border-1px small-2'></div>
<div class='border-1px small-3'></div>
<div class='border-1px small-4'></div>

您的方法没有错,但在创建对角线时最好避免角度线性渐变,因为角度线性渐变并不总是产生对角线。根据容器的尺寸,产生的线可以是对角线(或)盒子内任何位置的线。您可以在my answer here 中找到更多相关信息。使用to [side][side] 渐变的另一个优点是它具有响应性。


如果渐变不适合你,那么你可以看看使用 SVG,但我认为你不能在对角线方面创建具有精确粗细的线条。对角线不像直线那么简单。

div {
  position: relative;
  height: 100px;
  width: 100px;
}
svg {
  position: absolute;
  height: 10px;
  width: 10px;
  top: 0px;
  left: 0px;
}
path {
  stroke-width: 1.05;
  stroke: red;
  fill: none;
}
<div>
  <svg viewBox='0 0 10 10'>
    <path d='M0,0 10,10' />
  </svg>
</div>

here 提供了有关如何使用 SVG 对角线作为背景图像的演示。 SVG 图像也可以叠加在其他背景图像之上。

【讨论】:

  • 公平地说,我可以使用大容器获得对角线,但我不能像我的示例中那样使用小容器。我需要一个 10x10px 正方形的 1px 宽度的对角线,但这条线要么太宽,要么太模糊。你能举一个对角线那么小的例子吗?谢谢。
  • 你能像我在我的问题上发布的图片那样让它更薄吗?这正是我的问题。
  • 不,您必须使用倒数第二个或最后一个。我认为 sn-p 中的最后一个div 最接近有问题的图像(我刚刚看到)。如果你试图在两者之间做任何事情,它会变得模糊。这就是渐变的工作原理。
  • @CainNuke:我为.border-1px.small-4.border-1px.small-2 添加了一个稍微不同的变体,在它们之间引入了另一种白色。它在一定程度上避免了模糊,看起来比.border-1px.small-1略厚,但比原来的.border-1px.small-4更薄。检查它是否有帮助。我认为没有比这更接近的了。
  • 这太不可思议了。我认为这是解决我所有问题的方法。您是使用任何特殊工具绘制 SVG 还是手动绘制?
【解决方案2】:

您可以使用::after 伪类来执行此操作。

div{
width:28px;
height:28px;
position:relative;}

div:after{
content:"";
position:absolute;
border-top:1px solid red;
  width:40px;
  transform: rotate(45deg);
  transform-origin: 0% 0%;
}
<div>
  </div>

【讨论】:

  • 谢谢,但这在这种情况下不起作用,因为我将在同一个元素上有多个线性渐变,并且使用旋转将旋转所有这些,这不是我想要的。仅使用线性梯度就不可能实现单条对角线吗?
【解决方案3】:

白线

方向: 上、左 => 下、右

.line {
  background: linear-gradient(
    45deg,
    transparent,
    transparent 45%,
    #fff 45%,
    #fff 55%,
    transparent 55%,
    transparent 100%
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2021-07-18
    相关资源
    最近更新 更多