{"id":3361,"date":"2017-11-14T17:58:44","date_gmt":"2017-11-14T17:58:44","guid":{"rendered":"http:\/\/w3bits.com\/?p=3361"},"modified":"2021-09-17T12:18:04","modified_gmt":"2021-09-17T12:18:04","slug":"css-absolute-centering","status":"publish","type":"post","link":"https:\/\/w3bits.com\/css-absolute-centering\/","title":{"rendered":"A Guide to Absolute Centering in CSS"},"content":{"rendered":"<p>Centering things in CSS has always been quite a task for the front-end developers.<\/p>\n<p>And how about centering both horizontally and vertically within a parent element? It&#8217;s a pain when you don&#8217;t know how to do that correctly. <\/p>\n<p>Centering something or a group of things vertically and horizontally is usually called as absolute centering in CSS. This article covers some nice ways to achieve that with some implementation examples.<\/p>\n<p>Let me give you some use cases first, where we generally feel the need of CSS absolute centering:<\/p>\n<ul>\n<li>Icons in CTA Buttons<\/li>\n<li>Prompt or <a href=\"https:\/\/w3bits.com\/javascript-modal\/\" target=\"_blank\" rel=\"noopener noreferrer\">alert boxes<\/a><\/li>\n<li>Structural elements (eg. layout grid, grid cells)<\/li>\n<li>Navigation components, eg. list items<\/li>\n<li>Spinners and loaders<\/li>\n<\/ul>\n<p>If you are doing all the above mentioned thing the inline-block or table way, you are doing it wrong. Let&#8217;s now jump into the centering techniques without any delay.<\/p>\n<hr>\n<h2>Flexbox!<\/h2>\n<p>No explanation is needed, when it comes to aligning things with CSS flexbox properties. With the CSS flexbox module, you have a great control over aligning elements and not just the centering, but you can do a lot more things with properties like <code>justify-content<\/code>, <code>align-items<\/code>, <code>align-content<\/code>, and <code>align-self<\/code>.<\/p>\n<p>First off, you need to apply <code>display: flex<\/code> to the wrapping division (or parent) as shown below:<\/p>\n<pre><code>.box {\r\n  display: flex;\r\n}<\/code><\/pre>\n<p>And here are the code snippets for basic centered alignments along with the demonstration links:<\/p>\n<h3>Horizontal Centering<\/h3>\n<p>By default, the <code>flex-direction<\/code> is set to <code>row<\/code>, and supplying the <code>center<\/code> value for <code>justify-content<\/code> will make the content inside the flexbox aligned centered horizontally.<\/p>\n<pre><code>.box {\r\n  justify-content: center;\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/flexbox-centering\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<h3>Vertical Centering<\/h3>\n<p>For vertical-centering, the <code>center<\/code> value to <code>align-items<\/code> will align the flexbox content centered vertically.<\/p>\n<pre><code>.box {\r\n  align-items: center;\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/flexbox-centering\/2\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<h3>Absolute Centering<\/h3>\n<p>And as you might have guessed already, using the value <code>center<\/code> for both <code>justify-content<\/code> and <code>align-items<\/code> will make the flexbox content aligned at absolute center of the box.<\/p>\n<pre><code>.box {\r\n  justify-content: center;\r\n  align-items: center;\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/flexbox-centering\/3\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<hr>\n<h2>Grid<\/h2>\n<p>Of course, for fallbacks, you may consider inline-block based middling wherever needed, but I have one more technique that seems much more handy than them.<\/p>\n<hr>\n<h2>Hacking the <code>position<\/code><\/h2>\n<p>This technique is not required anymore, since you have flexbox working just perfectly for you. But! You always need a fallback to the modern CSS properties if your job required to make things compatible to older browsers as well!<\/p>\n<p>The trickery lies in:<\/p>\n<ol>\n<li>Giving a top and\/or left offset (50% of the total space available) to the absolutely positioned element. It&#8217;s the element which we want to appear aligned vertically\/horizontally\/absolutely centered.<\/li>\n<li>The element will now be starting from 50% horizontal and\/or vertical coordinate(s) onwards of the total available space, which is of course, not the horizontal\/vertical\/absolute center of the parent division.<\/li>\n<li>In order to make it look centered, we can make some shift to the element to little on top and\/or left side; that shift should be equal to half the width and\/or height of the element. What comes to your mind to make these shifting with CSS? I think CSS transform property is all we need here.<\/li>\n<\/ol>\n<figure>\n<img decoding=\"async\" src=\"https:\/\/w3bits.com\/wp-content\/uploads\/css-position-absolute-centering.jpg\" alt=\"CSS Position-based Centering\"><figcaption>CSS Position-based Centering<\/figcaption><\/figure>\n<p>A small <em>pitfall<\/em> of this technique is, you might need to provide a height at times when you need the parent element to be obvious enough.<\/p>\n<p>Starting off with making our parent or wrapping element relatively positioned:<\/p>\n<pre><code>.box {\r\n  position: relative;\r\n}<\/code><\/pre>\n<h3>Horizontal centering<\/h3>\n<p>Horizontal centering can be obtained by placing the object to 50% left inside the box, and then shifting it to left half of it&#8217;s total width.<\/p>\n<pre><code>.box .object {\r\n  position: absolute;\r\n  left: 50%;\r\n  transform: translateX(-50%);\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/css-position-based-centering\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<h3>Vertical centering<\/h3>\n<p>Vertical centering is achieved by giving a 50% to the <code>top<\/code> inside the box, and then shifting it up half of it&#8217;s total height.<\/p>\n<pre><code>.box .object {\r\n  position: absolute;\r\n  top: 50%;\r\n  transform: translateY(-50%);\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/css-position-based-centering\/2\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<h3>Horizontal Centering<\/h3>\n<p>And obviously, placing at 50% from top and left inside the box, the object is required to be shifted left half of it&#8217;s total width and up half ot it&#8217;s total height.<\/p>\n<pre><code>.box .object {\r\n  position: absolute;\r\n  top: 50%;\r\n  left: 50%;\r\n  transform: translate(-50%, -50%);\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/w3bits.com\/labs\/css-position-based-centering\/2\/\" class=\"button\" target=\"_blank\" rel=\"noopener noreferrer\">See the Demo<\/a><\/p>\n<p>That&#8217;s it. You may however experiment using relatively positioned child elements too in the last technique I mentioned, but using absolute child inside a relatively positioned parent gives you more control as it doesn&#8217;t messes up with the flow anymore.<\/p>\n<p>Let me know what you think about this. Thanks for your time :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.<\/p>\n","protected":false},"author":1,"featured_media":3401,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8,21],"class_list":["post-3361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-css","tag-flexbox"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Guide to Absolute Centering in CSS<\/title>\n<meta name=\"description\" content=\"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/w3bits.com\/css-absolute-centering\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Guide to Absolute Centering in CSS\" \/>\n<meta property=\"og:description\" content=\"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/w3bits.com\/css-absolute-centering\/\" \/>\n<meta property=\"og:site_name\" content=\"W3Bits\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-14T17:58:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-17T12:18:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"725\" \/>\n\t<meta property=\"og:image:height\" content=\"315\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rahul\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@w3bits_\" \/>\n<meta name=\"twitter:site\" content=\"@w3bits_\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rahul\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/\",\"url\":\"https:\/\/w3bits.com\/css-absolute-centering\/\",\"name\":\"A Guide to Absolute Centering in CSS\",\"isPartOf\":{\"@id\":\"https:\/\/w3bits.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg\",\"datePublished\":\"2017-11-14T17:58:44+00:00\",\"dateModified\":\"2021-09-17T12:18:04+00:00\",\"author\":{\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac\"},\"description\":\"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.\",\"breadcrumb\":{\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/w3bits.com\/css-absolute-centering\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage\",\"url\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg\",\"contentUrl\":\"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg\",\"width\":725,\"height\":315,\"caption\":\"CSS Absolute Centering\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/w3bits.com\/css-absolute-centering\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/w3bits.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Guide to Absolute Centering in CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/w3bits.com\/#website\",\"url\":\"https:\/\/w3bits.com\/\",\"name\":\"W3Bits\",\"description\":\"Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/w3bits.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac\",\"name\":\"Rahul\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/w3bits.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g\",\"caption\":\"Rahul\"},\"description\":\"I'm Rahul, a web enthusiast and the person responsible for all the content you read here on W3Bits.\",\"url\":\"https:\/\/w3bits.com\/author\/meadminrahul9\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Guide to Absolute Centering in CSS","description":"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/w3bits.com\/css-absolute-centering\/","og_locale":"en_US","og_type":"article","og_title":"A Guide to Absolute Centering in CSS","og_description":"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.","og_url":"https:\/\/w3bits.com\/css-absolute-centering\/","og_site_name":"W3Bits","article_published_time":"2017-11-14T17:58:44+00:00","article_modified_time":"2021-09-17T12:18:04+00:00","og_image":[{"width":725,"height":315,"url":"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg","type":"image\/jpeg"}],"author":"Rahul","twitter_card":"summary_large_image","twitter_creator":"@w3bits_","twitter_site":"@w3bits_","twitter_misc":{"Written by":"Rahul","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/w3bits.com\/css-absolute-centering\/","url":"https:\/\/w3bits.com\/css-absolute-centering\/","name":"A Guide to Absolute Centering in CSS","isPartOf":{"@id":"https:\/\/w3bits.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage"},"image":{"@id":"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage"},"thumbnailUrl":"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg","datePublished":"2017-11-14T17:58:44+00:00","dateModified":"2021-09-17T12:18:04+00:00","author":{"@id":"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac"},"description":"A guide to absolute centering techniques in CSS, using modern approach as well as covering fallbacks for older browesers.","breadcrumb":{"@id":"https:\/\/w3bits.com\/css-absolute-centering\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/w3bits.com\/css-absolute-centering\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/w3bits.com\/css-absolute-centering\/#primaryimage","url":"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg","contentUrl":"https:\/\/w3bits.com\/wp-content\/uploads\/css-absolute-centering.jpg","width":725,"height":315,"caption":"CSS Absolute Centering"},{"@type":"BreadcrumbList","@id":"https:\/\/w3bits.com\/css-absolute-centering\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/w3bits.com\/"},{"@type":"ListItem","position":2,"name":"A Guide to Absolute Centering in CSS"}]},{"@type":"WebSite","@id":"https:\/\/w3bits.com\/#website","url":"https:\/\/w3bits.com\/","name":"W3Bits","description":"Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/w3bits.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/w3bits.com\/#\/schema\/person\/be0e5389728f04fca3db34bad77ef1ac","name":"Rahul","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/w3bits.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e574a987fbce499dc42961a23e1dfd9bfddadff03a41086b4eea64154d266b29?s=96&d=mm&r=g","caption":"Rahul"},"description":"I'm Rahul, a web enthusiast and the person responsible for all the content you read here on W3Bits.","url":"https:\/\/w3bits.com\/author\/meadminrahul9\/"}]}},"_links":{"self":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/3361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/comments?post=3361"}],"version-history":[{"count":24,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/3361\/revisions"}],"predecessor-version":[{"id":4826,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/posts\/3361\/revisions\/4826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/media\/3401"}],"wp:attachment":[{"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/media?parent=3361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/categories?post=3361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3bits.com\/wp-json\/wp\/v2\/tags?post=3361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}