Since pseudo elements are essentially added as children elements, one option is to use a flexbox layout. Add display: flex
to the parent element, and then use align-items: center
for vertical centering and justify-content: center
for horizontal centering.
.block {
height: 150px;
width: 150px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
}
.block:after {
content: "content";
}
<div class="block"></div>
Alternatively, you could also absolutely position the element relative to the parent and use the transform trick for vertical/horizontal centering.
.block {
height: 150px;
width: 150px;
border: 1px solid black;
position: relative;
}
.block:after {
content: "content";
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
<div class="block"></div>