如何使用CSS制作花式箭头?

Tom*_*may 100 html css css-shapes

好的,所以每个人都知道你可以用这个做三角形:

#triangle {
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 100px solid red;
}
Run Code Online (Sandbox Code Playgroud)

这产生了一个坚实的,填充三角形.但是你会怎么做一个类似于空心的箭头状三角形呢?

只有两条线在该点相交的箭头. 像一个大于号:>

Gol*_*rol 92

您可以使用beforeor after伪元素并对其应用一些CSS.有各种方式.您可以同时beforeafter,和旋转和定位每个人形成的酒吧之一.一个更简单的解决方案是仅向before元素添加两个边框并使用它旋转它transform: rotate.

向下滚动以查找使用实际元素而不是pseuso元素的其他解决方案

在这种情况下,我在列表中添加了箭头作为项目符号,并使用em大小使其大小与列表的字体一致.

ul {
    list-style: none;
}

ul.big {
    list-style: none;
    font-size: 300%
}

li::before {
    position: relative;
    /* top: 3pt; Uncomment this to lower the icons as requested in comments*/
    content: "";
    display: inline-block;
    /* By using an em scale, the arrows will size with the font */
    width: 0.4em;
    height: 0.4em;
    border-right: 0.2em solid black;
    border-top: 0.2em solid black;
    transform: rotate(45deg);
    margin-right: 0.5em;
}

/* Change color */
li:hover {
  color: red; /* For the text */
}
li:hover::before {
  border-color: red; /* For the arrow (which is a border) */
}
Run Code Online (Sandbox Code Playgroud)
<ul>
    <li>Item1</li>
    <li>Item2</li>
    <li>Item3</li>
    <li>Item4</li>
</ul>

<ul class="big">
    <li>Item1</li>
    <li>Item2</li>
    <li>Item3</li>
    <li>Item4</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

当然,您不需要使用before或者after,您也可以将相同的技巧应用于普通元素.对于上面的列表,它很方便,因为您不需要额外的标记.但有时你可能想要(或需要)标记.你可以使用一个divspan那个,我甚至看到人们甚至回收i元素为'图标'.所以标记可能如下所示.是否<i>正确使用是有争议的,但你也可以使用span来保证安全.

/* Default icon formatting */
i {
  display: inline-block;
  font-style: normal;
  position: relative;
}

/* Additional formatting for arrow icon */
i.arrow {
    /* top: 2pt; Uncomment this to lower the icons as requested in comments*/
    width: 0.4em;
    height: 0.4em;
    border-right: 0.2em solid black;
    border-top: 0.2em solid black;
    transform: rotate(45deg);
}
Run Code Online (Sandbox Code Playgroud)
And so you can have an <i class="arrow" title="arrow icon"></i> in your text.
This arrow is <i class="arrow" title="arrow icon"></i> used to be deliberately lowered slightly on request.
I removed that for the general public <i class="arrow" title="arrow icon"></i> but you can uncomment the line with 'top' <i class="arrow" title="arrow icon"></i> to restore that effect.
Run Code Online (Sandbox Code Playgroud)

如果您寻求更多灵感,请务必查看Nicolas Gallagher提供的纯CSS图标库.:)


Ala*_*ing 70

这可以比其他建议更容易解决.

只需画一个正方形并将一个border属性应用于2个连接边.

然后根据箭头指向的方向旋转方块,例如: transform: rotate(<your degree here>)

.triangle {
    border-right: 10px solid; 
    border-bottom: 10px solid;
    height: 30px;
    width: 30px;
    transform: rotate(-45deg);
}
Run Code Online (Sandbox Code Playgroud)
<div class="triangle"></div>
Run Code Online (Sandbox Code Playgroud)


Rok*_*jan 37

响应箭头

他们调整你的自动文本,并彩色相同的颜色.即插即用:)
jsBin演示操场

在此输入图像描述

body{
  font-size: 25px; /* Change font and  see the magic! */
  color: #f07;     /* Change color and see the magic! */
} 

/* RESPONSIVE ARROWS */
[class^=arr-]{
  border:       solid currentColor;
  border-width: 0 .2em .2em 0;
  display:      inline-block;
  padding:      .20em;
}
.arr-right {transform:rotate(-45deg);  -webkit-transform:rotate(-45deg);}
.arr-left  {transform:rotate(135deg);  -webkit-transform:rotate(135deg);}
.arr-up    {transform:rotate(-135deg); -webkit-transform:rotate(-135deg);}
.arr-down  {transform:rotate(45deg);   -webkit-transform:rotate(45deg);}
Run Code Online (Sandbox Code Playgroud)
This is <i class="arr-right"></i> .arr-right<br>
This is <i class="arr-left"></i> .arr-left<br>
This is <i class="arr-up"></i> .arr-up<br>
This is <i class="arr-down"></i> .arr-down
Run Code Online (Sandbox Code Playgroud)


Dan*_*eld 14

这是一种不同的方法:

1)使用乘法字符:&times;×

2)隐藏一半 overflow:hidden

3)然后添加一个三角形作为提示的伪元素.

这里的优点是不需要变换.(它将在IE8 +中工作)

小提琴

.arrow {
  position: relative;
}
.arrow:before {
  content: '×';
  display: inline-block;
  position: absolute;
  font-size: 240px;
  font-weight: bold;
  font-family: verdana;
  width: 103px;
  height: 151px;
  overflow: hidden;
  line-height: 117px;
}
.arrow:after {
  content: '';
  display: inline-block;
  position: absolute;
  left: 101px;
  top: 51px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 25px 0 25px 24px;
  border-color: transparent transparent transparent black;
}
Run Code Online (Sandbox Code Playgroud)
<div class="arrow"></div>
Run Code Online (Sandbox Code Playgroud)

  • 适用于Internet Explorer,chrome和firefox. (2认同)

Gil*_*mbo 11

只需伪元素之前之后 使用- CSS

*{box-sizing: border-box; padding: 0; margin: 0}
:root{background: white; transition: background .3s ease-in-out}
:root:hover{background: red }
div{
  margin: 20px auto;
  width: 150px;
  height: 150px;
  position:relative
}

div:before, div:after{
  content: '';
  position: absolute;
  width: 75px;
  height: 20px;
  background: black;
  left: 40px
}

div:before{
  top: 45px;
  transform: rotateZ(45deg)
}

div:after{
  bottom: 45px;
  transform: rotateZ(-45deg)
}
Run Code Online (Sandbox Code Playgroud)
<div/>
Run Code Online (Sandbox Code Playgroud)


web*_*iki 8

另一种使用border而没有CSS3属性的方法:

div, div:after{
    border-width: 80px 0 80px 80px;
    border-color: transparent transparent transparent #000;
    border-style:solid;
    position:relative;
}
div:after{
    content:'';
    position:absolute;
    left:-115px; top:-80px;
    border-left-color:#fff;
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
Run Code Online (Sandbox Code Playgroud)