Search code examples
htmlcsscss-shapes

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


Solution

  • 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>