Created
October 23, 2014 05:32
-
-
Save danielchikaka/9cf502575c725f4bc14e to your computer and use it in GitHub Desktop.
This css snippet is for centering div in a web page
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
.centered_div { | |
width: 100px; | |
height: 100px; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
margin-left: -50px; | |
margin-top: -50px; | |
background: red; | |
} | |
.centered_div { | |
width:500px; | |
height: 200px; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
margin-left: -250px; | |
margin-top: -100px; | |
background: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is another trick, if you don't know the width of a div
.centered {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}