To align the text contents of this div vertically center.

[pastacode lang=”css” manual=”%23box%0A%7B%0A%20%20height%3A%2090px%3B%0A%20%20width%3A%20270px%3B%0A%20%20background%3A%20%23001%3B%0A%20%20font-size%3A%2048px%3B%0A%20%20font-style%3A%20oblique%3B%0A%20%20color%3A%20%23FFF%3B%0A%20%20text-align%3A%20center%3B%0A%20%20margin-top%3A%2021px%3B%0A%20%20margin-left%3A%205px%3B%0A%7D%0A%3Cdiv%20Id%3D%22box%22%3E%0A%20%20Lorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%0A%3C%2Fdiv%3E%0A” message=”css align code” highlight=”” provider=”manual”/]

  • This approach can only works for a single line of text
[pastacode lang=”markup” manual=”div%0A%7B%0A%20%20height%3A%2090px%3B%0A%20%20line-height%3A%2090px%3B%0A%0A%20%20text-align%3A%20center%3B%0A%20%20border%3A%202px%20dashed%20%23f69c55%3B%0A%7D%0A%3Cdiv%3E%0A%20%20Hello%20World!%0A%3C%2Fdiv%3E%0A” message=”css align code” highlight=”” provider=”manual”/]
  • solution for a single line and multiple lines of text
[pastacode lang=”css” manual=”div%20%0A%7B%0A%20%20height%3A%20200px%3B%0A%20%20line-height%3A%20200px%3B%0A%20%20text-align%3A%20center%3B%0A%20%20border%3A%202px%20dashed%20%23f69c55%3B%0A%7D%0Aspan%20%0A%7B%0A%20%20display%3A%20inline-block%3B%0A%20%20vertical-align%3A%20middle%3B%0A%20%20line-height%3A%20normal%3B%0A%7D%0A%3Cdiv%3E%0A%20%20%3Cspan%3EWelcome%20to%20Wikitechy.%20%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A” message=”css align code” highlight=”” provider=”manual”/]

  • Use the below CSS code
[pastacode lang=”css” manual=”display%3A%20table-cell%3B%0Avertical-align%3A%20middle%3B%0A%20finally%20CSS%20looks%20like%3A%0A%23box%20%0A%7B%0A%20%20height%3A%2090px%3B%0A%20%20width%3A%20270px%3B%0A%20%20background%3A%20%23001%3B%0A%20%20font-size%3A%2048px%3B%0A%20%20font-style%3A%20oblique%3B%0A%20%20color%3A%20%23FFF%3B%0A%20%20text-align%3A%20center%3B%0A%20%20margin-top%3A%2020px%3B%0A%20%20margin-left%3A%205px%3B%0A%20%20display%3A%20table-cell%3B%0A%20%20vertical-align%3A%20middle%3B%0A%7D%0A%3Cdiv%20id%3D%22box%22%3E%0A%20%20Some%20text%3C%2Fdiv%3E%0A” message=”text align code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Flexible approach for vertical alignment:
[pastacode lang=”markup” manual=”div%20%0A%7B%0A%20%20width%3A%20250px%3B%0A%20%20min-height%3A%2050px%3B%0A%20%20line-height%3A%2050px%3B%0A%20%20text-align%3A%20center%3B%0A%20%20border%3A%201px%20solid%20%23123456%3B%0A%20%20margin-bottom%3A6px%3B%0A%7D%0Aspan%20%0A%7B%0A%20%20display%3A%20inline-block%3B%0A%20%20vertical-align%3A%20middle%3B%0A%20%20line-height%3A%20normal%3B%0A%7D%0A%3Cdiv%3E%0A%20%20%3Cspan%3ELorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%3Cbr%20%2F%3E%0A%20%20%20%20Lorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%3Cbr%20%2F%3E%0A%20%20%20Lorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A%3Cdiv%3E%0A%20%3Cspan%3ELorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A%3Cdiv%3E%0A%20%20%3Cspan%3ELorem%20ipsum%20dolor%20sit%20amet.%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A%3Cdiv%3E%0A” message=”text align code” highlight=”” provider=”manual”/]

  • For reference and simpler answer for vertically center text:
  • Pure CSS:
[pastacode lang=”css” manual=”vertical-align%20%0A%7B%0A%20%20%20%20position%3A%20relative%3B%0A%20%20%20%20top%3A%2050%25%3B%0A%20%20%20%20-webkit-transform%3A%20translateY(-50%25)%3B%0A%20%20%20%20-ms-transform%3A%20translateY(-50%25)%3B%0A%20%20%20%20transform%3A%20translateY(-50%25)%3B%0A%7D%0A” message=”text align code” highlight=”” provider=”manual”/] [pastacode lang=”css” manual=”Or%20as%20a%20SASS%2FSCSS%20Mixin%3A%0A%40mixin%20vertical-align%20%0A%7B%0A%20%20%20%20position%3A%20relative%3B%0A%20%20%20%20top%3A%2060%25%3B%0A%20%20%20%20-webkit-transform%3A%20translateY(-50%25)%3B%0A%20%20%20%20-ms-transform%3A%20translateY(-50%25)%3B%0A%20%20%20%20transform%3A%20translateY(-50%25)%3B%0A%7D%0A” message=”div align center” highlight=”” provider=”manual”/] [ad type=”banner”]
  • Use by:
[pastacode lang=”css” manual=”.class-to-center%20%0A%7B%0A%20%20%20%20%40include%20vertical-align%3B%0A%7D%0A” message=”div align center” highlight=”” provider=”manual”/]
  • The element being placed on a “half pixel”. A solution for this parent element to preserve-3d. Like following:
    .parent-element
[pastacode lang=”css” manual=”%7B%0A%20%20%20%20-webkit-transform-style%3A%20preserve-3d%3B%0A%20%20%20%20-moz-transform-style%3A%20preserve-3d%3B%0A%20%20%20%20transform-style%3A%20preserve-3d%3B%0A%7D%0A” message=”text align css code” highlight=”” provider=”manual”/]

  • For a single line of text
  • It can be used when #box has non-fixed, relative height in %.
[pastacode lang=”css” manual=”%3Cdiv%20id%3D%22box%22%3E%3C%2Fdiv%3E%0A%23box%3A%3Abefore%0A%7B%0A%20%20%20%20display%3A%20block%3B%0A%20%20%20%20content%3A%20%22%22%3B%0A%20%20%20%20height%3A%2050%25%3B%0A%7D%0A%23box%3A%3Aafter%20%0A%7B%0A%20%20%20%20vertical-align%3A%20top%3B%0A%20%20%20%20line-height%3A%200%3B%0A%20%20%20%20content%3A%20%22TextContent%22%3B%0A%7D%0A” message=”text align css code” highlight=”” provider=”manual”/]
  • If we need to place inner text in HTML, not in CSS, then we need to wrap text content in additional inline element and edit  #box:: after to match it.

For example,

[pastacode lang=”markup” manual=”%3Cdiv%20id%3D%22box%22%3E%3Cspan%3ETextContent%3C%2Fspan%3E%3C%2Fdiv%3E%0AIn%20this%20case%20%23box%3A%3Aafter%20should%20be%20replaced%20to%20%23box%20span.%0A” message=”align center css” highlight=”” provider=”manual”/]

  • A very simple & most powerful solution to vertically align center:
[pastacode lang=”css” manual=”.outer-div%20%0A%7B%0A%20height%3A%20100px%3B%0A%20%20width%3A%20200px%3B%0A%20%20text-align%3A%20center%3B%0A%20%20border%3A%201px%20solid%20%23000%3B%0A%7D%0A%0A.inner%20%0A%7B%0A%20%20position%3A%20relative%3B%0A%20%20top%3A%2045%25%3B%0A%20%20transform%3A%20translateY(-45%25)%3B%0A%20%20color%3A%20red%3B%0A%7D%0A%3Cdiv%20class%3D%22outer-div%22%3E%0A%20%20%3Cspan%20class%3D%22inner%22%3ENo%20data%20available%3C%2Fspan%3E%0A%3C%2Fdiv%3E%0A” message=”align center css” highlight=”” provider=”manual”/] [ad type=”banner”]

  • The CSS property transform is usually used for rotating and scaling elements, but with its translateY function we can now vertically align elements. Usually this must be done with absolute positioning or setting line-heights, but these require us to either know the height of the element or only works on single-line text, etc.
  • So we can write,
[pastacode lang=”css” manual=”.element%20%0A%7B%0A%20%20%20%20position%3A%20relative%3B%0A%20%20%20%20top%3A%2050%25%3B%0A%20%20%20transform%3A%20translateY(-50%25)%3B%0A%7D%20%0A” message=”align center css” highlight=”” provider=”manual”/]
  • It is a similar technique to the absolute-position method, but with the upside that we don’t have to set any height on the element or position-property on the parent

  • To achieve the effect of line-height, it only works if we have one line of text. if we had more content, the text will still be centered, but the div itself will be slightly larger.
[pastacode lang=”markup” manual=”div%0A%7B%0A%20%20height%3A120px%3B%0A%20%20line-height%3A%20120px%3B%0A%7D%0AAnother%20way%2C%0Adiv%20%0A%7B%0A%20%20%20padding%3A%2060px%200%3B%0A%7D%0A” message=”html align code” highlight=”” provider=”manual”/]
  • It set the top and bottom padding of the div to 60px, and the left and right padding to zero, making the div 120px high, and placing the text vertically centered in the div.

  • For all our vertical alignment
[pastacode lang=”css” manual=”%40mixin%20vertical-align(%24position%3A%20relative)%20%0A%7B%0A%20%20position%3A%20%24position%3B%0A%20%20top%3A%2060%25%3B%0A%20%20-webkit-transform%3A%20translateY(-50%25)%3B%0A%20%20-ms-transform%3A%20translateY(-50%25)%3B%0A%20%20transform%3A%20translateY(-50%25)%3B%0A%7D%0AThen%20include%0A.element%0A%7B%0A%20%20%20%20%40include%20vertical-align()%3B%0A%7D%0A” message=”html align code” highlight=”” provider=”manual”/]

  • vertically center style
[pastacode lang=”css” manual=”%23box%0A%7B%0A%20%20display%3A%20table-cell%3B%0A%20%20vertical-align%3A%20middle%3B%0A%20%20height%3A%2090px%3B%0A%20%20width%3A%20270px%3B%0A%20%20background%3A%20%23000%3B%0A%20%20font-size%3A%2048px%3B%0A%20%20font-style%3A%20oblique%3B%0A%20%20color%3A%20%23FFF%3B%0A%20%20text-align%3A%20center%3B%0A%20%20margin-top%3A%2020px%3B%0A%20%20margin-left%3A%205px%3B%0A%7D%0A%3Cdiv%20Id%3D%22box%22%3E%0A%20%20Lorem%20ipsum%20dolor%20sit%20amet%2C%20consectetur%20adipiscing%20elit.%0A%3C%2Fdiv%3E%0A” message=”html align code” highlight=”” provider=”manual”/]
  • This article provides some of the basic informations on css vertical align , css align , text align , div align center , vertical , align center , html align , vertically center div , html align text , vertical align middle css , html text align center , css image , table align center css , vertical and horizontal , horizontal and vertical , html image center.

Categorized in: