【问题标题】:CSS borders bend insideCSS 边框向内弯曲
【发布时间】:2019-09-17 11:14:27
【问题描述】:

我想在元素内构建带有弯曲边框的容器。是否可以只使用css?如果是容器应该有auto高度

【问题讨论】:

    标签: html css responsive-design


    【解决方案1】:

    你可以试试这个方法——在你的 div 前后画一个圆角的 div

    CSS:

    #bend-inside{
        width: 500px;
        overflow: hidden;
    }
    
    #box-before{
        background-color: white;
        border-radius: 600px;
        height: 600px;
        width: 600px;
        display: inline-block;
        position: absolute;
        z-index: 2;
       right: 550px;
        top: -200px;
        overflow: hidden;
    }
    
    #box{
        background-color: #e5e5e5;
        width: 400px;
        height: 200px;
        margin: 0 auto;
        display: inline-block;
        overflow: hidden;
        position: absolute;
        z-index: 1;
        right: 200px;
    }
    
    #box-after{
        background-color: white;
        border-radius: 600px;
        height: 600px;
        width: 600px;
        display: inline-block;
        position: absolute;
        z-index: 2;
        right: -350px;
        top: -200px;
        overflow: hidden;
    }
    

    HTML:

    <div id="bend-inside">
        <div id="box-before"></div>
        <div id="box"></div>
        <div id="box-after"></div>
    </div>
    

    Example

    【讨论】:

    • 感谢您的解决方案。但是主体背景是动态的,所以我们应该为圆形元素设置透明背景
    【解决方案2】:

    你可以使用这个技巧:

    HTML:

    <div class='continer'>
        <div class='left'></div>
        <div class='right'></div>
    </div>
    

    CSS:

    .continer{
        width:500px;
        height:200px;
        background:red;
    }
    .right{
        float:right;
        width:10%;
        height:120%;
        border-radius:100%;
        margin-top:-5%;
        margin-right:-4%;
        background:white;
    }
    .left{
        float:left;
        width:10%;
        height:120%;
        border-radius:100%;
        margin-top:-5%;
        margin-left:-4%;
        background:white;
    }
    

    这里的例子: http://jsfiddle.net/NSm8M/

    【讨论】:

    • 感谢您的解决方案。但是主体背景是动态的,所以我们应该为圆形元素设置透明背景
    【解决方案3】:

    如果您正在寻找纯 CSS 解决方案,那么一种选择可能是使用 ::before::after 伪元素来实现类似的效果。

    演示:http://jsfiddle.net/abhitalks/2mx6b/

    HTML

    <div class="curved"></div>
    

    相关 CSS

    .curved {
        width: 400px;
        height: 200px;
        background-color: #ccc;
        border-radius: 0 0 35% 35%;
        position: relative;
    }
    .curved::before {
        height: 200px;
        width: 40px;
        border-radius: 0 40% 50% 0;
        content: '';
        position: absolute;
        top: 0px; left: 0px;
    }
    .curved::after{
        height: 200px;
        width: 40px;
        border-radius: 40% 0 0 50%;
        content: '';
        position: absolute;
        top: 0px; right: 0px;
    }
    

    更新

    另一种方法是使用radial-gradient。这将完全摆脱伪元素。但是,缺点是您必须仔细制作它并牢记跨浏览器兼容性。

    无论如何,只是为了展示它是如何完成的:

    演示 2:http://jsfiddle.net/abhitalks/2mx6b/2/

    CSS

    .curved {
        width: 400px;
        height: 200px;
        border-radius: 0 0 40% 40%;
        background: 
            -webkit-radial-gradient(-19% center, circle, orange 0, orange 124px, transparent 50px), 
            -webkit-radial-gradient(119% center, circle, orange 0, orange 124px, transparent 50px) #ccc;
        background-clip: padding-box;
    }
    

    注意:关于您的评论:不可以。您实际上不能在任何建议的解决方案中使用transparent。原因是父元素的背景会渗出,或者在多个背景的情况下,堆叠顺序中较低的背景会渗出。

    【讨论】:

    • 感谢您的解决方案。但是主体背景是动态的,所以我们应该为圆形元素设置透明背景
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2010-09-26
    相关资源
    最近更新 更多