Creating a diagonal line/section/border with CSS

前端 未结 6 1816
离开以前
离开以前 2020-12-07 23:34

I am trying to create a diagonal line on a webpage, to act as a section/section break. This is essentially a split colour section. I cant use an image as if the page gets en

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 23:59

    You can do this without any clipping and just using borders in a unique way. This should also be cross-browser compatible, but I haven't tested it across everything

    Initially divided this into 2 separate divs / triangles and joined them, but thanks to web-tiki and kaiido perfected it to use only 1 div and minimal CSS

    *{
      border: 0;
      padding: 0;
      margin: 0;
    }
    
    #gradient {
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 90px 100vw 0 0;
      border-color: yellow black transparent transparent;
      transform: scale(1.0001);
    }

    Original Answer using multiple divs:

    *{
      border: 0;
      padding: 0;
      margin: 0;
    }
    
    #container {
      width: 100%;
      position: relative;
    }
    
    #container div {
      position: absolute;
    }
    
    #top-triangle {
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 90px 100vw 0 0;
      border-color: yellow transparent transparent transparent;
    }
    
    #bottom-triangle {
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 0 0 90px 100vw;
      border-color: transparent transparent black transparent;
    }

提交回复
热议问题