Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Shadow in a triangle

Tags:

html

css

I have this triangle in CSS:

.triangle {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 200px 200px 0 0;
  border-color: #007bff transparent transparent transparent;
}
<div class="triangle"></div>

How can I apply a 1px shadow on the hypotenuse line?

like image 701
Aral Roca Avatar asked Sep 11 '16 19:09

Aral Roca


People also ask

How do you put a shadow on a triangle in CSS?

Assuming we're cool with CSS3, one method would be to have a container box with hidden overflow and another box inside it which is rotate and hangs out of it. The part that is still visible would form a triangle. Then you can use a box-shadow on both the boxes to achieve a shadow all the way around.

Can you add shadow to an image in CSS?

Adding Shadows to ElementsYou can add CSS shadow to the element box or the text inside it. A common practice is setting horizontal and vertical properties to indicate the shadow position.

How many types of shadows are there in CSS?

With CSS3 you can create two types of shadows: text-shadow (adds shadow to text) and box-shadow (adds shadow to other elements).


3 Answers

Since box-shadow won't work, you have to apply a drop shadow filter on the triangle:

.triangle {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 200px 200px 0 0;
  border-color: #007bff transparent transparent transparent;
  -webkit-filter: drop-shadow(1px 1px 1px rgba(0,0,0,.5));
  filter: drop-shadow(1px 1px 1px rgba(0,0,0,.5));
}
<div class="triangle"></div>
like image 121
Anselm Avatar answered Oct 04 '22 11:10

Anselm


You can make this without using borders by employing an angled gradient.

div {
  width: 200px;
  height: 200px;
  margin: 2em auto;
  background: linear-gradient(to bottom right, #007bff 50%, rgba(0,0,0,.5) 50%, transparent 52%);
}
<div></div>
like image 22
Paulie_D Avatar answered Oct 04 '22 11:10

Paulie_D


Create a polygon in an svg, then apply a drop-shadow filter.

.triangle {
  width: 200px;
  color: #007bff;
  filter: drop-shadow(1px 1px 1px rgba(0, 0, 0, .5));
}
<svg class="triangle" viewBox="0 0 100 100">
  <polygon points="0,0 100,0 0,100" fill="currentColor" />
</svg>

Try to use semantic, meaningful HTML elements instead of relying purely on CSS for shapes.

like image 29
Ricky Avatar answered Oct 04 '22 11:10

Ricky