Created
January 6, 2015 20:16
-
-
Save kostasx/5ef90effe320f6e7af09 to your computer and use it in GitHub Desktop.
CENTERING ELEMENTS USING CSS
This file contains hidden or 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
/* | |
* CENTERING ELEMENTS USING CSS | |
* <div class="container"><div class="box"></div></div> | |
*/ | |
/* HORIZONTAL CENTERING USING TEXT ALIGN */ | |
.container{ | |
text-align: center; | |
} | |
.box { | |
display: inline-block; | |
} | |
/* HORIZONTAL CENTERING USING MARGIN AUTO */ | |
.box { | |
margin: 0 auto; | |
} | |
/* HORIZONTAL + VERTICAL CENTERING (KNOWN WIDTH AND SIZE) */ | |
.container { | |
position:relative; | |
} | |
.box { | |
position:absolute; | |
left:50%; | |
top:50%; | |
margin-left: -200px; /* .box width is 200px */ | |
margin-top: -200px; /* .box height is 200px */ | |
} | |
/* HORIZONTAL + VERTICAL CENTERING (UNKNOWN WIDTH AND SIZE) */ | |
.container { | |
position:relative; | |
} | |
.box { | |
position:absolute; | |
top:50%; | |
left:50%; | |
transform: translate(-50%, -50%); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment