9

I am trying to create triangle shaped pointer/border on a horizontal line.

Here is an example of what I am trying to achieve:

Example

I tried manipulating the top border of a div, but everything I've done so far doesn't work at all.

misterManSam
  • 24,303
  • 11
  • 69
  • 89
Alexander Lomov
  • 119
  • 1
  • 5

4 Answers4

8

There are several ways to achieve that and it probably depends on your layout. One solution is to use a rotated element with borders on two sides.

.triangle {
  background: green;
  border: 2px solid white;
  border-width: 2px 2px 0 0;
  transform: rotate(-45deg);
}

.box {
  background: green;
  width: 400px;
  height: 80px;
  position: relative;
}

.line {
  height: 39px;
  border-bottom: 2px solid white;
}

.triangle {
  background: green;
  border: 2px solid white;
  border-width: 2px 2px 0 0;
  transform: rotate(-45deg);
  position: relative;
  left: 300px;
  top: 28px;
  width: 20px;
  height: 20px;
}
<div class="box">
  <div class="line">
    <div class="triangle">
      </div>
    </div>
  </div>
Paul
  • 8,974
  • 3
  • 28
  • 48
1

You can use :before and :after pseudo-elements:

body{
    background:black;
}
div{
    border-top:1px solid green;
    position:relative;
    margin-top: 100px;
}

div:before{
    content: '';
    position:absolute;
    left: calc(90% - 20px);
    top: -9px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 10px 10px 10px;
    border-color: transparent transparent black transparent;
    z-index: 10;
}

div:after{
    content: '';
    position:absolute;
    right:10%;
    top: -10px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 10px 10px 10px;
    border-color: transparent transparent green transparent;
}
<div></div>

Basiclly, there are two triangles, one is in the color of the background with larger z-index, and the other one is in the color of the border.

JSFiddle

Update

As @misterManSam mentioned, you can achieve it without using z-index - fiddle

Community
  • 1
  • 1
Vucko
  • 20,555
  • 10
  • 56
  • 107
  • This solution is also great. If I only can accept two answers Thank you very much! – Alexander Lomov Nov 10 '14 at 06:30
  • +1 for no extra, unnecessary markup. Keep the styling elements in the CSS like this answer does @AlexanderLomov :) – misterManSam Nov 11 '14 at 01:11
  • @Vucko - [Here is an alternative](http://jsbin.com/jihojo/1/edit?html,css,output) I made based on your answer. It more closely resembles the picture in the question and doesn't include calc() which is not really needed :) though it does rely on a transform. The z-index should not be needed at all. – misterManSam Nov 11 '14 at 01:35
  • @misterManSam your solution is also good, there's no need for adding extra markup :) – Vucko Nov 11 '14 at 06:27
  • @Vucko - I made it from your example, just tweaked it, so feel free to add it as an additional snippet in your answer so it doesn't get lost in the comments :) – misterManSam Nov 11 '14 at 06:45
  • 1
    @misterManSam I've updated my answer with you fiddle :) – Vucko Nov 11 '14 at 07:33
1

This can be achieved using a single div element.

Here's a quick demo:

html,body{background:green; height:100%;padding:0;margin:0;overflow:hidden;}
.line {
  margin-top: 50px;
  height: 5px;
  width: 80%;
  background: #fff;
  position: relative;
  box-sizing: border-box;
}
.line:after,
.line:before {
  content: "";
  position: absolute;
}
.line:after {
  left: calc(100% + 2px);
  height: 25px;
  width: 25px;
  top: -13px;
  border-top: 5px solid #fff;
  border-left: 5px solid #fff;
  transform: rotate(45deg);
}
.line:before {
  height: 100%;
  top: 0;
  left: calc(100% + 34px);
  width: 20%;
  background: inherit;
}
<div class="line"></div>
jbutler483
  • 24,074
  • 9
  • 92
  • 145
0

.box {
  margin-top: 100px;
  height: 50px;
  background: green;
  position: relative;
}

.box .line {
  position: absolute;
  top: 50%;
  left: 5px;
  right: 5px;
  margin-top: -1px;
  height: 2px;
  background: white;
}

.box .triangle {
  position: absolute;
  width: 0;
  height: 0;
  border-bottom: 8px solid white;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid transparent;
  top: 50%;
  right: 50px;
  margin-top: -15px;
}

.box .triangle .inner {
  position: absolute;
  top: -4px;
  left: -6px;
  width: 0;
  height: 0;
  border-bottom: 6px solid green;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid transparent;
}
<div class="box">
  <div class="line"></div>
  <div class="triangle"><div class="inner"></div></div>
</div>
dashtinejad
  • 6,193
  • 4
  • 28
  • 44