Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a triangle between two divs? CSS

Tags:

html

css

I would like to create a triangle like the one below: CSS to recreate

however, I'm not quite sure how to get the triangle between the divs.

Here's the code I currently have:

.container {
  display: flex;
  flex-direction: row;
  align-content: center;
}
.box {
  height: 50px;
  width: 50%;
  background-color: #ddd;
  border: 1px solid red;
}
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
</div>
like image 940
JacobDClark Avatar asked Apr 11 '26 00:04

JacobDClark


2 Answers

There must be better ways, but I usually do something along these lines:

.container {
  display: flex;
  flex-direction: row;
  align-content: center;
}

.box {
  height: 50px;
  width: 50%;
  background-color: #ddd;
  border: 1px solid red;
}

.triangle {
  position: relative;
}

.triangle::before {
  content: "";
  width: 10px;
  height: 10px;
  border-top: 2px solid red;
  border-right: 2px solid red;
  background: #ddd;
  transform: rotate(45deg);
  position: absolute;
  top: 50%;
  right: -7px;
  margin-top: -5px;
}
<div class="container">
  <div class="box triangle"></div>
  <div class="box"></div>
</div>
like image 139
LSE Avatar answered Apr 12 '26 13:04

LSE


HTML:

  <div class="box"> 
  <div class='arrow-right'></div>
  </div>
  
   <div class="box"> 
  <div class='arrow-right'></div>
  </div>
  
</div>

CSS:

.container {
  display: flex;
  flex-direction: row;
  align-content: center;
}
.box {
  height: 50px;
  width: 50%;
  background-color: #ddd;
  border: 1px solid red;
  position:relative;
}
.arrow-right{
  position:absolute;
 right:-.6rem;
 z-index:2;
 top:50%;
 transform:translateY(-50%);
     width: 0.6rem;
    height: 1rem;
    
}
 .arrow-right::after{
    z-index:3;
    position: absolute;
    display: block;
    content: "";
    border-color: transparent;
    border-style: solid;
    border-width: 0.6rem 0 0.6rem 0.6rem;
    right: 2px;
    border-left-color: #ddd;
     overflow:hidden;
 }
 .arrow-right::before{
    right: 0;
    z-index:3;
    position: absolute;
    display: block;
    content: "";
    border-color: transparent;
    border-style: solid;
    border-width: 0.6rem 0 0.6rem 0.6rem;
    border-left-color: red;     
 }

jsfiddle

like image 31
Todor Markov Avatar answered Apr 12 '26 13:04

Todor Markov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!