{"id":354,"date":"2017-03-14T13:41:03","date_gmt":"2017-03-14T13:41:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=354"},"modified":"2017-03-29T17:54:43","modified_gmt":"2017-03-29T12:24:43","slug":"how-vertically-center-text-css","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/how-vertically-center-text-css\/","title":{"rendered":"[10 Solved Answer]How to vertically center text with CSS"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM<\/label><\/p>\n<p>To align the text contents of this div vertically center.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">css align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">#box<br\/>{<br\/>  height: 90px;<br\/>  width: 270px;<br\/>  background: #001;<br\/>  font-size: 48px;<br\/>  font-style: oblique;<br\/>  color: #FFF;<br\/>  text-align: center;<br\/>  margin-top: 21px;<br\/>  margin-left: 5px;<br\/>}<br\/>&lt;div Id=&quot;box&quot;&gt;<br\/>  Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>This approach can only works for a single line of text<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">css align code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">div<br\/>{<br\/>  height: 90px;<br\/>  line-height: 90px;<br\/><br\/>  text-align: center;<br\/>  border: 2px dashed #f69c55;<br\/>}<br\/>&lt;div&gt;<br\/>  Hello World!<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n<ul>\n<li>solution for a single line and multiple lines of text<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">css align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">div <br\/>{<br\/>  height: 200px;<br\/>  line-height: 200px;<br\/>  text-align: center;<br\/>  border: 2px dashed #f69c55;<br\/>}<br\/>span <br\/>{<br\/>  display: inline-block;<br\/>  vertical-align: middle;<br\/>  line-height: normal;<br\/>}<br\/>&lt;div&gt;<br\/>  &lt;span&gt;Welcome to Wikitechy. &lt;\/span&gt;<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>Use the below CSS code<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">text align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">display: table-cell;<br\/>vertical-align: middle;<br\/> finally CSS looks like:<br\/>#box <br\/>{<br\/>  height: 90px;<br\/>  width: 270px;<br\/>  background: #001;<br\/>  font-size: 48px;<br\/>  font-style: oblique;<br\/>  color: #FFF;<br\/>  text-align: center;<br\/>  margin-top: 20px;<br\/>  margin-left: 5px;<br\/>  display: table-cell;<br\/>  vertical-align: middle;<br\/>}<br\/>&lt;div id=&quot;box&quot;&gt;<br\/>  Some text&lt;\/div&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>Flexible approach for vertical alignment:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">text align code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">div <br\/>{<br\/>  width: 250px;<br\/>  min-height: 50px;<br\/>  line-height: 50px;<br\/>  text-align: center;<br\/>  border: 1px solid #123456;<br\/>  margin-bottom:6px;<br\/>}<br\/>span <br\/>{<br\/>  display: inline-block;<br\/>  vertical-align: middle;<br\/>  line-height: normal;<br\/>}<br\/>&lt;div&gt;<br\/>  &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;br \/&gt;<br\/>    Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;br \/&gt;<br\/>   Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;\/span&gt;<br\/>&lt;\/div&gt;<br\/>&lt;div&gt;<br\/> &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;\/span&gt;<br\/>&lt;\/div&gt;<br\/>&lt;div&gt;<br\/>  &lt;span&gt;Lorem ipsum dolor sit amet.&lt;\/span&gt;<br\/>&lt;\/div&gt;<br\/>&lt;div&gt;<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>For reference and simpler answer for vertically center text:<\/li>\n<li>Pure CSS:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">text align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">vertical-align <br\/>{<br\/>    position: relative;<br\/>    top: 50%;<br\/>    -webkit-transform: translateY(-50%);<br\/>    -ms-transform: translateY(-50%);<br\/>    transform: translateY(-50%);<br\/>}<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">div align center<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">Or as a SASS\/SCSS Mixin:<br\/>@mixin vertical-align <br\/>{<br\/>    position: relative;<br\/>    top: 60%;<br\/>    -webkit-transform: translateY(-50%);<br\/>    -ms-transform: translateY(-50%);<br\/>    transform: translateY(-50%);<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li><strong>Use by:<\/strong><\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">div align center<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.class-to-center <br\/>{<br\/>    @include vertical-align;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>The element being placed on a \u201chalf pixel\u201d. A solution for this parent element to preserve-3d. Like following:<br \/>\n.parent-element<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">text align css code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">{<br\/>    -webkit-transform-style: preserve-3d;<br\/>    -moz-transform-style: preserve-3d;<br\/>    transform-style: preserve-3d;<br\/>}<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>For a single line of text<\/li>\n<li>It\u00a0can\u00a0be used when\u00a0#box has non-fixed,\u00a0relative height in %.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">text align css code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">&lt;div id=&quot;box&quot;&gt;&lt;\/div&gt;<br\/>#box::before<br\/>{<br\/>    display: block;<br\/>    content: &quot;&quot;;<br\/>    height: 50%;<br\/>}<br\/>#box::after <br\/>{<br\/>    vertical-align: top;<br\/>    line-height: 0;<br\/>    content: &quot;TextContent&quot;;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>If we need to place inner text in HTML, not in CSS, then we need to wrap text content in additional\u00a0inline\u00a0element and edit\u00a0 #box:: after to match it.<\/li>\n<\/ul>\n<p><strong>For example,<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">align center css<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;div id=&quot;box&quot;&gt;&lt;span&gt;TextContent&lt;\/span&gt;&lt;\/div&gt;<br\/>In this case #box::after should be replaced to #box span.<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>A very simple &amp; most powerful solution to vertically align center:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">align center css<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.outer-div <br\/>{<br\/> height: 100px;<br\/>  width: 200px;<br\/>  text-align: center;<br\/>  border: 1px solid #000;<br\/>}<br\/><br\/>.inner <br\/>{<br\/>  position: relative;<br\/>  top: 45%;<br\/>  transform: translateY(-45%);<br\/>  color: red;<br\/>}<br\/>&lt;div class=&quot;outer-div&quot;&gt;<br\/>  &lt;span class=&quot;inner&quot;&gt;No data available&lt;\/span&gt;<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<ul>\n<li>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.<\/li>\n<li>So we can write,<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">align center css<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.element <br\/>{<br\/>    position: relative;<br\/>    top: 50%;<br\/>   transform: translateY(-50%);<br\/>} <\/code><\/pre> <\/div>\n<ul>\n<li>It is a similar technique to the absolute-position method, but with the upside that we don\u2019t have to set any height on the element or position-property on the parent<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>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.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">html align code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">div<br\/>{<br\/>  height:120px;<br\/>  line-height: 120px;<br\/>}<br\/>Another way,<br\/>div <br\/>{<br\/>   padding: 60px 0;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>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.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<ul>\n<li>For all our vertical alignment<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">html align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">@mixin vertical-align($position: relative) <br\/>{<br\/>  position: $position;<br\/>  top: 60%;<br\/>  -webkit-transform: translateY(-50%);<br\/>  -ms-transform: translateY(-50%);<br\/>  transform: translateY(-50%);<br\/>}<br\/>Then include<br\/>.element<br\/>{<br\/>    @include vertical-align();<br\/>}<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<ul>\n<li>vertically center style<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">html align code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">#box<br\/>{<br\/>  display: table-cell;<br\/>  vertical-align: middle;<br\/>  height: 90px;<br\/>  width: 270px;<br\/>  background: #000;<br\/>  font-size: 48px;<br\/>  font-style: oblique;<br\/>  color: #FFF;<br\/>  text-align: center;<br\/>  margin-top: 20px;<br\/>  margin-left: 5px;<br\/>}<br\/>&lt;div Id=&quot;box&quot;&gt;<br\/>  Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br\/>&lt;\/div&gt;<\/code><\/pre> <\/div>\n<ul>\n<li>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.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM To align the text contents of this div vertically center. SOLUTION 1: This approach can only works for a single line of text solution for a single line and multiple lines of text SOLUTION 2: Use the below CSS code [ad type=&#8221;banner&#8221;] SOLUTION 3: Flexible approach for vertical alignment: SOLUTION 4: For reference and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,24],"tags":[1401,40,710,1398,1407,1397,728,729,713,709,1399,520,1410,722,714,723,717,1402,1404,1411,1406,1408,513,718,1400,712,727,1405,725,726,724,716,1409,715,719,721,1403,711,720],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-align-center","tag-center-text-in-div","tag-centering-in-css","tag-css-align","tag-css-image","tag-css-vertical-align","tag-css-vertical-align-div","tag-css-vertical-align-middle","tag-css-vertical-center","tag-css-vertical-align-property","tag-div-align-center","tag-horizontal-align-css","tag-horizontal-and-vertical","tag-how-to-align-to-middle-by-height","tag-how-to-center-in-css","tag-how-to-vertically-align-an-image-inside-div","tag-how-to-vertically-center-a-div-for-all-browsers","tag-html-align","tag-html-align-text","tag-html-image-center","tag-html-text-align-center","tag-table-align-center-css","tag-text-align","tag-true-center-vertical-and-horizontal-css-div","tag-vertical","tag-vertical-align-anything-with-just-3-lines-of-css","tag-vertical-align-div-inside-div","tag-vertical-align-middle-css","tag-vertical-align-middle-div","tag-vertical-align-middle-not-working","tag-vertical-align-text-in-div","tag-vertical-alignment-of-elements-in-a-div","tag-vertical-and-horizontal","tag-vertical-align","tag-vertical-align-with-inline-elements","tag-vertically-align-text-in-element-with-css","tag-vertically-center-div","tag-vertically-center-multi-lined-text","tag-vertically-center-text-in-an-absolutely-centered-sphere"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}