center with "transform"
Last updated
Last updated
.centered-element {
margin: 0;
/* โญ๏ธ child ่ท้จ parent ็ๅๆจ็ณป */
position: absolute;
/* โญ๏ธ ๅ็ดๆฐดๅนณ็ฝฎไธญ */
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
/* โญ๏ธ 1. parent ่จญๅฎๅๆจ็ณป */
position: relative;
height: 100vh;
border: 2px solid #006100;
background-color: orange;
}
.centered-element {
margin: 1em;;
padding: 1em;
border: 1px solid black;
background-color: rgba(255,255,255, 0.5);
/* โญ๏ธ 2. child ่ท้จ parent ็ๅๆจ็ณป */
position: absolute;
/* โญ๏ธ 3. ๅ็ด็ฝฎไธญ */
top: 50%;
transform: translateY(-50%);
}
<div class="container">
<div class="centered-element">
<p>I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered.</p>
</div>
</div>