How to make this arrow in CSS

Asked

Viewed 2,486 times

13

I have this next arrow in a layout, but I don’t want to make an image, but only with CSS. How do I do? I have to wear edges?

inserir a descrição da imagem aqui

4 answers

18


You can do it with CSS with two shapes, a rectangle and a triangle. And so you can put the size you want of the arrow.

 #triangle-right 
 { 
     width: 0;
     height: 0; 
     border-top: 50px solid transparent;    /* tamanho do topo ao meio */
     border-left: 75px solid red;           /* comprimeto da seta      */
     border-bottom: 50px solid transparent; /* tamanho do meio a baixo */
     margin-left:100px;                     /* comprimento da cauda    */
 }
 #rectangle 
 { 
     width: 100px;                          /* comprimento da cauda */
     height: 50px;                          /* largura da cauda     */
     background: red; 
     position: absolute; 
     margin-top:25px;                       /* metade da largura para ficar centrado*/
 }
<div>
  <div id="rectangle"></div>
  <div id="triangle-right"></div>
</div>

Source of forms

  • 2

    @Felipestoker, if you want the arrow exactly the way you have it, this is the best solution. Bacco’s however, is not affected by css’s if you want to apply much by setting a background image. + 1

  • 2

    Example like you asked http://jsfiddle.net/wwjpwd3m/1/

  • 1

    You guys are 10! That’s exactly what I wanted!

  • @Jorgeb. Is there a way I put a border? I saw that if I put it, it leaves square.

  • You want an edge around the whole arrow?

11

No image, no HTML change:

Using borders you can improvise with CSS only, without needing any image:

.flecha::after {
  display:block;
  position:absolute;
  content:'';
  left:-26px;
  top:-3px;
  width: 20px;
  height: 6px;
  background:black;
}

.flecha {
  position:relative;
  margin-left:20px;
  width: 0; 
  height: 0; 
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 6px solid black;
}
<div class="flecha"></div>

Edit: the logic is similar to @Jorge B’s response, but does not need changes to the HTML by using pseudo-elements instead of two Divs.


Alternative: image embedded in CSS


If the problem of having an image is the external resource, instead of linking the image, you can use a data URI:

.flecha {
   width: 26px;
   height: 12px;
   background-repeat: no-repeat;
   background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAMCAYAAAB8xa1IAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZS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L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/Pjn3yM0AAAElSURBVHjatFQ7boNAEB1g+SMhkGUJqtQpkjrXyB1cWEi+gbsU6aMUlnwb+wT2BXwIpCBYlrxZeR0rpYGRRsuMYN6b4e1YURSRUoosy6JHzfM8/f0wDBw++b5/MfU4xy5oAmuaRpNl67ruE+S9LMve+7434OS4rquDMR3Ztk2O4+gTtZ7btl3jlHEcH5iA6WjBgHD1KJBhzWQB5nMndV1/IA7SNN0CWLP54XeRnMy5nvEkSbZlWRLPC3Dk0owWhuHOBgNJMxtGu6T/rU7tUOB3URQkgiB4hTIEANVY5kwa8l5JKSuOAbKHzCvkSEAMJ6OYiexyBfnK83zDIFre5uaO/Ae3C4tu3jClIzrZ4PlP+mNWkCkihLhfQS9Q2RmTGu5X0K8AAwD5DrixhRb2XAAAAABJRU5ErkJggg==);
}
<div class="flecha"></div>

  • what is that background-image?

  • is the image you use at the bottom, in this case, of your div arrow.

11

You can use a Unicode HTML (decimal or hexadecimal) to make the arrow, and be able to adapt the font size, colors, etc with css:

    <p style="font-size:40px;">&rarr;<p>
    <p style="font-size:20px; color:red;">&#8594<p>

Example LINK

For more examples, you can see through w3schools

  • In my browser (Chrome/OSX) the characters are not displayed. But when I copied and pasted in a text editor, they appeared. ???

  • @bfavaretto, maybe it’s platform problem, I don’t know. In windows environment works in the 3 browsers, I find it very strange that this happens to you

3

Another alternative to this, even if it’s not pure CSS, is to use webfonts icons like http://typicons.com/ They are completely customizable in CSS (color, size, opacity, etc.).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.