【问题标题】:How can I draw two lines obliquely with CSS (or SVG)?如何用 CSS(或 SVG)斜着画两条线?
【发布时间】:2017-01-04 12:58:08
【问题描述】:

我想用 CSS(或 SVG)创建附加的 div 元素的背景图片。

div.target {
  background-image: linear-gradient(
    to right bottom,
    transparent 50%,
    #00BCD4 50%
  );

我想用 CSS(或 SVG)创建的 div 元素的背景图片

【问题讨论】:

  • 我选择了最佳答案。

标签: css svg css-shapes linear-gradients


【解决方案1】:

我们可以使用多个背景图像渐变来做到这一点,如下面的 sn-p。较暗的阴影被指定为元素的背景颜色。然后,使用渐变创建的两个背景图像层以产生所需效果的方式放置。在较暗的阴影上方添加部分透明的白色层会产生较浅的阴影。

第二层的background-size应该更小,它的background-position应该在元素的左下方。

div {
  height: 200px;
  background-color: rgb(20,203,194);
  background-image: linear-gradient(to top left, rgba(255,255,255,0.25) 50%, rgba(255,255,255,0) 50%), linear-gradient(to top right, rgba(255,255,255,0.25) 50%, rgba(255,255,255,0) 50%);
  background-size: 100% 100%, 50px 50px;
  background-position: left top, left bottom;
  background-repeat: no-repeat;
}
<div></div>

已知有角度的 CSS 渐变会产生轻微的锯齿状(或不均匀或粗糙)边缘,可以通过稍微偏移颜色停止点来避免这种情况,如下面的演示所示。

div {
  height: 200px;
  background-color: rgb(20,203,194);
  background-image: linear-gradient(to top left, rgba(255,255,255,0.25) 50%, rgba(255,255,255,0) calc(50% + 1px)), linear-gradient(to top right, rgba(255,255,255,0.25) 50%, rgba(255,255,255,0) calc(50% + 1px));
  background-size: 100% 100%, 50px 50px;
  background-position: left top, left bottom;
  background-repeat: no-repeat;
}
<div></div>

【讨论】:

  • 我很惊讶,因为它按预期显示(可能是因为我认为它不能用 CSS 完成)。很有帮助!
【解决方案2】:

您可以使用:before:after 伪元素来做到这一点。

div {
  position: relative;
  width: 500px;
  height: 100px;
  background: #0BC7BE;
}
div:after {
  position: absolute;
  border-style: solid;
  border-width: 0 0 100px 500px;
  border-color: transparent transparent rgba(255, 255, 255, 0.3) transparent;
  right: 0;
  top: 0;
  content: "";
}
div:before {
  position: absolute;
  border-style: solid;
  border-width: 50px 0 0 70px;
  border-color: transparent transparent transparent rgba(255, 255, 255, 0.3);
  left: 0;
  bottom: 0;
  content: "";
}
<div></div>

【讨论】:

  • 我认为即使使用伪元素也无法实现。很有帮助!
猜你喜欢
  • 2021-07-08
  • 2016-07-30
  • 2021-06-18
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多