center div vertically and horizontally code example
Example 1: center div horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Example 2: css center horizontally and vertically
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Example 3: css align center
//HTML
<div class="parent">
<span>Hello World</span>
</div>
//CSS
.parent {
display: flex;
justify-content: center;
align-items: center;
}
Example 4: css align items vertical center
.parent {
display: flex;
justify-content: center;
align-items: center;
}
Example 5: vertically and horizontally center a fixed div
// you dont need to know the divs dimensions and it does not require any container divs
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Example 6: css vertical center
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.parent {
display: flex;
flex-direction: column;
justify-content: center;
}