简体   繁体   English

背景剪辑混乱边界半径

[英]Background-clip messes with border-radius

I have a box that has following CSS: 我有一个包含以下CSS的盒子:

.box{
    background-clip: content-box;
    background-color: #FFEA27;
    border-radius: 4%;
} 

And the problem is that border radius is not being calculated correctly because of the background-clip: content-box; 问题是由于background-clip: content-box;没有正确计算边界半径background-clip: content-box; since border-radius calculates paddings also. 因为border-radius也计算填充。 At the end I get results like this: https://prnt.sc/n9gxpv 最后我得到这样的结果: https//prnt.sc/n9gxpv

Let's look at the upper right corner for example. 我们来看看右上角的例子。 The rounding from right line to edge and from top line to edge is not equal and thus we I don't get perfectly round edge. 从右线到边缘以及从顶线到边缘的圆角不相等,因此我们没有得到完美的圆边。

Is there any workaround for this. 有没有解决方法呢? Like setting background color of a div without using background-color . 就像设置div的背景颜色而不使用background-color Important this to say is that I cannot switch to margins from paddings and eliminate need for backgroud-clip attribute. 重要的是,我无法切换到填充边距并消除对backgroud-clip属性的需要。

I made this simple workaround to fix it: 我做了这个简单的解决方法来修复它:

I made another div inside the current box and set it's width and height to 100% (so it takes the width and height of original element). 我在当前框中创建了另一个div,并将其宽度和高度设置为100%(因此它需要原始元素的宽度和高度)。 Than I just set background color and border radius to inner element. 我只是将背景颜色和边框半径设置为内部元素。 Here is code example. 这是代码示例。

HTML: HTML:

<div class="box">
        <div class="innerBox">

        </div>
</div>

CSS: CSS:

.innerBox{
    width: 100%;
    height: 100%;
    border-radius: 2%;
    background-color: #1C1C1C;
}

Instead of using % as the unit, use pixel. 而不是使用%作为单位,使用像素。

.box{
    background-clip: content-box;
    background-color: #FFEA27;
    border-radius: 4px;
} 

Works perfectly. 完美的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM