Cut Corners using CSS

后端 未结 15 1155
悲&欢浪女
悲&欢浪女 2020-11-22 03:34

I\'m looking to \"cut\" the top left corner of a div, like if you had folded the corner of a page down.

I\'d like to do it in pure CSS, are there any methods?

相关标签:
15条回答
  • 2020-11-22 04:01

    You can use clip-path, as Stewartside and Sviatoslav Oleksiv mentioned. To make things easy, I created a sass mixin:

    @mixin cut-corners ($left-top, $right-top: 0px, $right-bottom: 0px, $left-bottom: 0px) {
      clip-path: polygon($left-top 0%, calc(100% - #{$right-top}) 0%, 100% $right-top, 100% calc(100% - #{$right-bottom}), calc(100% - #{$right-bottom}) 100%, $left-bottom 100%, 0% calc(100% - #{$left-bottom}), 0% $left-top);
    }
    
    .cut-corners {
      @include cut-corners(10px, 0, 25px, 50px);
    }
    
    
    0 讨论(0)
  • 2020-11-22 04:08

    by small modification of Joshep's code...You can use this code which seems like right corner folded down as per your requirement.

    div {
        height: 300px;
        background: red;
        position: relative;
    }
    
    div:before {
        content: '';
        position: absolute;
        top: 0; right: 0;
        border-top: 80px solid white;
        border-left: 80px solid blue;
        width: 0;
    }
    
    0 讨论(0)
  • 2020-11-22 04:08

    Here's a solution for if you don't want a solid-color background, i.e. just a border with square-cut corners.

    .container {
      width: 100px;
      height: 100px;
      background-color: white;
      border: 1px solid black;
      position: relative;
    }
    
    .border {
          position: absolute;
          width: 100%;
          height: 100%;
    }
    .border:before {
            content: '';
            position: absolute;
            border-top: 15px solid white;
            border-left: 15px solid white;
            width: 0;
          }
      
    .border:after {
            content: '';
            position: absolute;
            width: 16px;
            height: 1px;
            background: black;
          }
     
    .tl:before { top: -5px; left: -5px; transform: rotate(-45deg); }
    .tl:after { top: 5px; left: -3px; transform: rotate(-45deg);}
    
    .tr:before { top: -5px; right: -5px; transform: rotate(45deg); }
    .tr:after { top: 5px; right: -3px; transform: rotate(45deg); }
    
    .bl:before { bottom: -5px; left: -5px; transform: rotate(45deg); }
    .bl:after { bottom: 5px; left: -3px; transform: rotate(45deg); }
    
    .br:before { bottom: -5px; right: -5px; transform: rotate(-45deg); }
    .br:after { bottom: 5px; right: -3px; transform: rotate(-45deg); }
        
        
    <html>
      <body>
         <div class="container">
           <div class="border tl"></div>
           <div class="border tr"></div>
           <div class="border bl"></div>
           <div class="border br"></div>
         </div>
      </body>
    </html>

    0 讨论(0)
  • I recently cut off the top right corner and overlaid the tabs like folders. Complete code noob, so ignore the shitty code, but I did this by combining a square, a triangle, and a rectangle... This may or may not be a new approach, but hopefully, someone finds it helpful.

    https://i.stack.imgur.com/qFMRz.png

    Here is the HTML:

    <!DOCTYPE html>
    <html lang ="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" type="text/css" href="style.css"> 
        </head>
        <body>
            <div class="folders">
                <div class="container">
                    <div class="triangleOne">
                        <p class="folderNames">Home</p>
                    </div>
                    <div class="triangleOneCut">
                    </div>
                    <div class="triangleOneFill">
                    </div>
                </div>
    
                <div class="container2">
                    <div class="triangleOne blue">
                        <p class="folderNames">About</p>
                    </div>
                    <div class="triangleOneCut blueCut">
                    </div>
                    <div class="triangleOneFill blue">
                    </div>
                </div>
    
                <div class="container3">
                    <div class="triangleOne green">
                        <p class="folderNames">Contact</p>
                    </div>
                    <div class="triangleOneCut greenCut">
                    </div>
                    <div class="triangleOneFill green">
                    </div>
                </div>
            </div>
        </body>
    </html>
    

    Here is the CSS:

    .triangleOne {
        height: 50px;
        width: 40px;
        background: red;
        border-radius: 5px 0px 0px 5px;
        position: absolute;
    }
    
    .triangleOneCut {
        content: '';
        position: absolute;
        top: 0; left: 40px;
        border-top: 10px solid transparent;
        border-left: 10px solid red;
        width: 0;
    }
    
    .triangleOneFill {
        content: '';
        position: absolute;
        top: 10px; left: 40px;
        width: 10px;
        height: 40px;
        background-color: red;
        border-radius: 0px 0px 5px 0px;
    }
    
    .container {
        position: relative;
        height: 50px;
        width: 50px;
        display: inline-block;
        z-index: 3;
    }
    
    .container2 {
        position: relative;
        height: 50px;
        width: 50px;
        display: inline-block;
        left: -10px;
        z-index: 2;
    }
    
    .container3 {
        position: relative;
        height: 50px;
        width: 50px;
        display: inline-block;
        left: -20px;
        z-index: 1;
    }
    
    .blue {
        background-color: blue;
    }
    
    .green {
        background-color: green;
    }
    
    .blueCut {
        border-left: 10px solid blue;
    }
    
    .greenCut {
        border-left: 10px solid green;
    }
    
    .folders {
        width: 160px;
        height: 50px;
        /* border: 10px solid white; */
        margin: auto;
        padding-left: 25px;
        margin-top: 100px;
    }
    
    .folderNames {
        text-align: right;
        padding-left: 2px;
        color: white;
        margin-top: 1.5px;
        font-family: monospace;
        font-size: 6.5px;
        border-bottom: double 1.5px white;
    }
    
    0 讨论(0)
  • 2020-11-22 04:11

    If the parent element has a solid color background, you can use pseudo-elements to create the effect:

    div {
        height: 300px;
        background: red;
        position: relative;
    }
    
    div:before {
        content: '';
        position: absolute;
        top: 0; right: 0;
        border-top: 80px solid white;
        border-left: 80px solid red;
        width: 0;
    }
    <div></div>

    http://jsfiddle.net/2bZAW/


    P.S. The upcoming border-corner-shape is exactly what you're looking for. Too bad it might get cut out of the spec, and never make it into any browsers in the wild :(

    0 讨论(0)
  • 2020-11-22 04:14

    Here is another approach using CSS transform: skew(45deg) to produce the cut corner effect. The shape itself involves three elements (1 real and 2 pseudo-elements) as follows:

    • The main container div element has overflow: hidden and produces the left border.
    • The :before pseudo-element which is 20% the height of the parent container and has a skew transform applied to it. This element prodcues the border on the top and cut (slanted) border on the right side.
    • The :after pseudo-element which is 80% the height of the parent (basically, remaining height) and produces the bottom border, the remaining portion of the right border.

    The output produced is responsive, produces a transparent cut at the top and supports transparent backgrounds.

    div {
      position: relative;
      height: 100px;
      width: 200px;
      border-left: 2px solid beige;
      overflow: hidden;
    }
    div:after,
    div:before {
      position: absolute;
      content: '';
      width: calc(100% - 2px);
      left: 0px;
      z-index: -1;
    }
    div:before {
      height: 20%;
      top: 0px;
      border: 2px solid beige;
      border-width: 2px 3px 0px 0px;
      transform: skew(45deg);
      transform-origin: right bottom;
    }
    div:after {
      height: calc(80% - 4px);
      bottom: 0px;
      border: 2px solid beige;
      border-width: 0px 2px 2px 0px;
    }
    .filled:before, .filled:after {
      background-color: beige;
    }
    
    /* Just for demo */
    
    div {
      float: left;
      color: beige;
      padding: 10px;
      transition: all 1s;
      margin: 10px;
    }
    div:hover {
      height: 200px;
      width: 300px;
    }
    div.filled{
      color: black;
    }
    body{
     background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
    }
    <div class="cut-corner">Some content</div>
    <div class="cut-corner filled">Some content</div>


    The below is another method to produce the cut corner effect by using linear-gradient background images. A combination of 3 gradient images (given below) is used:

    • One linear gradient (angled towards bottom left) to produce the cut corner effect. This gradient has a fixed 25px x 25px size.
    • One linear gradient to provide a solid color to the left of the triangle that causes the cut effect. A gradient is used even though it produces a solid color because we can control size, position of background only when images or gradients are used. This gradient is positioned at -25px on X-axis (basically meaning it would end before the place where the cut is present).
    • Another gradient similar to the above which again produces a solid color but is positioned at 25px down on the Y-axis (again to leave out the cut area).

    The output produced is responsive, produces transparent cut and doesn't require any extra elements (real or pseudo). The drawback is that this approach would work only when the background (fill) is a solid color and it is very difficult to produce borders (but still possible as seen in the snippet).

    .cut-corner {
      height: 100px;
      width: 200px;
      background-image: linear-gradient(to bottom left, transparent 50%, beige 50%), linear-gradient(beige, beige), linear-gradient(beige, beige);
      background-size: 25px 25px, 100% 100%, 100% 100%;
      background-position: 100% 0%, -25px 0%, 100% 25px;
      background-repeat: no-repeat;
    }
    .filled {
      background-image: linear-gradient(black, black), linear-gradient(black, black), linear-gradient(black, black), linear-gradient(black, black), linear-gradient(to bottom left, transparent calc(50% - 1px), black calc(50% - 1px), black calc(50% + 1px), beige calc(50% + 1px)), linear-gradient(beige, beige), linear-gradient(beige, beige);
      background-size: 2px 100%, 2px 100%, 100% 2px, 100% 2px, 25px 25px, 100% 100%, 100% 100%;
      background-position: 0% 0%, 100% 25px, -25px 0%, 0px 100%, 100% 0%, -25px 0%, 100% 25px;
    }
    
    /* Just for demo */
    
    *{
      box-sizing: border-box;
      }
    div {
      float: left;
      color: black;
      padding: 10px;
      transition: all 1s;
      margin: 10px;
    }
    div:hover {
      height: 200px;
      width: 300px;
    }
    body{
     background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
    }
    <div class="cut-corner">Some content</div>
    <div class="cut-corner filled">Some content</div>

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