Add border over triangle element
Asked Answered
M

3

7

I am trying to create a border around a triangle. I have this so far:

JSFiddle

.myDiv {
  width: 300px;
  padding: 15px;
  text-align: right;
  background-color: lightblue;
  position: relative;
  border: 1px solid black;
}
.myDiv::before {
  content: "";
  position: absolute;
  bottom: -20px;
  right: 20px;
  border-right: 20px solid lightblue;
  border-bottom: 20px solid transparent;
}
<div class="myDiv">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco</div>

But I can't add a border to the before element. How can I add a border around the piece that's sticking out on the bottom ('before` element)?

(I saw this question, but I can't apply the same principle to this, since it's different shapes.)

Milner answered 26/10, 2016 at 23:4 Comment(1)
Make a ::after that’s 2px wider, 1px taller, and 1px to the left. Then, set the border-color to black.Theatrician
S
6

Try to add an ::after with more border-width and different position bottom and right, its work very well. Don't forget to change border-color to black and low down the z-index by -1.

Example:


.myDiv {
  width: 300px;
  padding: 15px;
  text-align: right;
  background-color: lightblue;
  position: relative;
  border: 1px solid black;
}

.myDiv::before {
  content: "";
  position: absolute;
  bottom: -20px;
  right: 20px;
  border-right: 20px solid lightblue;
  border-bottom: 20px solid transparent;
}

.myDiv::after {
  z-index:-1;
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  bottom: -22px;
  right: 19px;
  border-right: 21px solid black;
  border-bottom: 21px solid transparent;
}
<div class="myDiv">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco</div>

Fiddle demo

Scherer answered 26/10, 2016 at 23:17 Comment(1)
This is a pretty sneaky idea. I never thought of doing that.Overdo
W
0

Add an :after element positioned behind the blue triangle.

.myDiv::after {
  content: "";
  position: absolute;
  bottom: -22px;
  right: 19px;
  border-right: 21px solid black;
  border-bottom: 21px solid transparent;
  z-index: 9;
}

.myDiv {
  width: 300px;
  padding: 15px;
  text-align: right;
  background-color: lightblue;
  position: relative;
  border: 1px solid black;
}
.myDiv::before {
  content: "";
  position: absolute;
  bottom: -20px;
  right: 20px;
  border-right: 20px solid lightblue;
  border-bottom: 20px solid transparent;
  z-index:10;
}

.myDiv::after {
  content: "";
  position: absolute;
  bottom: -22px;
  right: 19px;
  border-right: 21px solid black;
  border-bottom: 21px solid transparent;
  z-index: 9;
}
<div class="myDiv">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco</div>
Winterize answered 26/10, 2016 at 23:32 Comment(0)
H
0

You can use an SVG for the shape as well.

You create the triangle with the polygon tag:

<polygon 
class="triangle" /* CSS Styling, if you want properties instead of inline attributes */
fill="currentColor" /* To be able to change the fill with the property color in CSS */
stroke="#000" /* Match your border color */
stroke-dasharray="0, 30, 90" /* To remove the stroke from the "top" part of triangle */
points="30,4 60,4 60,30" /> /* The whole triangle figure */

Code Snippet:

.myDiv {
  width: 300px;
  padding: 15px;
  text-align: right;
  background-color: lightblue;
  position: relative;
  border: 1px solid black;
}
.myDiv svg {
  position: absolute;
  right: 20px;
  bottom: -59.7px;
  color: lightblue;
}
<div class="myDiv">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
  <svg height="64" width="64" viewBox="0 0 64 64">
    <polygon class="triangle" fill="currentColor" stroke="#000" stroke-dasharray="0, 30, 90" points="30,4 60,4 60,30" />
  </svg>
</div>
Hildy answered 26/10, 2016 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.