css
  • css3
  • css-selectors
  • 2017-08-26 142 views 0 likes 
    0

    一个CSS按钮与Safari浏览器的截图再次更新:@ LGSon的答案后的样式尖左边缘和圆角右边缘

    enter image description here

    更新:

    谢谢您的回答,但以下代码不会产生正确的效果,人们可以清楚地看到SVG与标签之间因高度差异而分开:

    .divsclass { 
     
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 146 206'%3E%3Cdefs%3E%3Cstyle%3E.c1%7Bfill:%23fff;%7D.c2%7Bfill:none;stroke:%23a0310f;stroke-linecap:round;stroke-miterlimit:10;stroke-width:10px;%7D%3C/style%3E%3C/defs%3E%3Cpath d='M146 0H98.53l-1 .47-94 94a12 12 0 0 0 0 17l94 94L99 206h47z' class='c1'/%3E%3Cpath d='M113.18 28.2l-73 74.8 73 74.81' class='c2'/%3E%3C/svg%3E"); 
     
        background-repeat: no-repeat; 
     
    } 
     
    
     
    .divsclass a { 
     
        color: red; 
     
        text-transform: uppercase; 
     
        background-color: white; 
     
        margin-left: .75em; 
     
        border-bottom-right-radius: 10px; 
     
        border-top-right-radius: 10px; 
     
        font-size: 75%; 
     
    }
    <div style="padding: 1em; background: black"> 
     
        <div class="divsclass"><a role="button">BACK</a></div> 
     
    </div>

    我试图创建以下按钮,其中里面的文本(BACK在下面的示例)可以是可变长度的。我正在尝试左侧的背景图片和右侧的边框半径,但它不起作用(使用:before)。有任何想法吗?

    P.S.如果有帮助,我确实将左三角形图像作为单独的SVG。

    enter image description here

    +0

    你能分享你的CSS代码吗? – Geff

    +0

    如果左边的三角形是一个SVG,为什么不把整个按钮的形状变成一个SVG(或者至少是左边的那一部分,其余部分可能是一个正常的右边有圆角的div,所以它由于内容可以轻松拉伸)?否则,试图用CSS在左边生成舍入点需要类似clip-path(https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path)或其他奇怪的黑客。 SVG对此非常完美 – diopside

    +0

    我更新了我的问题以反映问题,即使在使用SVG时也是如此。 – Sammy

    回答

    1

    这可以结合单独使用CSS来实现与伪元素

    a { 
     
        position: relative; 
     
        display: inline-block; 
     
        padding: 2px 10px 2px 20px; 
     
        border-top-right-radius: 10px; 
     
        border-bottom-right-radius: 10px; 
     
        overflow: hidden; 
     
        text-decoration: none; 
     
    } 
     
    a::before, 
     
    a::after { 
     
        position: absolute; 
     
        content: ''; 
     
        left: 0; 
     
        top: 50%; 
     
        width: 100%; 
     
        padding-bottom: 100%; 
     
        background: lightgray; 
     
        transform: rotate(-45deg); 
     
        transform-origin: left top; 
     
        z-index: -1; 
     
    } 
     
    a::after { 
     
        left: 5px; 
     
        border: 1px solid black; 
     
    }
    <a href="#">BACK</a>


    更新

    这里是使用现有的SVG和伪版本

    span { 
     
        display: inline-block; 
     
        background-color: black; 
     
        padding: 2px 10px 2px 20px; 
     
    } 
     
    a { 
     
        position: relative; 
     
        display: block; 
     
        color: red; 
     
        padding: 2px 10px 2px 0; 
     
        text-transform: uppercase; 
     
        background-color: white; 
     
        border-bottom-right-radius: 10px; 
     
        border-top-right-radius: 10px; 
     
        font-size: 75%; 
     
    } 
     
    
     
    a::before { 
     
        content: ''; 
     
        position: absolute; 
     
        top: 0; 
     
        left: -14px; 
     
        height: 100%; 
     
        width: 15px; 
     
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 146 206'%3E%3Cdefs%3E%3Cstyle%3E.c1%7Bfill:%23fff;%7D.c2%7Bfill:none;stroke:%23a0310f;stroke-linecap:round;stroke-miterlimit:10;stroke-width:10px;%7D%3C/style%3E%3C/defs%3E%3Cpath d='M146 0H98.53l-1 .47-94 94a12 12 0 0 0 0 17l94 94L99 206h47z' class='c1'/%3E%3Cpath d='M113.18 28.2l-73 74.8 73 74.81' class='c2'/%3E%3C/svg%3E"); 
     
        background-repeat: no-repeat; 
     
        background-size: 100%; 
     
        background-position: center; 
     
    }
    <span><a role="button">BACK</a></span>

    +0

    谢谢,看起来很完美!而不是'div',我可以使用'a'还是不行? – Sammy

    +0

    @Sammy更新我的答案,而不是使用锚点 – LGSon

    +0

    谢谢......所以这工作得很好,除了在Safari浏览器! – Sammy

    0

    如果使用DIV作为一个按钮和图像作为背景,你可以让你的CSS是这样的:

    div.divsclass { 
    border-radius: 15px 50px 30px 5px: 
    } 
    

    所以,你可以设置边框半径在所有的4个角落,如果你只想用CSS。或者如前所述编辑你的SVG。

    +0

    问题(正如你可以看到,如果你运行上面的代码)是,这取决于字体的大小,并显示在不同的高度在每个浏览器。我无法对齐'a'标签和背景的像素。 – Sammy

    相关问题