Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active December 16, 2020 11: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 vielhuber/0f1273b25d9806459716 to your computer and use it in GitHub Desktop.
Save vielhuber/0f1273b25d9806459716 to your computer and use it in GitHub Desktop.
vertical centering / align #css
/*
method 1: flexbox
- you don't special rules for the child here
- height of parent should be defined via min-height
*/
.parent {
display: flex;
flex-direction: column;
justify-content: center;
align-items:center;
min-height: 100vh; /* change this */
}
/* ie11 fix */
.parent, .parent > *
{
max-width:100%;
}
/*
method 2: position
- height of parent must not be explicitly defined (except it has no other childs)
- bad when the child element is bigger than the screen height
*/
.parent {
position: relative;
height:100vh; /* change this */
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/*
method 3: legacy
- height of parent must be explicitly defined
*/
.parent
{
text-align: center;
white-space: nowrap;
font-size:0;
height:100vh; /* change this */
}
.parent:before
{
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.parent > *
{
display: inline-block;
vertical-align: middle;
width: 100%;
font-size:13px;
white-space:normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment