Here's a good drop-in solution for perfectly centered circular X icon buttons
width
and height
in the pseudo element rule .close::before, .close::after
aria-label
currentColor
to adapt to the current text color specified on the button or an ancestor..close {
vertical-align: middle;
border: none;
color: inherit;
border-radius: 50%;
background: transparent;
position: relative;
width: 32px;
height: 32px;
opacity: 0.6;
}
.close:focus,
.close:hover {
opacity: 1;
background: rgba(128, 128, 128, 0.5);
}
.close:active {
background: rgba(128, 128, 128, 0.9);
}
/* tines of the X */
.close::before,
.close::after {
content: " ";
position: absolute;
top: 50%;
left: 50%;
height: 20px;
width: 4px;
background-color: currentColor;
}
.close::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.close::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
_x000D_
<div style="padding: 15px">
<button class="close" aria-label="Close"></button>
</div>
<div style="background: black; color: white; padding: 15px">
<button class="close" aria-label="Close"></button>
</div>
<div style="background: orange; color: yellow; padding: 15px">
<button class="close" aria-label="Close"></button>
</div>
_x000D_