Last active
February 16, 2018 12:20
-
-
Save SarathSantoshDamaraju/9b01eceb84e5abf118b145750fc67717 to your computer and use it in GitHub Desktop.
CSS -- centre align item to the div
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
<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