35
loading...
This website collects cookies to deliver better user experience
.center class represents the element to be center aligned
.parent represents its parent element.
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.parent {
display: flex;
justify-content: center;
align-items: center;
}
flex-direction: column;
$w: 200px; // SCSS Variable
$h: 100px; // SCSS Variable
.center {
position: absolute;
top: 50%;
left: 50%;
margin: -50px -100px; // Negative margin of half the
// width and height
}
(#{$h} / 2) - Half the height
(#{$h} / 2) * -1) - Negated value of half the height
Which gives us:
margin: calc((#{$h} / 2) * -1) calc((#{$w} / 2) * -1);
.parent {
display: grid;
}
.center {
margin: auto;
}
.parent {
display: grid;
place-items: center;
}
.parent {
height: 600px; //Fixed height
padding: 200px 0; //Fixed vertical padding
}
.center{
margin: 0 auto;
height: 100%; // Child takes max height
}
.parent {
display: table-cell;
vertical-align: middle;
}
.center{
margin: auto;
}
.parent {
text-align: center;
}
.center {
margin: 0 auto;
}