admin管理员组

文章数量:1405349

Pseudo elements are widely used for design. In the example below you'll see a pseudo element (::before) that's used to give a secondary background to a button, the use case is for adding some hover animation but the animation is not implemented here since that's not the issue.

The issue is that even tho the pseudo element is set to height and width of 100% you can still see a tiny bit of the parent element:

What you see in this image are two buttons. Both of them have a ::before element with background-color set to green, and height and width of 100%. The difference is that the button on the left has background-color set to white, and the right one set to the same green of the ::before element. If you'll look closely enough you can see how in the left button you can see the white background of the button.

Code example:

* {
    box-sizing: border-box;
}

body {
    background: black;
}

button { 
    position: relative;
    padding: 10px;
    margin: 0;
    border: none;
    background-color: white;
    z-index: 0;
    overflow: hidden;
    border-radius: 50px;
    border: 0;
}

button::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-color: green;
    z-index: -1;
}

button:nth-child(2) {
    background-color: green;
}
<div>
    <button>button</button>
    <button>button</button>
</div>

本文标签: