Cut Corners using CSS

后端 未结 15 1179
悲&欢浪女
悲&欢浪女 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 03:49

    If you need a diagonal border instead of a diagonal corner, you can stack 2 divs with each a pseudo element:

    DEMO

    http://codepen.io/remcokalf/pen/BNxLMJ

    .container {
      padding: 100px 200px;
      overflow: hidden;
    }
    
    div.diagonal {
      background: #da1d00;
      color: #fff;
      font-family: Arial, Helvetica, sans-serif;
      width: 300px;
      height: 300px;
      padding: 70px;
      position: relative;
      margin: 30px;
      float: left;
    }
    
    div.diagonal2 {
      background: #da1d00;
      color: #fff;
      font-family: Arial, Helvetica, sans-serif;
      width: 300px;
      height: 300px;
      padding: 70px;
      position: relative;
      margin: 30px;
      background: #da1d00 url(http://www.remcokalf.nl/background.jpg) left top;
      background-size: cover;
      float: left;
    }
    
    div.diagonal3 {
      background: #da1d00;
      color: #da1d00;
      font-family: Arial, Helvetica, sans-serif;
      width: 432px;
      height: 432px;
      padding: 4px;
      position: relative;
      margin: 30px;
      float: left;
    }
    
    div.inside {
      background: #fff;
      color: #da1d00;
      font-family: Arial, Helvetica, sans-serif;
      width: 292px;
      height: 292px;
      padding: 70px;
      position: relative;
    }
    
    div.diagonal:before,
    div.diagonal2:before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      border-top: 80px solid #fff;
      border-right: 80px solid transparent;
      width: 0;
    }
    
    div.diagonal3:before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      border-top: 80px solid #da1d00;
      border-right: 80px solid transparent;
      width: 0;
      z-index: 1;
    }
    
    div.inside:before {
      content: '';
      position: absolute;
      top: -4px;
      left: -4px;
      border-top: 74px solid #fff;
      border-right: 74px solid transparent;
      width: 0;
      z-index: 2;
    }
    
    h2 {
      font-size: 30px;
      line-height: 1.3em;
      margin-bottom: 1em;
      position: relative;
      z-index: 1000;
    }
    
    p {
      font-size: 16px;
      line-height: 1.6em;
      margin-bottom: 1.8em;
    }
    
    #grey {
      width: 100%;
      height: 400px;
      background: #ccc;
      position: relative;
      margin-top: 100px;
    }
    
    #grey:before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      border-top: 80px solid #fff;
      border-right: 80px solid #ccc;
      width: 400px;
    }
    <div id="grey"></div>
    <div class="container">
      <div class="diagonal">
        <h2>Header title</h2>
        <p>Yes a CSS diagonal corner is possible</p>
      </div>
      <div class="diagonal2">
        <h2>Header title</h2>
        <p>Yes a CSS diagonal corner with background image is possible</p>
      </div>
      <div class="diagonal3">
        <div class="inside">
          <h2>Header title</h2>
          <p>Yes a CSS diagonal border is even possible with an extra div</p>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-22 03:50

    You could use linear-gradient. Let's say the parent div had a background image, and you needed a div to sit on top of that with a gray background and a dog-eared left corner. You could do something like this:

    .parent-div { background: url('/image.jpg'); }
    .child-div { 
       background: #333;
       background: linear-gradient(135deg, transparent 30px, #333 0);
    }
    

    See it on CodePen

    Further reading:

    • CSS Gradients on CSS-Tricks
    • Beveled corners & negative border-radius with CSS3 gradients
    0 讨论(0)
  • 2020-11-22 03:51

    According to Harry's linear-gradient solution (answered Oct 14 '15 at 9:55), it says that opacity background isn't possible, I tried it and yep, it isn't.

    But! I found a workaround. No it's not super optimised, but it worked. So here's my solution. Since Harry doesn't use pseudo element, we can achieve this by creating one.

    Set position relative to the container and create a pseudo element with the same linear-gradient properties. In other words, just clone it. Then put a transparent background for the container, and lets say a black background for the clone. Put a position absolute on it, a z-index of -1 and an opacity value (ie. 50%). It will do the job. Again it's a workaround and it's not perfect but it works just fine.

    .cut-corner {
        position: relative;
        color: white;
        background-repeat: no-repeat;
        background-image: linear-gradient(white, white), linear-gradient(white, white), linear-gradient(white, white), linear-gradient(white, white), linear-gradient(to bottom left, transparent calc(50% - 1px), white calc(50% - 1px), white calc(50% + 1px), transparent calc(50% + 1px)), linear-gradient(transparent, transparent), linear-gradient(transparent, transparent);
        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;
    }
    .cut-corner:after {
        content: "";
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
        top: 0;
        z-index: -1;
        opacity: 0.5;
        background-repeat: no-repeat;
        background-image: linear-gradient(white, white), linear-gradient(white, white), linear-gradient(white, white), linear-gradient(white, white), linear-gradient(to bottom left, transparent calc(50% - 1px), white calc(50% - 1px), white calc(50% + 1px), black calc(50% + 1px)), linear-gradient(black, black), linear-gradient(black, black);
        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 */
    
    div {
      padding: 10px;
    }
    body{
     background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
    }
    <div class="cut-corner">
      Some content<br>
      Some content<br>
      Some content<br>
      Some content  
    </div>

    0 讨论(0)
  • 2020-11-22 03:58

    Another one solution: html:

    <div class="background">
      <div class="container">Hello world!</div>
    </div>
    

    css:

    .background {
      position: relative;
      width: 50px;
      height: 50px;
      border-right: 150px solid lightgreen;
      border-bottom: 150px solid lightgreen;
      border-radius: 10px;
    }
    .background::before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      width: 0;
      height: 0;
      border: 25px solid lightgreen;
      border-top-color: transparent;
      border-left-color: transparent;
    }
    .container {
      position: absolute;
      padding-left: 25px;
      padding-top: 25px;
      font-size: 38px;
      font-weight: bolder;
    }
    

    https://codepen.io/eggofevil/pen/KYaMjV

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

    This code allows you to cut corners on each side of the rectangle:

    div {
      display:block;
      height: 300px;
      width: 200px;
      background: url('http://lorempixel.com/180/290/') no-repeat;
      background-size:cover;
    
      -webkit-clip-path: polygon(10px 0%, calc(100% - 10px) 0%, 100% 10px, 100% calc(100% - 10px), calc(100% - 10px) 100%, 10px 100%, 0% calc(100% - 10px), 0% 10px);
      clip-path: polygon(10px 0%, calc(100% - 10px) 0%, 100% 10px, 100% calc(100% - 10px), calc(100% - 10px) 100%, 10px 100%, 0% calc(100% - 10px), 0% 10px);
    }
    

    http://jsfiddle.net/2bZAW/5552/

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

    CSS Clip-Path

    Using a clip-path is a new, up and coming alternative. Its starting to get supported more and more and is now becoming well documented. Since it uses SVG to create the shape, it is responsive straight out of the box.

    • CanIUse
    • Clip Path Generator

    div {
      width: 200px;
      min-height: 200px;
      -webkit-clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 25%, 75% 0);
      clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 25%, 75% 0);
      background: lightblue;
    }
    <div>
      <p>Some Text</p>
    </div>

    CSS Transform

    I have an alternative to web-tiki's transform answer.

    body {
      background: lightgreen;
    }
    div {
      width: 200px;
      height: 200px;
      background: transparent;
      position: relative;
      overflow: hidden;
    }
    div.bg {
      width: 200%;
      height: 200%;
      background: lightblue;
      position: absolute;
      top: 0;
      left: -75%;
      transform-origin: 50% 50%;
      transform: rotate(45deg);
      z-index: -1;
    }
    <div>
      <div class="bg"></div>
      <p>Some Text</p>
    </div>

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