I am trying to re-create the lightning bolt symbol from The Flash (DC Comics) (or the
Disclaimer: I recommend SVG for these but that doesn't mean we shouldn't be having fun with CSS. Use this for learning but not production implementation.
Here is a method to achieve the shape with just a single element (+ a couple of pseudos) and some background linear-gradients
. The shape can be scaled without any distortions.
Explanation on how the shape was achieved:
border-radius
on a pseudo-element (:after
). :before
) is added and is skewed along both X and Y axes to give the bolt's parts a skewed appearance.linear-gradients
on top of one another. It involves 6 gradient images where 3 are for the inside yellow part of the bolt and the other 3 are for the gray borders.Note: Scaling works pretty well if transform: scale(...)
is used instead of a height/width change + transition.
.lightning {
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
margin: 50px auto;
}
.lightning:after,
.lightning:before {
position: absolute;
content: '';
height: 100%;
width: 100%;
top: 0%;
left: 0%;
}
.lightning:after {
background: white;
border: 2px solid;
border-radius: 50%;
z-index: -1;
}
.lightning:before {
background: linear-gradient(transparent 0%, yellow 0%), linear-gradient(to top left, yellow 43%, gray 43%, gray 44%, transparent 44%), linear-gradient(to top left, transparent 56%, gray 56%, gray 57%, yellow 57%), linear-gradient(transparent 0%, gray 0%), linear-gradient(to top left, gray 51%, transparent 51%), linear-gradient(to top left, transparent 49%, gray 49%);
background-size: 20% 40%, 22% 42%, 22% 42%, 23% 42%, 23% 42%, 23% 42%;
background-position: 50% 50%, 32% 5%, 70% 100%, 50% 50%, 33% 7%, 69% 98%;
background-repeat: no-repeat;
backface-visibility: hidden;
transform: skewY(-30deg) skewX(-30deg);
z-index: 2;
}
/* Just for demo */
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
.lightning {
transition: all 1s;
}
.lightning:hover {
transform: scale(1.5);
}
With Animation for Bolt:
.lightning {
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
margin: 50px auto;
}
.lightning:after, .lightning:before {
position: absolute;
content: '';
height: 100%;
width: 100%;
top: 0%;
left: 0%;
}
.lightning:after {
background: white;
border: 2px solid;
border-radius: 50%;
z-index: -1;
}
.lightning:before {
background: linear-gradient(transparent 0%, yellow 0%), linear-gradient(to top left, yellow 43%, gray 43%, gray 44%, transparent 44%), linear-gradient(to top left, transparent 56%, gray 56%, gray 57%, yellow 57%), linear-gradient(transparent 0%, gray 0%), linear-gradient(to top left, gray 51%, transparent 51%), linear-gradient(to top left, transparent 49%, gray 49%);
background-size: 20% 40%, 22% 42%, 22% 42%, 23% 42%, 23% 42%, 23% 42%;
background-position: 50% 50%, 32% 5%, 70% 100%, 50% 50%, 33% 7%, 69% 98%;
background-repeat: no-repeat;
backface-visibility: hidden;
transform: skewY(-30deg) skewX(-30deg);
z-index: 2;
}
/* Just for demo */
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
.lightning {
transition: all 1s;
}
.lightning:hover:before {
animation: boltstrike 1s;
}
@-webkit-keyframes boltstrike {
25% {
transform: translateX(-5%) translateY(5%) skewY(-30deg) skewX(-30deg);
}
50% {
transform: translateX(7.5%) translateY(-7.5%) skewY(-30deg) skewX(-30deg);
}
100% {
transform: skewY(-30deg) skewX(-30deg);
}
}
@keyframes boltstrike {
25% {
transform: translateX(-5%) translateY(5%) skewY(-30deg) skewX(-30deg);
}
50% {
transform: translateX(5%) translateY(-5%) skewY(-30deg) skewX(-30deg);
}
100% {
transform: skewY(-30deg) skewX(-30deg);
}
}
Click here for a full demo with animation, color change on click etc.
Older version without border:
.lightning {
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
}
.lightning:after,
.lightning:before {
position: absolute;
content: '';
height: 100%;
width: 100%;
top: 0%;
left: 0%;
}
.lightning:after {
background: white;
border: 2px solid;
border-radius: 50%;
z-index: -1;
}
.lightning:before {
background: linear-gradient(transparent 0%, yellow 0%), linear-gradient(to top left, yellow 50%, transparent 50%), linear-gradient(to top left, transparent 50%, yellow 50%);
background-size: 20% 40%, 20% 40%, 20% 40%;
background-position: 50% 50%, 30% 5%, 70% 100%;
background-repeat: no-repeat;
backface-visibility: hidden;
transform: skewY(-30deg) skewX(-30deg);
z-index: 2;
}
/* Just for demo */
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
.lightning {
transition: all 1s;
}
.lightning:hover {
height: 250px;
width: 250px;
}