简体   繁体   English

删除 :before :after 伪元素中的意外空间

[英]Removing unexpected space in :before :after pseudo elements

I recently had a problem when I was adding SVGs to :before and after pseudo elements to get curved sections on a page.我最近在将 SVG 添加到 :before 和 after 伪元素以在页面上获得弯曲部分时遇到了问题。

.div:after {
    position: absolute;
    content: url(svg/curve-down-bottom.svg);
    width: 100%;
    bottom: -1px;
    left: 0;
    z-index: 999;
}

The method worked great apart from that there was additional space below the SVG within the pseudo element, as shown below:除了在伪元素中的 SVG 下方有额外的空间外,该方法效果很好,如下所示:

伪元素中 svg 下方不需要的空间

I tried many methods to fix this, but see my answer below for the one that worked!我尝试了很多方法来解决这个问题,但请参阅下面的答案以了解最有效的方法!

I hope this helps someone.我希望这可以帮助别人。

Adding line-height: 0;添加行高:0; fixed the issue, as below:修复了问题,如下:

.div:after {
    position: absolute;
    content: url(svg/curve-down-bottom.svg);
    width: 100%;
    line-height: 0;
    bottom: -1px;
    left: 0;
    z-index: 999;
}

A you can see here:你可以在这里看到:

没有额外空间的伪元素

As others have commented you could also add display: block;正如其他人评论的那样,您还可以添加display: block; . . This has worked with similar issues in the past where images have space below, however with this specific issue line-height:0;这在过去曾处理过类似的问题,其中图像下方有空间,但是这个特定问题line-height:0; provided the desired result.提供了想要的结果。

.div:after {
    position: absolute;
    content: url(svg/curve-down-bottom.svg);
    width: 100%;
    line-height: 0;
    bottom: -1px;
    left: 0;
    z-index: 999;
    display: block;
}

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

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