Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active February 10, 2016 04:35
Show Gist options
  • Save khoand0000/a8c9e8777837f32c7533 to your computer and use it in GitHub Desktop.
Save khoand0000/a8c9e8777837f32c7533 to your computer and use it in GitHub Desktop.
middle vertical align. Just using the link and apply suitable case: https://css-tricks.com/centering-css-complete-guide/

Take note when using https://css-tricks.com/centering-css-complete-guide/

  • Vertically
    • Is it inline or inline-* elements (like text or links)?
      • Is it a single line?
        • don't know height of element: padding-top == padding-bottom
        • know height of element: using line-height
            .center-text-trick {
              height: 100px; /* apply inline-block element, inline element doesn't need it */
              line-height: 100px; 
              white-space: nowrap; /* don't break line */
            }

Ref:

using line-height make text (inline or inline-block) is middle alignment

<div class="container">
  <span class="text">middle text</span>
</div>

Suppose, you know height of .container (eg: 300px). css is

.text {
line-height: 300px;
}

Note, the solution is right when text is not broken to multi-lines.

/* http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/ */
.parent-element {
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment