如何将箭头添加到 div 元素?

7

我正在尝试为带箭头的 div 创建错误标签,但居中存在问题。

我得到的结果为:enter image description here

我需要像这样的效果:enter image description here

HTML:

<div class="error-label-arrow"></div>
<div class="error-label">This field is required.</div>

CSS:

div.error-label{
    padding: 7px 10px 0 4px;
    color: #fff;
    background-color: #DA362A;
    -webkit-border-radius: 5px; 
    -moz-border-radius: 5px; 
    border-radius: 5px;
}

div.error-label-arrow{
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent; 
    border-right:10px solid #DA362A;
    float: left;
}

这可行吗?

这里有一个示例代码


加一些填充?还是加一个示例,我会向你展示我的意思。 - Liam
1
你可以使用背景图片代替用CSS创建箭头,然后给div固定高度,vertical-align: center;应该就能起作用了。这种方法也适用于旧版浏览器。 - Shadow The Spring Wizard
3个回答

25
你可以使用CSS伪类来实现这个,因此你不需要一个“箭头”元素:

div.error-label {
  padding: 10px;
  color: #fff;
  background-color: #DA362A;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  width: 190px;
  margin: 30px;
}

div.error-label:before {
  content: ' ';
  height: 0;
  position: absolute;
  width: 0;
  left: 18px;
  border: 10px solid transparent;
  border-right-color: #DA362A;
}
<div class="error-label">This field is required.</div>


能否删除 margin:30px;,因为它会在周围区域产生大量的空白空间。 - Kin
@Kirix 我只是应用了 margin,这样你就可以在屏幕上看到箭头。注意:对填充/边距的更改将需要更改伪类中的 left - Curtis

3

这是解决方案

div.error-label{
    padding: 7px 10px 0 4px;
    color: #fff;
    background-color: #DA362A;
    -webkit-border-radius: 5px; 
    -moz-border-radius: 5px; 
    border-radius: 5px;
    width: 150px;
    margin-left: 5px;
}

div.error-label-arrow{
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent; 
    border-right:10px solid #DA362A;
    float: left;
    height: 1px;
    margin-top: 3px;
}
<div class="error-label-arrow"></div>
<div class="error-label">This field is required.</div>


2
您可以通过使用伪元素来实现此目的,请参考下面的答案。

.error{
   position: relative;
   background: red;
   padding: 10px;
   color: #fff;
   margin:0 0 0 12px;
   width:auto;
   display:inline-block;
 
 }

.error::before {
 content: "";
 width: 0;
 height: 0;
 border-style: solid;
 border-width: 9px 11px 9px 0;
 border-color: transparent red transparent transparent;
 position: absolute;
 left: -11px;
 top: 0;
 bottom: 0;
 margin: auto;
}

.arrow_type2 {
 position: relative;
 background: #28d57f;
 border: 3px solid #0cf5a7;
  padding:50px 20px;
  margin-left:20px;
  margin-top:20px;
  width:auto;
  display:inline-block;
}
.arrow_type2:after, .arrow_type2:before {
 right: 100%;
 top: 50%;
 border: solid transparent;
 content: " ";
 height: 0;
 width: 0;
 position: absolute;
 pointer-events: none;
}

.arrow_type2:after {
 border-color: rgba(40, 213, 127, 0);
 border-right-color: #28d57f;
 border-width: 10px;
 margin-top: -10px;
}
.arrow_type2:before {
 border-color: rgba(12, 245, 167, 0);
 border-right-color: #0cf5a7;
 border-width: 14px;
 margin-top: -14px;
}
<div class="error">This is field required</div>


<div class="arrow_type2">Arrow type two</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接