How to make 3-corner-rounded triangle in CSS

前端 未结 7 2545
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 03:34

I\'d like to achieve a custom-colored shape like this using no Javascript: \"3

Curren

7条回答
  •  囚心锁ツ
    2020-11-27 04:04

    Played around with Murray Smiths most upvoted version. Wrote it as a Stylus mixin and fixed some margin issues and added a direction option. The mixin also scales the triangle to somewhat pixelperfect size. Not tested very well. Use with care

    http://codepen.io/perlundgren/pen/VYGdwX

        triangle(direction = up, color = #333, size = 32px)
            position: relative
            background-color: color
            width:  2*(round(size/3.25))
            height: 2*(round(size/3.25))
            border-top-right-radius: 30%
            &:before,
            &:after
              content: ''
              position: absolute
              background-color: inherit
              width:  2*(round(size/3.25))
              height: 2*(round(size/3.25))
              border-top-right-radius: 30%
    
            if direction is up
              transform: rotate(-60deg) skewX(-30deg) scale(1,.866)
              margin: (@width/4) (@width/2.5) (@width/1.2) (@width/2.5)
    
            if direction is down
              transform: rotate(-120deg) skewX(-30deg) scale(1,.866)
              margin: 0 (@width/1.5) (@width/1.5) (@width/6)
    
            if direction is left
              transform: rotate(-30deg) skewX(-30deg) scale(1,.866)
              margin: (@width/5) 0 (@width) (@width/1.4)
    
            if direction is right
              transform: rotate(-90deg) skewX(-30deg) scale(1,.866)
              margin: (@width/5) (@width/1.4) (@width) 0
    
            &:before
              transform: rotate(-135deg) skewX(-45deg) scale(1.414,.707) translate(0,-50%)
            &:after
              transform: rotate(135deg) skewY(-45deg) scale(.707,1.414) translate(50%)
    

    and then just add the mixin to your class

        .triangle
          &.up
            triangle()
          &.down
            triangle(down)
          &.left
            triangle(left)
          &.right
            triangle(right)
    

提交回复
热议问题