Skip to content

Instantly share code, notes, and snippets.

@teeaich
Created July 11, 2013 17:26
Show Gist options
  • Select an option

  • Save teeaich/5977427 to your computer and use it in GitHub Desktop.

Select an option

Save teeaich/5977427 to your computer and use it in GitHub Desktop.
center content height and width
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px; /* half the height of the element */
margin-left: -100px; /* half the width of the element */
}
/* when content has percentage size */
.center {
position: absolute;
left: 50%;
top: 50%;
/*
Nope =(
margin-left: -25%;
margin-top: -25%;
*/
/*
Yep!
*/
transform: translate(-50%, -50%);
/*
Not even necessary really.
e.g. Height could be left out!
*/
width: 40%;
height: 50%;
}
/*Within Container
Place your content block inside of a position: relative container to perfectly center your content within the container!*/
.Absolute-Center {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
/*
Overflow
Content taller than the block or container (viewport or a position: relative container) will overflow and may spill outside the content block and container or even be cut off. Simply adding overflow: auto will allow the content to scroll within the block as long as the content block itself isn’t taller than its container (perhaps by adding max-height: 100%; if you don’t have any padding on the content block itself).
*/
.Absolute-Center.is-Overflow {
width: 50%;
height: 300px;
max-height: 100%;
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment