Create a lightning bolt design (like The Flash)

后端 未结 9 604
陌清茗
陌清茗 2021-01-29 23:44

\"T-shirt

I am trying to re-create the lightning bolt symbol from The Flash (DC Comics) (or the

9条回答
  •  后悔当初
    2021-01-30 00:39

    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:

    • The white circle with the black border is a normal CSS circle created using border-radius on a pseudo-element (:after).
    • Another pseudo-element (:before) is added and is skewed along both X and Y axes to give the bolt's parts a skewed appearance.
    • The bolt is actually created by stacking multiple 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.
    • The size of the background images (gradients) are determined by the size of the bolt and each of them is positioned in such a way that they produce the lightning bolt like appearance.
    • The center part of the bolt actually has only one solid color but is still produced using a gradient because we can't control the size of solid color backgrounds.

    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;
    }
    
    
    
    


提交回复
热议问题