具有圆边界半径的背景剪辑

时间:2015-06-06 00:50:00

标签: html css

如何在background-clip:padding-box上实现边框半径?

我有一个不透明的厚边框;但是,所有四个内角都是90度角。

<!DOCTYPE html>
<html>
<head>
    <title>Border</title>
    <style>
        body, html{
            width: 100%;
            height: 100%;
        }
        .image_frame{
            display: flex;
            width:50%;
            background: white;
            background-clip: padding-box;
            border: 20px solid rgba(0, 0, 0, 0.3);
            border-radius: 20px;
        }
    </style>
</head>
<body>
    <div class="image_frame">
        <img src="http://trudog.com/home/wp-content/uploads/2015/03/Shocked-Pups.jpg">
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

background-clip: padding-box的实施很好。 .image_frame(在这种情况下,body)的父级背景确实应该在半透明边界下方可见。

这里的问题是边框宽度等于边框半径。因此,边框的内角将为90度,因为圆角基本上采用实心象限的形式。来自spec

  

填充边(内边框)半径是外边框半径减去相应的边框粗细。在这导致负值的情况下,内半径为零。

如果希望内角具有半径,则需要指定大于边框宽度的边框半径。对于20px的内半径和20px的border-width,生成的border-radius为40px:

.image_frame {
    display: flex;
    width: 50%;
    background: white;
    background-clip: padding-box;
    border: 20px solid rgba(0, 0, 0, 0.3);
    border-radius: 40px;
    overflow: hidden;
}

此外,您需要设置overflow: hidden以使圆角实际剪切图像;有关详细信息,请参阅我对this question的回答。

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<title>Border</title>
<style>
.image_frame {
 display: flex;
 width: 150px;
 background: white;
 background-clip: padding-box;
 border: 20px solid rgba(0, 0, 0, 0.3);
 border-radius: 190px;
 }
 body, html{
        width: 100%;
        height: 100%;
    }
</style>
</head>
 <body>
  <div class="image_frame">
    <img src="http://trudog.com/home/wp-content/uploads/2015/03/Shocked-Pups.jpg" style="border-radius: 146px;width: 150px; height: 150px;">
  </div>
</body>
  </html>

试试这个..... https://jsfiddle.net/f0v5v8ns/1/