【问题标题】:Extra pixel in before and after pseudo elements伪元素前后的额外像素
【发布时间】:2020-07-10 12:08:53
【问题描述】:

我正在尝试使用 before 和 after 伪元素创建背景效果,方法是使其比实际元素高和宽一个像素,但它似乎总是在右侧或左侧多出一个像素。这仅在浏览器最大化(Firefox、Chrome 和 Edge)时发生,但在浏览器宽度较小时不会发生。

*, *::before, *::after{ box-sizing: border-box; }

body {
    display: flex;
    flex-flow: column wrap;
    justify-content: center;
    align-items: center;
    background-color: #111;
}

img {
    max-width: 300px;
    display: block;
    padding: 4px;
}

.main-box {
    position: relative;
}

.img-box {
    padding: 0;
    margin: 0;
    background-color: #000;
}

.img-box::before{
    content: '';
    position: absolute;
    top: -1px;
    left: -1px;
    right: -1px;
    bottom: -1px;
    filter: blur(10px);
    z-index: -2;
}

.img-box::after{
    content: '';              
    position: absolute;
    top: -1px;
    left: -1px;
    right: -1px;
    bottom: -1px;
    z-index: -1;
}

.img-box::before, .img-box::after{
    background-image: linear-gradient(45deg, #ff0000, #111, #0000ff);
    opacity: 0.7;
    transition: opacity ease-out 150ms;
}

.main-box:hover .img-box::after {
    opacity: 1;
}
<div class="main-box">
   <div class="img-box"><img src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" alt="keyboard"></div>
</div>

在图片中不是很清楚,但在浏览器中,背景图像的所有边都多 1px,除了左侧多 2px。

输出:Thicker line on the left

【问题讨论】:

    标签: html css background-image pseudo-element absolute


    【解决方案1】:

    这看起来像抗锯齿。我猜您的浏览器或操作系统的缩放级别设置为 100% 以外的其他值。

    一些浏览器会尝试调整定位,但在某些缩放级别上,这无法正确完成,最终会导致一侧落地,另一侧天花板。

    为了避免这种情况,您可以使用translate 属性,该属性应该允许适当的抗锯齿(它会变得模糊,但大小相同)。

    *, *::before, *::after{ box-sizing: border-box; }
    
    body {
        display: flex;
        flex-flow: column wrap;
        justify-content: center;
        align-items: center;
        background-color: #111;
    }
    
    img {
        max-width: 300px;
        display: block;
        padding: 4px;
    }
    
    .main-box {
        position: relative;
    }
    
    .img-box {
        padding: 0;
        margin: 0;
        background-color: #000;
    }
    
    .img-box::before{
        content: '';
        position: absolute;
        top: 0px;
        left: 0px;
        width: calc( 100% + 2px );
        height: calc( 100% + 2px );
        transform: translate(-1px,-1px);
        filter: blur(10px);
        z-index: -2;
    }
    
    .img-box::after{
        content: '';              
        position: absolute;
        top: 0px;
        left: 0px;
        width: calc( 100% + 2px );
        height: calc( 100% + 2px );
        transform: translate(-1px,-1px);
        z-index: -1;
    }
    
    .img-box::before, .img-box::after{
        background-image: linear-gradient(45deg, #ff0000, #111, #0000ff);
        opacity: 0.7;
        transition: opacity ease-out 150ms;
    }
    
    .main-box:hover .img-box::after {
        opacity: 1;
    }
    <div class="main-box">
       <div class="img-box"><img src="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png" alt="keyboard"></div>
    </div>

    【讨论】:

    • 非常感谢!有效。只是为了确保我已经正确理解它,使用位置来定义元素的大小可能会导致一些舍入和抗锯齿问题,而使用宽度、高度和变换不会,因为大小是硬编码的(因为没有更好的术语)。我说的对吗?
    • 在使用topleft时,一些浏览器会尝试将实际值四舍五入到最接近的像素边界,这样就没有抗锯齿(也方便避免与其余部分混淆)页面布局)。通过一些缩放(例如 125%),您最终可能会以一种奇怪的方式(一侧比另一侧大)使框变得比它应该更大的位置。当使用transform 时,该元素将超出正常布局,因此浏览器可以使用抗锯齿来定位它(+ 它在 GPU 上呈现为它自己的画布层)。所以这里,不需要拉伸
    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2016-08-14
    • 2016-11-26
    • 1970-01-01
    • 2013-02-02
    • 2011-05-12
    • 2016-05-10
    相关资源
    最近更新 更多