代码之家  ›  专栏  ›  技术社区  ›  Emad Dehnavi

底部有锯齿形边框的容器仅适用于边框

  •  2
  • Emad Dehnavi  · 技术社区  · 5 年前

    我试着做一个底部有锯齿状边缘的容器,如下所示:

    enter image description here

    我试过这个,但我不知道怎么得到那个底部灰色背景的突袭,我只希望边界像图像一样是灰色的,有人能帮忙吗?:

    https://jsfiddle.net/umw8yh21/1/

    HTML:

    <div class="myform">
       <div class="myformMain">Content</div>
       <div class="myformFooter"></div>
    </div>
    

    CSS:

    .myform{
          border: 4px solid lightgrey;
        border-bottom: none;
    }
    .myformMain {
      height: 200px;
        padding: 36px 0;
        box-sizing: border-box;
        background-color: white;
    }
    .myformFooter:after{
    content: " ";
        display: block;
        position: relative;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 36px;
        background: linear-gradient(white 0%, transparent 0%), linear-gradient(135deg, #272220 33.33%, transparent 33.33%) 0 0%, #272220 linear-gradient(45deg, #272220 33.33%, white 33.33%) 0 0%;
        background: -webkit-linear-gradient(white 0%, transparent 0%), -webkit-linear-gradient(135deg, #d9d9d9 33.33%, transparent 33.33%) 0 0%, #d9d9d9 -webkit-linear-gradient(45deg, #d9d9d9 33.33%, white 33.33%) 0 0%;
        background: -o-linear-gradient(white 0%, transparent 0%), -o-linear-gradient(135deg, #272220 33.33%, transparent 33.33%) 0 0%, #272220 -o-linear-gradient(45deg, #272220 33.33%, white 33.33%) 0 0%;
        background: -moz-linear-gradient(white 0%, transparent 0%), -moz-linear-gradient(135deg, #272220 33.33%, transparent 33.33%) 0 0%, #272220 -moz-linear-gradient(45deg, #272220 33.33%, white 33.33%) 0 0%;
        background: -ms-linear-gradient(white 0%, transparent 0%), -ms-linear-gradient(135deg, #272220 33.33%, transparent 33.33%) 0 0%, #272220 -ms-linear-gradient(45deg, #272220 33.33%, white 33.33%) 0 0%;
        background-repeat: repeat-x;
        background-size: 0px 47%, 14px 41px, 14px 42px
    }
    

    编辑:其他类似的答案并不是我想要的,我已经检查过了,我需要一种方法使相同大小的边框成为锯齿形,不使用任何svg/png或纹理,只使用css。

    2 回复  |  直到 5 年前
        1
  •  2
  •   Fabrizio Calderan    5 年前

    你可以使用 SVG 作为底部重复的背景 non-scaling-stroke 属性集

        div {
          width: 50%;
          height: 180px;
          border: 4px #ededed solid;
          border-bottom: 0;
          background-image: url('data:image/svg+xml;utf8, <svg viewBox="0 0 200 110" xmlns="http://www.w3.org/2000/svg"><path d="M -15 110 L100 10 L215 110" fill="none" stroke="#ededed" stroke-width="4" vector-effect="non-scaling-stroke"/></svg>');
          background-position: bottom left;
          background-size: 10% auto;
          background-repeat: repeat-x;
        }
    <div></div>

    只需为边框宽度和 stroke-width 的属性 path .

    如果需要用文本填充此元素,请记住在底部添加一些空间(例如,使用 padding-bottom )所以内容不会重叠。

    同样值得注意的是 vector-effect="non-scaling-stroke" 将阻止缩放路径,以便可以无缝地将此背景应用于响应元素(否则,正常边框将保持固定宽度,而 静止无功发生器 路径将缩放),例如

    Codepen demo

    enter image description here

    此外,如果您愿意,还可以通过更改 background-size 在某些给定的MediaQuery上,例如

    @media all and (min-width: 800px) {
      /* 12 background repetitions */
      div { background-size: calc(100% / 12) auto }
    }
    
    @media all and (min-width: 1200px) {
      /* 18 background repetitions */
      div { background-size: calc(100% / 18) auto }
    }
    
        2
  •  0
  •   Temani Afif    5 年前

    您可以尝试如下操作:

    .container {
      height:150px;
      width:320px;
      border:3px solid grey;
      border-bottom:none;
      background:
        linear-gradient(135deg,transparent 50%,grey 50%,grey calc(50% + 3px),transparent 0) -13px 100%,
        linear-gradient(45deg,transparent 50%, grey 50%,grey calc(50% + 3px),transparent 0) 6px 100%;
      background-size:40px 20px;
      background-repeat:repeat-x;
    }
    
    body {
     background:pink;
    }
    <div class="container">
    </div>