Skip to content

Instantly share code, notes, and snippets.

@SarathSantoshDamaraju
Last active February 16, 2018 12:20
Show Gist options
  • Save SarathSantoshDamaraju/9b01eceb84e5abf118b145750fc67717 to your computer and use it in GitHub Desktop.
Save SarathSantoshDamaraju/9b01eceb84e5abf118b145750fc67717 to your computer and use it in GitHub Desktop.
CSS -- centre align item to the div
/*
<div calss="parent">
<!-- Can be an image too -->
<p>Hello world</p>
</div>
*/
/* ============== With Flex */
div.parent {
display:flex;
align-items: center;
/* ... */
}
/* ============== With Transition - centers only vertically */
div.parent {
position: relative; /* Important */
/* ... */
}
.child {
position: absolute;
top: 50%;
transform: translate(0, -50%)
}
/* ============== General Code */
div{
position:relative;
/* ... */
}
p{
margin: 0;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment