css 制作箭头
使用 CSS 制作箭头的方法
使用边框(Border)制作三角形箭头
通过设置元素的边框宽度和颜色,可以利用边框的交界处形成三角形箭头。
.arrow {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #000;
}
通过调整 border-left、border-right、border-top 或 border-bottom 的宽度和颜色,可以控制箭头的方向和大小。
使用伪元素(::before 或 ::after)制作箭头
伪元素可以用于在元素前后添加箭头,通常结合 transform 和 border 实现。
.element::after {
content: '';
position: absolute;
top: 50%;
right: -10px;
width: 0;
height: 0;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #000;
transform: translateY(-50%);
}
使用 SVG 内联绘制箭头
SVG 提供了更灵活的箭头绘制方式,适合复杂或自定义箭头形状。
.arrow-svg {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M7 10l5 5 5-5z' fill='%23000'/%3E%3C/svg%3E");
width: 24px;
height: 24px;
}
使用 clip-path 制作箭头
clip-path 可以通过裁剪元素形状实现箭头效果。
.arrow-clip {
width: 20px;
height: 20px;
background-color: #000;
clip-path: polygon(0% 50%, 50% 100%, 100% 50%, 50% 0%);
}
使用旋转和定位制作箭头
通过旋转矩形元素并定位,可以实现箭头效果。
.arrow-rotate {
width: 15px;
height: 3px;
background-color: #000;
position: relative;
}
.arrow-rotate::before {
content: '';
position: absolute;
width: 10px;
height: 3px;
background-color: #000;
transform: rotate(45deg);
right: -3px;
top: -3px;
}
.arrow-rotate::after {
content: '';
position: absolute;
width: 10px;
height: 3px;
background-color: #000;
transform: rotate(-45deg);
right: -3px;
top: 3px;
}
以上方法可以根据具体需求选择,边框法适合简单箭头,伪元素和 SVG 适合复杂或动态箭头,clip-path 和旋转法则提供更多自定义可能性。







