如何将箭头添加到div?

时间:2013-02-11 09:02:46

标签: html css css-shapes

我正在尝试使用箭头为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;
}

有可能吗?

以下是fiddle

3 个答案:

答案 0 :(得分:16)

您可以使用CSS Pseudo classes执行此操作,因此您不需要“箭头”元素:

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:10px;
    border: 10px solid transparent;
    border-right-color: #DA362A;
}

现场演示http://jsfiddle.net/p9ZPg/

答案 1 :(得分:2)

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;
}

以下是fiddle

答案 2 :(得分:2)

  

您可以通过在答案下方使用tick_broadcast_device检查来实现此目的

Pseudo elements
.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;
}