{"id":98,"date":"2017-03-11T04:52:14","date_gmt":"2017-03-11T04:52:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=98"},"modified":"2017-10-15T13:38:54","modified_gmt":"2017-10-15T08:08:54","slug":"best-way-to-center-a-on-a-page-vertically-and-horizontally","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/best-way-to-center-a-on-a-page-vertically-and-horizontally\/","title":{"rendered":"[Solved \u2013 3 Answers] Best way to center a &lt;div&gt; on a page vertically and horizontally?"},"content":{"rendered":"<p><strong><span class=\"label label-warning\">PROBLEM :<\/span><\/strong><\/p>\n<p>The problem is,<\/p>\n<p>Best way to center a &lt;div&gt; element on a page both vertically and horizontally?<\/p>\n<p>Basically, margin-left: auto; margin-right: auto; will center on the horizontal, but what is the best way to do it vertically, too?<\/p>\n<p><span class=\"label label-info\">SOLUTION 1:\u00a0<\/span><\/p>\n<p>This solution is the best and most flexible way for\u00a0center a div ( css center div)<\/p>\n<p>You can check the demo on dabblet.com<\/p>\n<p>The main trick in this demo is that in the normal flow of elements going from top to bottom, so the margin-top: auto is set to zero. However, an absolutely positioned element acts the same for distribution of free space, and similarly can be centered vertically at the specified top and bottom (does not work in IE7).<\/p>\n<p>This trick will work with any sizes of div.<\/p>\n[pastacode lang=&#8221;markup&#8221; manual=&#8221;%3Cdiv%3E%3C%2Fdiv%3E&#8221; message=&#8221;HTML Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;css&#8221; manual=&#8221;div%20%7B%0A%20width%3A%20100px%3B%0A%20height%3A%20100px%3B%0A%20background-color%3A%20red%3B%0A%0A%20position%3A%20absolute%3B%0A%20top%3A0%3B%0A%20bottom%3A%200%3B%0A%20left%3A%200%3B%0A%20right%3A%200%3B%0A%0A%20margin%3A%20auto%3B%0A%7Dc&#8221; message=&#8221;CSS Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><span class=\"label label-info\">SOLUTION 2:\u00a0<\/span><br \/>\nIt is the another Simplest technique to center a div ( css center div) (This method has its implications though, but if you only need to center element regardless of flow of the rest of the content, it&#8217;s just fine.)<\/p>\n<p><strong>Markup:<\/strong><\/p>\n[pastacode lang=&#8221;markup&#8221; manual=&#8221;%3Cdiv%3EWelcome%20to%20wikitechy.com%3C%2Fdiv%3E&#8221; message=&#8221;HTML Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;css&#8221; manual=&#8221;div%20%7B%0A%20background%3Ared%3B%0A%20position%3Aabsolute%3B%0A%20color%3A%23fff%3B%0A%20top%3A50%25%3B%0A%20left%3A50%25%3B%0A%20padding%3A15px%3B%0A%20-ms-transform%3A%20translateX(-50%25)%20translateY(-50%25)%3B%0A%20-webkit-transform%3A%20translate(-50%25%2C-50%25)%3B%0A%20transform%3A%20translate(-50%25%2C-50%25)%3B%0A%7D%20%0A&#8221; message=&#8221;CSS Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>This will center element horizontally and vertically too. No negative margins, just power of transforms.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><span class=\"label label-info\">SOLUTION 3:\u00a0 <\/span><\/p>\n<p>In this solution, there are two ways to to center a div ( css center div) through CSS.<\/p>\n[pastacode lang=&#8221;css&#8221; manual=&#8221;.middleDiv%20%7B%0A%20position%20%3A%20absolute%3B%20%0A%20width%20%3A%20200px%3B%0A%20height%20%3A%20200px%3B%0A%20left%20%3A%2050%25%3B%0A%20top%20%3A%2050%25%3B%0A%20margin-left%20%3A%20-100px%3B%20%2F*%20half%20of%20the%20width%20*%2F%0A%20margin-top%20%3A%20-100px%3B%20%2F*%20half%20of%20the%20height%20*%2F%0A%7D&#8221; message=&#8221;CSS Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n","protected":false},"excerpt":{"rendered":"<p>[Solved &#8211; 3 Answers] HTML &#8211; css center div &#8211; Best way to center a element on a page both vertically and horizontally? Basically, margin-left:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83400,24],"tags":[3652,83407,37,83405,83403,3650,83401,83402,83404,83408,1399,83406,83410,83409,83412,83411,83413],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-css-2","category-html","tag-align-div-center-bootstrap","tag-bootstrap-div-center","tag-center-div-horizontally-and-vertically","tag-center-div-on-page-bootstrap","tag-center-div-on-page-horizontally","tag-center-div-on-page-vertically","tag-center-div-vertically-and-horizontally-bootstrap","tag-center-div-vertically-and-horizontally-inside-div","tag-center-div-vertically-and-horizontally-responsive","tag-css-div-center","tag-div-align-center","tag-div-center","tag-div-id-page","tag-div-plus-center","tag-div-page-unitbox","tag-divimp-home-page","tag-showpageindivtab"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/98","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}