I want to achieve the effect in the picture, it’s a button with hover effect. Before the hover, the button must have a triangle on the left like the above text in the picture, after the hover the triangle multiply to cover the hole button from left to right, the triangle must alternate like the button below in the picture.
this is my attempt of code
.triangle-btn {
position: relative;
display: inline-block;
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
outline: none;
cursor: pointer;
}
.triangle-btn:before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
border-top: 25px solid transparent;
border-right: 50px solid #007bff;
border-bottom: 25px solid transparent;
}
.triangle-btn:hover:before {
width: 100%;
transition: all 0.3s ease;
}
.triangle-btn:after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border-top: 25px solid transparent;
border-left: 50px solid #007bff;
border-bottom: 25px solid transparent;
}
.triangle-btn:hover:after {
width: 100%;
transition: all 0.3s ease;
}
<button class="triangle-btn">Hover Me!</button>
3
Answers
This is my solution so far if anyone interested
To create a button hover effect with triangles in the background, you can use CSS to style the button and add a pseudo-element to create the triangles. Here’s an example:
After spending too much time on this, I came up with the following that allows you to set:
--expandedTriangleCount
,background-color
via the--shapeColor
custom property, or to set a linear-gradient – across all triangles – if--shapeColor2
is set (if it’s unset then alinear-gradient
is still used, but it transitions from--shapeColor
to--shapeColor
,--shapeStrokeColor
; this is the color that separates the various triangles from their neighbours,--shapeStrokeWidth
--vertexSize
; the height of the triangles are calculated in CSS (with a hardcoded value of the square-root of 3, assqrt()
is yet to be implemented outside of Safari (at the time of writing)).JS Fiddle demo.
References:
background-image
.block-size
.box-sizing
.clip-path()
.color
.display
.::first-letter
pseudo-element.font-size
.:hover
pseudo-class.inline-size
.margin
.margin-block
.margin-inline
.min-block-size
.padding
.padding-block
.padding-inline
.place-content
.polygon()
.repeating-linear-gradient()
.sqrt()
.text-align
.transition
.var()
.