Cut Corners using CSS

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

    If you need a transparent cut out edge, you can use a rotated pseudo element as a background for the div and position it to cut out the desired corner:

    Transprent cut out edge on a div

    body {
      background: url(http://i.imgur.com/k8BtMvj.jpg);
      background-size: cover;
    }
    div {
      position: relative;
      width: 50%;
      margin: 0 auto;
      overflow: hidden;
      padding: 20px;
      text-align: center;
    }
    div:after {
      content: '';
      position: absolute;
      width: 1100%; height: 1100%;
      top: 20px; right: -500%;
      background: rgba(255,255,255,.8);
      transform-origin: 54% 0;
      transform: rotate(45deg);
      z-index: -1;
    }
    <div>
      ... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>
    </div>

    Note that this solution uses transforms and you need to add the required vendor prefixes. For more info see canIuse.

    To cut the bottom right edge, you can change the top, transform and transform-origin properties of the pseudo element to:

    body {
      background: url(http://i.imgur.com/k8BtMvj.jpg);
      background-size: cover;
    }
    div {
      position: relative;
      width: 50%;
      margin: 0 auto;
      overflow: hidden;
      padding: 20px;
      text-align: center;
    }
    div:after {
      content: '';
      position: absolute;
      width: 1100%; height: 1100%;
      bottom: 20px; right: -500%;
      background: rgba(255,255,255,.8);
      transform-origin: 54% 100%;
      transform: rotate(-45deg);
      z-index: -1;
    }
    <div>
      ... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>
    </div>

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

    With a small edit to Joseph's code, the element does not require a solid background:

    div {
        height: 300px;
        background: url('http://images2.layoutsparks.com/1/190037/serene-nature-scenery-blue.jpg');
        position: relative;
    }
    
    div:before {
        content: '';
        position: absolute;
        top: 0; right: 0;
        border-top: 80px solid white;
        border-left: 80px solid rgba(0,0,0,0);
        width: 0;
    }
    

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

    This use of 'rgba(0,0,0,0)' allows the inner 'corner' to be invisible .

    You can also edit the 4th parameter 'a', where 0 < a < 1, to have a shadow for more of a 'folded-corner' effect:

    http://jsfiddle.net/2bZAW/1922/ (with shadow)


    NOTE: RGBA color values are supported in IE9+, Firefox 3+, Chrome, Safari, and in Opera 10+.

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

    We had the problem of different background colors for our cutted elements. And we only wanted upper right und bottom left corner.

    body {
     background-color: rgba(0,0,0,0.3)
     
    }
    
    .box {
     position: relative;
     display: block;
     background: blue;
     text-align: center;
     color: white;
     padding: 15px;
     margin: 50px;
    }
    
    .box:before,
    .box:after {
     content: "";
     position: absolute;
     left: 0; 
     right: 0;
     bottom: 100%;
     border-bottom: 15px solid blue;
     border-left: 15px solid transparent;
     border-right: 15px solid transparent;
    }
    
    .box:before{
    	border-left: 15px solid blue;
    }
    
    .box:after{
    	border-right: 15px solid blue;
    }
    
    .box:after {
     bottom: auto;
     top: 100%;
     border-bottom: none;
     border-top: 15px solid blue;
    }
    
    
    /* Active box */
    .box.active{
    	background: white;
    	color: black;
    }
    
    
    
    .active:before,
    .active:after {
     border-bottom: 15px solid white;
    }
    
    .active:before{
    	border-left: 15px solid white;
    }
    
    .active:after{
    	border-right: 15px solid white;
    }
    
    .active:after {
     border-bottom: none;
     border-top: 15px solid white;
    }
    <div class="box">
     Some text goes here. Some text goes here. Some text goes here. Some text goes here.<br/>Some text goes here.<br/>Some text goes here.<br/>Some text goes here.<br/>Some text goes here.<br/>Some text goes here.<br/>
    </div>
    <div class="box">
     Some text goes here.
    </div>
    <div class="box active">
     Some text goes here.
     <span class="border-bottom"></span>
    </div>
    <div class="box">
     Some text goes here.
    </div>

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