Creating a diagonal line/section/border with CSS

前端 未结 6 1784
离开以前
离开以前 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:57

    if you want to put diagonal border in column you can do it this way and its 100% responsive. your requirement might be different though. I put it transparent image which contains white diagonal border in that section's right column.

    <div id="wrapper">
      <div class="h-row">
        <div class="h-left">
        </div>
        <div class="h-right">
          <div class="hr-box"></div>
        </div>
      </div>
    </div>
    
    <style>
    .h-row{display: table; table-layout: fixed; height: 100%; width: 100%;}
    .h-left,
    .h-right{display: table-cell; vertical-align: top; height:250px;}/*height is just for demo purpose you can remove it ofcourse and fill the content */ 
    .h-left{background: #e9eae2; padding: 50px 83px 15px 165px; width: 69%;}
    .h-right{background: #7acec3 url('https://previews.dropbox.com/p/thumb/AAMv9WREPIx2AXUVhzCrK5Hl1jxf3ofX0teck9P94bG_SCjB28QPmKqXuchYyjp_xFMjMrGLzRYHh0O9wBOZJMZW9L_97lScKB22dgco9eGMJ1PCBbFepUcDkPg3aUE_1ONik2rKQ2SgRvtUgdq8nA_Ev1gxLxq8yWcXNKOdxKGBNOqe4FTHnbAgGy-JD4UtwZliw8c0fmNah8rydlD4JetFxNubkUyW4I_Q-XRL5qjW9A/p.png?size=1280x960&size_mode=3') no-repeat center center/ 100% 100%; padding: 50px 165px 15px 0; width: 31%; position: relative;}
    .h-left .row{margin:0 -44px;}
    </style>
    

    https://codepen.io/neel555nc/pen/LgjoOg

    0 讨论(0)
  • 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);
    }
    <div id="gradient"></div>

    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;
    }
    <div id="container">
      <div id="top-triangle"></div>
      <div id="bottom-triangle"></div>
    </div>

    0 讨论(0)
  • 2020-12-08 00:01

    With an svg, it is pretty simple :

    svg {
      display: block;
      width: 100%;
      height: 90px;
      background: yellow;
    }
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 10" preserveAspectRatio="none">
      <polygon points="100 0 100 10 0 10" />
    </svg>

    Note that I used the preserveAspectRatio="none" attribute so that the shape can have 100% width and keep 90px height

    And here with a monkey image :

    div {
      position: relative;
    }
    svg {
      display: block;
      width: 100%;
      height: 90px;
      background: yellow;
    }
    img {
      height: 50px;
      position: absolute;
      top: 0; bottom: 0;
      left: 0; right: 0;
      margin: auto;
      background: #fff;
      border-radius: 50%;
      padding: 10px;
    }
    <div>
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 10" preserveAspectRatio="none">
        <polygon points="100 0 100 10 0 10" />
      </svg>
      <img src="http://images.clipartpanda.com/monkey-clipart-black-and-white-16981-monkey-face-svg.svg" alt="" />
    </div>

    0 讨论(0)
  • 2020-12-08 00:04

    You can use CSS3 clip:

    .yellow {
      width: 100%;
      height: 90px;
      background: yellow;
      -webkit-clip-path: polygon(100% 0, 0 0, 0 100%);
      clip-path: polygon(100% 0, 0 0, 0 100%);
    }
    .black {
      width: 100%;
      height: 90px;
      background: black;
      -webkit-clip-path: polygon(100% 0, 0 100%, 100% 99%);
      clip-path: polygon(100% 0, 0 100%, 100% 99%);
      margin-top: -90px;
    }
    <div class="yellow"></div>
    <div class="black"></div>

    Demo: http://jsfiddle.net/zLkrfeev/2/

    It's not widely supported by the browsers: http://caniuse.com/#feat=css-clip-path

    0 讨论(0)
  • 2020-12-08 00:09

    Use a linear gradient

    div {
      height: 90px;
      background-image: linear-gradient(to bottom right, yellow, yellow 50%, black 50%, black);
    }
    <div></div>

    0 讨论(0)
  • 2020-12-08 00:20

    You can do this using a gradient.

    body {
      height: 200px;
      margin: 0 0 20px 0;
      background-color: transparent;
      background-size: 20px 20px;
      background-image:
         -webkit-repeating-linear-gradient(-45deg, black, black 1px, transparent 1px, transparent 14px);
      background-image:
         -moz-repeating-linear-gradient(-45deg, black, black 1px, transparent 1px, transparent 14px);
      background-image:
         -o-repeating-linear-gradient(-45deg, black, black 1px, transparent 1px, transparent 14px);
      background-image:
         repeating-linear-gradient(-45deg, black, black 1px, transparent 1px, transparent 14px);
    }
    

    JSFiddle

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