{"id":23569,"date":"2015-07-24T10:25:39","date_gmt":"2015-07-24T04:55:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23569"},"modified":"2015-07-24T13:48:46","modified_gmt":"2015-07-24T08:18:46","slug":"different-ways-for-aligning-an-element-vertically-center-in-html-using-css","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/different-ways-for-aligning-an-element-vertically-center-in-html-using-css\/","title":{"rendered":"Different Ways for aligning an element Vertically-Center in HTML using CSS"},"content":{"rendered":"<p>Aligning an element the way we want is never an easy task, especially when a non-UI developer is doing it and this problem worsens when we try to align that element Vertically-Center. Although doing it using patches is not difficult, but doing it the right way is a challenge.<\/p>\n<p>Here you\u2019ll find some cool CSS techniques by which you can easily Vertically-Center align any element.<\/p>\n<p>Below find the html snippet:<\/p>\n<p>[java]<br \/>\n&lt;div class=&quot;parent&quot;&gt;<\/p>\n<p>    &lt;div class=&quot;child&quot;&gt;Content&lt;\/div&gt;<\/p>\n<p>&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p>Given below are the CSS protocols, which can be applied to align the child element in the center of the parent element.<\/p>\n<p><strong>Table Method<\/strong><\/p>\n<p>[java]<br \/>\n.parent {<\/p>\n<p>\tdisplay:table;<\/p>\n<p>}<\/p>\n<p>.child {<\/p>\n<p>\tdisplay:table-cell;<br \/>\n\tvertical-align:middle;<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p><strong>Flexbox Layout Method<\/strong><\/p>\n<p>[java]<br \/>\n.parent {<\/p>\n<p>\tdisplay: -webkit-flexbox;<br \/>\n \tdisplay: -ms-flexbox;<br \/>\n\tdisplay: -webkit-flex;<br \/>\n\tdisplay: flex;<br \/>\n\t-webkit-flex-align: center;<br \/>\n  \t-ms-flex-align: center;<br \/>\n  \t-webkit-align-items: center;<br \/>\n \talign-items: center;<br \/>\n  \ttext-align: center; <\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p><strong>Translate() Method<\/strong><\/p>\n<p>[java]<br \/>\n.parent {<\/p>\n<p>\tposition: relative;<\/p>\n<p>}<\/p>\n<p>.child {<\/p>\n<p>   position: absolute;<br \/>\n   left: 50%;<br \/>\n   top: 50%;<br \/>\n   margin-left: -25%;<br \/>\n   margin-top: -25%;<br \/>\n   transform: translate(-50%, -50%);<br \/>\n   width: 40%;<br \/>\n   height: 50%;<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p><strong>Absolute Positioning and Stretching Method<\/strong><\/p>\n<p>[java]<br \/>\n.parent {<\/p>\n<p>\tposition: relative;<\/p>\n<p>}<\/p>\n<p>.child {<\/p>\n<p>   \tposition: absolute;<br \/>\n    top: 0;<br \/>\n    bottom: 0;<br \/>\n    left: 0;<br \/>\n    right: 0;<br \/>\n    width: 50%;<br \/>\n    height: 30%;<br \/>\n    margin: auto;<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>I have tried my best to keep it as simple as possible, and it should solve your purpose that too conventionally.<\/p>\n<p>Cheers!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aligning an element the way we want is never an easy task, especially when a non-UI developer is doing it and this problem worsens when we try to align that element Vertically-Center. Although doing it using patches is not difficult, but doing it the right way is a challenge. Here you\u2019ll find some cool CSS [&hellip;]<\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[355,4842],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23569"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23569"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23569\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}