CSS3 curved cutout from div?

后端 未结 2 1651
轻奢々
轻奢々 2020-12-21 07:42

Is there a way to create a cutout in a div with CSS3 like in the example below?

\"ente

相关标签:
2条回答
  • 2020-12-21 08:21

    You may have to simplfiy your design, but a similar effect is possible using gradients and pseudo-elements.

    Demo

    body{background: salmon;}
    .main {background:gray; width:300px; height:200px; position: relative; margin-top:20px;}
    .main:after, .main:before{content:''; width: 50px; height: 20px; background: inherit; position: absolute; }
    .main:after{ top:-20px; left:0;}
    .main:before{ top:-20px; right:0;}
    
    .phone{width:175px; height:40px; background: gray; margin-left:62.5px; border-radius: 50px; text-align: center; line-height: 40px; position: relative;}
    
    .phone:after,.phone:before{content:''; width:20px; height:20px; background:red; position: absolute;}
    
    .phone:before{bottom:-20px; left:-12px; background: -webkit-radial-gradient(100% 0, circle, rgba(204,0,0,0) 20px, gray 15px);}
    .phone:after{bottom:-20px; left:168px; background: -webkit-radial-gradient(0 0, circle, rgba(204,0,0,0) 20px, gray 15px);}
    
    0 讨论(0)
  • 2020-12-21 08:28

    EDIT - NEW Solution

    with radial-gradient, it is possible to achieve new levels of quality in cutout divs: Running Demo

    Read more on https://stackoverflow.com/a/18853833/1654265


    OLD Solution

    You can do it with an homogeneus background, not with an artistic one like your. There are things that CSS will never do, for example becoming Photoshop.

    However, you can do the trick using borders, negative margin and z-index;

    Demo: http://jsfiddle.net/cB8Qq/

    HTML

    <body>
        <div class="container">
            <div class="rounded">bla bla bla</div>
            <div class="digged"> <br/><br/>or yada yada yada </div>
        </div>
    </body>
    

    CSS

    .container{
        text-align: center;
        background: #ffffd;    
    }
    
    .rounded{
        margin: 0 auto;
        border-radius: 50px;
        width: 200px;
        height: 30px;
        background: silver;    
        padding: 10px;    
        border: 10px solid #ffffd;    
        z-index: 1;
        position: relative;
    }
    
    .digged{    
        margin: 0 auto;
        background: silver;
        width: 400px;
        height: 100px;
        margin-top: -30px
    }
    
    0 讨论(0)
提交回复
热议问题