Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add arrow to div?

I am trying to create error label for div with arrow, but have problem with centering.

I get: enter image description here

I need something like: 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;
}

Is it possible?

Here is the fiddle

like image 558
Kin Avatar asked Feb 11 '13 09:02

Kin


2 Answers

You can use CSS Pseudo classes to do this, therefore you won't need an "arrow" element:

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>
like image 53
Curtis Avatar answered Nov 11 '22 10:11

Curtis


Here is the solution

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>
like image 44
Hary Avatar answered Nov 11 '22 11:11

Hary