html - 纯粹在 CSS 背景中创建箭头

标签 html css css-shapes

我需要创建一个 CSS 类,该类可以动态添加到各种空的方形 div 框中,这些框将添加“半 Angular 尖线”,就像下面的示例所示。理想情况下,我只需将 class="arrow_right"添加到 div 即可显示正确的行。仅使用 CSS 可以吗?我不想依赖静态图像来执行此操作,以实现灵活性。谢谢!

examples

最佳答案

这是一个使用背景的想法:

.box {
  width:150px;
  height:150px;
  display:inline-block;
  border:2px solid;
}

.left {
  background:
    linear-gradient(135deg,transparent calc(15px*0.707),black calc(15px*0.707)) 100% calc(50% - 15px/2),
    linear-gradient( 45deg,transparent calc(15px*0.707),black calc(15px*0.707)) 100% calc(50% + 15px/2);
  background-size:50% 15px;
  background-repeat:no-repeat;
}

.right {
  background:
    linear-gradient(-135deg,transparent calc(15px*0.707),black calc(15px*0.707)) 0 calc(50% - 15px/2),
    linear-gradient( -45deg,transparent calc(15px*0.707),black calc(15px*0.707)) 0 calc(50% + 15px/2);
  background-size:50% 15px;
  background-repeat:no-repeat;
}

.top {
  background:
    linear-gradient( 135deg,transparent calc(15px*0.707),black calc(15px*0.707)) calc(50% - 15px/2) 100%,
    linear-gradient(-135deg,transparent calc(15px*0.707),black calc(15px*0.707)) calc(50% + 15px/2) 100%;
  background-size:15px 50%;
  background-repeat:no-repeat;
}
.bottom {
  background:
    linear-gradient( 45deg,transparent calc(15px*0.707),black calc(15px*0.707)) calc(50% - 15px/2) 0,
    linear-gradient(-45deg,transparent calc(15px*0.707),black calc(15px*0.707)) calc(50% + 15px/2) 0;
  background-size:15px 50%;
  background-repeat:no-repeat;
}
<div class="box left">

</div>
<div class="box right">

</div>
<div class="box top">

</div>
<div class="box bottom">

</div>

使用 CSS 变量,您可以优化代码:

.box {
  width:150px;
  height:150px;
  display:inline-block;
  border:2px solid;
}

.arrow {
  --s:15px;  /* Size */
  --c:black; /* Color */
  --g:transparent calc(var(--s)*0.707),var(--c) calc(var(--s)*0.707);
  
  background-image:linear-gradient(var(--a1),var(--g)),linear-gradient(var(--a2),var(--g));
  background-repeat:no-repeat;
}

.left {
  --a1:135deg;
  --a2:45deg;
  background-position: 100% calc(50% - var(--s)/2),  100% calc(50% + var(--s)/2);
  background-size:50% var(--s);
}

.right {
  --a1:-135deg;
  --a2:-45deg;
  background-position: 0 calc(50% - var(--s)/2),  0 calc(50% + var(--s)/2);
  background-size:50% var(--s);
}

.top {
  --a1:135deg;
  --a2:-135deg;
  background-position: calc(50% - var(--s)/2) 100%,  calc(50% + var(--s)/2) 100%;
  background-size:var(--s) 50%;
}
.bottom {
  --a1: 45deg;
  --a2:-45deg;
  background-position: calc(50% - var(--s)/2) 0,  calc(50% + var(--s)/2) 0;
  background-size:var(--s) 50%;
}
<div class="box arrow left">

</div>
<div class="box arrow right" style="--s:20px;--c:red">

</div>
<div class="box arrow top" style="--s:10px;--c:blue">

</div>
<div class="box arrow bottom"  style="--s:30px;--c:green">

</div>

关于html - 纯粹在 CSS 背景中创建箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231756/

相关文章:

java - 将静态 HTML 保存到数据库字段或从文件读取静态 HTML 更容易吗?

html - 在博客博客中实现 css 按钮

javascript - 一页滚动 : Trying to redirect to home page and scroll to the div - Not Working?

javascript - HTML 电子邮件模板解决方案到 table TD 中的 javascript 链接?

html - CSS:选择特定类型元素在 HTML 文档中的特定出现

html - CSS3 - 特殊边框半径

html - 如何在 CSS 中创建丝带形状

css - 带有CSS的图像的中心箭头

html - 如何在可滚动容器的CSS中垂直居中元素

HTML - 如何在不重复代码或 JS 的情况下处理阿拉伯语文本倒置