代码之家  ›  专栏  ›  技术社区  ›  LoreSchaeffer

带斜线阴影的CSS响应横幅

  •  3
  • LoreSchaeffer  · 技术社区  · 7 年前

    我试图用CSS为一个横幅创建一个背景,其中一面有颜色,另一面有一个45度的斜线。

    example

    我已经能够重新创建上面的图像,除了不停留在正确位置的投影。 任何建议都将不胜感激。

    这是我的代码:

    #container {
      height: 100px;
      width: 400px;
      overflow: hidden;
      background-color: #2962ff;
    }
    
    #triangle-topleft {
      width: 0;
      height: 0;
      border-top: 100px solid #2196f3;
      border-right: 400px solid transparent;
      -webkit-box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75);
      -moz-box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75);
      box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75);
    }
    <div id="container">
      <div id="triangle-topleft"></div>
    </div>
    3 回复  |  直到 7 年前
        1
  •  2
  •   MrMaavin    7 年前

    带边框的CSS三角形技巧不能用于此,因为阴影仍将应用于方框,而不仅仅应用于三角形。

    您必须创建一个伪元素,旋转它,然后对它应用阴影。

    #container {
      position: relative;
      height: 200px;
      width: 200px;
      overflow: hidden;
      background-color: grey;
    }
    
    #container:before { 
      content: '';
      position: absolute;
      left: 20%;
      width: 100%; 
      height: 200%; 
      background-color: rgb(255, 255, 255); /* fallback */
      background-color: rgba(255, 255, 255, 0.5);
      top: 0;
      -webkit-transform: rotate(45deg);
      -moz-transform: rotate(45deg);
      transform: rotate(45deg);
      box-shadow: inset 0 0 20px 10px #333;
    }
    <div id="container"></div>

    基本上,创建一个大于父级的矩形,然后旋转它并应用阴影。您可以根据需要调整颜色和旋转度

    演示: http://jsfiddle.net/b5TnZ/2032/

        2
  •  0
  •   Sumit Ridhal    7 年前

    可以在线性渐变中添加多个颜色停止点。使用双色设置。

    使用生成的渐变 Shapy

    .canvas {
        display: flex;
        height: 100vh;
        align-items: center;
        justify-content: center;
        min-height: 100%;
        min-width: 100%;
    }
    
    .gradient-canvas {
        max-height: 100%;
        max-width: 100%;
        width: 100%; 
        height: 100%;
        background: linear-gradient(127deg, rgb(31, 163, 209) 0%, rgb(31, 163, 209) 50%, rgb(25, 64, 208) 0%, rgb(46, 101, 223) 52%) 50% 50% / 100% 100% no-repeat;
    }
    <div class="canvas"><div class="gradient-canvas"></div></div>
        3
  •  0
  •   Temani Afif    7 年前

    您可以尝试如下渐变:

    #container {
      height: 150px;
      background:
        linear-gradient(135deg,#2962ff 49.8%,rgba(0,0,0,0.75) 50%, #2196f3 calc(50% + 10px));
      background-color:#2196f3;
    }
    <div id="container">
    </div>

    只需将DEG替换为 to bottom right 如果需要对角结果:

    #container {
      height: 150px;
      width:50%;
      background:
        linear-gradient(to bottom right,#2962ff 50%,rgba(0,0,0,0.75) 50%, #2196f3 calc(50% + 10px));
      background-color:#2196f3;
    }
    <div id=“container”>
    &L/DIV & GT;