Skip to content

Instantly share code, notes, and snippets.

@danielchikaka
Created October 23, 2014 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielchikaka/9cf502575c725f4bc14e to your computer and use it in GitHub Desktop.
Save danielchikaka/9cf502575c725f4bc14e to your computer and use it in GitHub Desktop.
This css snippet is for centering div in a web page
.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;
}
@BenRichter
Copy link

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%);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment