Creating a triangle in css with a gradient background

后端 未结 3 1184
闹比i
闹比i 2021-01-05 13:24

I am trying to create a triangle in css with a gradient background. I have not had any success as yet. Is there way to do this to bring off this effect seen in the image bel

3条回答
  •  臣服心动
    2021-01-05 13:44

    Creating triangles (or other shapes - pentagons, hexagons, octagons, decagons, dodecagons, tetradecagons, octadecagons and so on) with a gradient (or any other kind of image background) is really easy with CSS transforms.

    But in this case you don't even need a triangle. You just need to rotate a square pseudo-element by 45deg and apply the gradient on that from corner to corner.

    demo

    CSS:

    .warn {
      position: relative;
      margin: 0 auto;
      border: solid 1px darkred;
      width: 12em; height: 3em;
      border-radius: .2em;
      background: linear-gradient(lightcoral, firebrick);
    }
    .warn:before {
      position: absolute;
      top: 50%; left: 0;
      margin: -.35em -.45em;
      border-left: inherit; border-bottom: inherit;
      /* pick width & height such that 
         the diagonal of the square is 1em = 1/3 the height of the warn bubble */
      width: .7em; height: .7em;
      border-radius: 0 0 0 .2em;
      transform: rotate(45deg);
      background: linear-gradient(-45deg, firebrick -100%, lightcoral 200%);
      content: '';
    }
    

提交回复
热议问题