Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a pointy arrow with a div in CSS

How can I make a pointy arrow in CSS? Not just a triangle but one with a stem, like a traditional arrow that would be fired from a bow?

I'm trying to do it by creating a div container, containing two containers, left and right. The right will contain the triangle, and the left will contain three divs, the centre of which will be colored to create the stem.

Here's my code:

HTML:

<div id="main">
    <div class='arrowblock'>
        <div class='arrowright'></div>
        <div class='rectcontainer'>
            <div class='rect'></div>
            <div class='rect' style='background-color:green'>
            </div><div class='rect'>
            </div>
</div>

CSS:

.rectcontainer {
    height:30px;
    width:100px;
}

.arrowblock {
    width:130px;
    border-top: 15px solid transparent;
}
.arrowright {
float:right;
    border-top: 15px solid transparent;
    border-bottom: 15px solid transparent;
    border-left: 30px solid green;
}
.rect {
    width:100px;
    height:10px;
    background-color:transparent;
}

Is there a simpler way to achieve this?

like image 311
Dicky Moore Avatar asked Sep 15 '13 19:09

Dicky Moore


2 Answers

To build on @josh-crozier's answer, you can eliminate a lot of the HTML by using pseudo-elements instead:

jsFiddle

HTML:

<div class="arrow"></div>

CSS:

.arrow {
    position:relative;
    width:120px;
    margin:50px auto;
    height:0;
    border-bottom:10px solid blue;
}

.arrow::after { 
    content: '';
    width: 0; 
    height: 0; 
    border-top: 20px solid transparent;
    border-bottom: 20px solid transparent;
    border-left: 30px solid blue;
    position: absolute;
    right: -10px;
    top: -15px;
}

To add an arrow at the start of the line using the ::before element is trivial also: jsFiddle

like image 128
Codemonkey Avatar answered Sep 20 '22 20:09

Codemonkey


How about just using a html entity or unicode symbol for your arrow:

<div>&#8594;</div>

<div title="U+21A3: RIGHTWARDS ARROW WITH TAIL">↣</div>

div{
    font-size: 40px;
}

FIDDLE

There are more to choose from here

like image 31
Danield Avatar answered Sep 17 '22 20:09

Danield