How can I make a 45 degree responsive ribbon with folded corner?

后端 未结 2 1039
情话喂你
情话喂你 2020-12-11 09:46

Is it possible to create css ribbon in corner shaped?

.

I\'ve tried with an png image, but is there any option to create using css ? should work with responsiv

相关标签:
2条回答
  • 2020-12-11 10:28

    You can try like below:

    .container {
      width: 200px;
      height: 150px;
      position: relative;
      display:inline-block;
      margin: 10px;
      background: lightblue;
    }
    
    .stack-top {
      /* adjust the below to control the shape */
      --d:5px; 
      --g:16px;
      --c:#333;
      /**/
    
      position: absolute;
      top: 0;
      right: 0;
      transform: translate(29.29%, -100%) rotate(45deg); /* 29.29% = 100%*(1 - cos(45deg)) */
      color: #fff;
      text-align: center;
      width: 100px;
      transform-origin: bottom left;
      padding:5px 0 calc(var(--d) + 5px);
      background:
        linear-gradient(135deg, transparent var(--g), var(--c) calc(var(--g) - 0.3px)) left,
        linear-gradient(-135deg,transparent var(--g), var(--c) calc(var(--g) - 0.3px)) right;
      background-size:51% 100%;
      background-repeat:no-repeat;
      clip-path:polygon(0 0,100% 0,100% 100%, calc(100% - var(--d)) calc(100% - var(--d)), var(--d) calc(100% - var(--d)),0 100%)
    }
    <div class="container">
      <div class="stack-top">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:0px;--g:19px;width:120px;--c:blue">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:8px;--g:17px;width:80px;--c:red">XX</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:10px;--g:20px;width:200px;--c:green">1Month</div>
    </div>

    Another adjustment to add a shadow effect to the folded part:

    .container {
      width: 200px;
      height: 150px;
      position: relative;
      display:inline-block;
      margin: 10px;
      background: lightblue;
    }
    
    .stack-top {
      /* adjust the below to control the shape */
      --d:5px; 
      --w:100px;
      --c:#333;
      /**/
    
      position: absolute;
      top: 0;
      right: 0;
      transform: translate(29.29%, -100%) rotate(45deg); /* 29.29% = 100%*(1 - cos(45deg)) */
      color: #fff;
      text-align: center;
      width: var(--w);
      transform-origin: bottom left;
      padding:5px 0 calc(var(--d) + 5px);
      background:
        linear-gradient(rgba(0,0,0,0.6) 0 0) bottom/100% var(--d) no-repeat
        var(--c);
      clip-path:polygon(0 100%,0 calc(100% - var(--d)),50% calc(100% - var(--d) - var(--w)/2),100% calc(100% - var(--d)),100% 100%,calc(100% - var(--d)) calc(100% - var(--d)), var(--d) calc(100% - var(--d)))
    }
    <div class="container">
      <div class="stack-top">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:0px;--w:120px;--c:pink">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:8px;--w:80px;--c:red">XX</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:12px;--w:200px;--c:green">1Month</div>
    </div>

    You can add position option:

    .container {
      width: 200px;
      height: 150px;
      position: relative;
      display:inline-block;
      margin: 10px;
      background: lightblue;
    }
    
    .stack-top {
      /* adjust the below to control the shape */
      --d:5px; 
      --w:100px;
      --c:#333;
      /**/
    
      position: absolute;
      top: 0;
      right: 0;
      transform: translate(29.29%, -100%) rotate(45deg); /* 29.29% = 100%*(1 - cos(45deg)) */
      color: #fff;
      text-align: center;
      width: var(--w);
      transform-origin: bottom left;
      padding:5px 0 calc(var(--d) + 5px);
      background:
        linear-gradient(rgba(0,0,0,0.6) 0 0) bottom/100% var(--d) no-repeat
        var(--c);
      clip-path:polygon(0 100%,0 calc(100% - var(--d)),50% calc(100% - var(--d) - var(--w)/2),100% calc(100% - var(--d)),100% 100%,calc(100% - var(--d)) calc(100% - var(--d)), var(--d) calc(100% - var(--d)))
    }
    
    .stack-top.left {
      left:0;
      right:auto;
      transform: translate(-29.29%, -100%) rotate(-45deg);
      transform-origin: bottom right;
    }
    <div class="container">
      <div class="stack-top">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top" style="--d:0px;--w:120px;--c:pink">1Month</div>
    </div>
    
    <div class="container">
      <div class="stack-top left" style="--d:8px;--w:80px;--c:red">XX</div>
    </div>
    
    <div class="container">
      <div class="stack-top left" style="--d:12px;--w:200px;--c:green">1Month</div>
    </div>

    0 讨论(0)
  • 2020-12-11 10:37

    Try using a Linear Gradient.

    To create a linear gradient, you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

    Syntax:

    background-image: linear-gradient(direction, color-stop1, color-stop2, ...)
    

    Source: W3Schools.com

    0 讨论(0)
提交回复
热议问题