我想垂直居中一個僞元素與它的父h1元素。我用transform: translate(0,-50%);
來移動它,但它沒有正確對齊。在檢查開發人員工具中的元素時,我得到的h1高度爲37px,後者在-18.5px處正確計算。垂直中心::之後div
關於我失蹤的任何想法?
html,
body {
margin: 0;
padding: 0;
}
body {
color: white;
background-color: #000;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
.container {
max-width: 38rem;
padding-left: 1.5rem;
padding-right: 1.5rem;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
h1 {
font-size: 2rem;
display: block;
position: relative;
}
h1::after {
z-index: -1;
transform: translate(0,-50%);
position:absolute;
display: block;
font-size: 2rem;
content: "Heading one";
}
<div class="container">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<h1>Heading one</h1>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
</div>
Booyah!這很有用。感謝快速回復@Victor Allegret。 – sansSpoon