{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">HTML 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\">&lt;div&gt;&lt;\/div&gt;<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">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\">div {<br\/> width: 100px;<br\/> height: 100px;<br\/> background-color: red;<br\/><br\/> position: absolute;<br\/> top:0;<br\/> bottom: 0;<br\/> left: 0;<br\/> right: 0;<br\/><br\/> margin: auto;<br\/>}c<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">HTML 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\">&lt;div&gt;Welcome to wikitechy.com&lt;\/div&gt;<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">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\">div {<br\/> background:red;<br\/> position:absolute;<br\/> color:#fff;<br\/> top:50%;<br\/> left:50%;<br\/> padding:15px;<br\/> -ms-transform: translateX(-50%) translateY(-50%);<br\/> -webkit-transform: translate(-50%,-50%);<br\/> transform: translate(-50%,-50%);<br\/>} <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">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\">.middleDiv {<br\/> position : absolute; <br\/> width : 200px;<br\/> height : 200px;<br\/> left : 50%;<br\/> top : 50%;<br\/> margin-left : -100px; \/* half of the width *\/<br\/> margin-top : -100px; \/* half of the height *\/<br\/>}<\/code><\/pre> <\/div>\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}]}}