{"id":992,"date":"2014-04-25T13:45:49","date_gmt":"2014-04-25T20:45:49","guid":{"rendered":"http:\/\/www.cassandraking.net\/wordpressapp\/?p=992"},"modified":"2014-04-25T13:45:49","modified_gmt":"2014-04-25T20:45:49","slug":"css-tricks-to-center-absolutely-positioned-elements-of-variable-height","status":"publish","type":"post","link":"https:\/\/www.cassandraking.net\/wordpressapp\/css-tricks-to-center-absolutely-positioned-elements-of-variable-height\/","title":{"rendered":"CSS Tricks to Center Absolutely Positioned Elements of Variable Height"},"content":{"rendered":"<p><a href=\"http:\/\/www.cassandraking.net\/wordpressapp\/wp-content\/uploads\/2014\/04\/CSS.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-1000\" alt=\"CSS\" src=\"http:\/\/www.cassandraking.net\/wordpressapp\/wp-content\/uploads\/2014\/04\/CSS-150x150.png\" width=\"150\" height=\"150\" \/><\/a>These are some really clever solutions prompted by a discussion on the <a href=\"https:\/\/www.linkedin.com\/groups\/Front-End-Developers-3124573\">Front End Developers Group in LinkedIn<\/a><\/p>\n<p><strong>Question posed:<\/strong><\/p>\n<blockquote><p>Have you ever needed to centralize an absolute element (horizontal and vertical), but the height was variable?<\/p><\/blockquote>\n<p>Some really interesting solutions \u00c2\u00a0and the browser support for each one. I list a few of the tried and tested methods at the end.<\/p>\n<h2>Solution One:<\/h2>\n<p>On Code Pen:\u00c2\u00a0<a href=\"http:\/\/codepen.io\/LFeh\/pen\/LFHBG\">http:\/\/codepen.io\/LFeh\/pen\/LFHBG<\/a>.\u00c2\u00a0This is not100% supported in versions IE 8 and below, so use your best judgement:<\/p>\n<p><strong>the HTML:<\/strong><\/p>\n<pre><span style=\"color: #008000;\">\r\n&lt;div class=\"center centerALL\"&gt;\r\n&lt;div class=\"inner\"&gt;You can also position your element only in the vertical or horizontal. This work in IE6+&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n<\/span><\/pre>\n<p><strong>the CSS:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #339966;\">\/\/ Translate mixin\r\n\/\/ ------------------------------\r\n.translate(@x; @y) {\r\n  -webkit-transform: translate(@x, @y);\r\n      -ms-transform: translate(@x, @y); \r\n       -o-transform: translate(@x, @y);\r\n          transform: translate(@x, @y);\r\n}\r\n\r\n\/\/ Center-align mixin\r\n\/\/ ------------------------------\r\n\r\n\/\/ Center full\r\n.center-full() {\r\n  position: absolute;\r\n  top: 50%; left: 50%;\r\n  .translate(-50%, -50%);\r\n}\r\n\r\n\/\/ Center top only\r\n.center-top() {\r\n  position: absolute;\r\n  top: 50%; \r\n  .translate(0, -50%);\r\n}\r\n\r\n\/\/ Block element\r\n\/\/ ------------------------------\r\n\r\n.box {\r\n  border: darken(#428bca, 10%) solid 1px;\r\n  .center-full();\r\n  color: #222;\r\n  font-size: 26px;\r\n  font-family: arial;\r\n  height: 50%;\r\n  padding: 20px;\r\n  width: 50%;\r\n\r\n  p {\r\n    .center-top();\r\n  }\r\n}<\/span><\/pre>\n<p>Basically, this behaves much like the table layout trick:<\/p>\n<pre><span style=\"color: #339966;\">position: absolute; #top: 50%;display: table-cell; vertical-align: middle;\"\r\n<\/span><\/pre>\n<h2>Solution Two:<\/h2>\n<p>This one is supported in both IE6 and IE8: <a href=\"http:\/\/codepen.io\/anon\/pen\/fGpBu\">http:\/\/codepen.io\/anon\/pen\/fGpBu<\/a><\/p>\n<p><strong>The HTML:<\/strong><\/p>\n<pre><span style=\"color: #339966;\">&lt;div class=\"center centerALL\"&gt;<\/span>\r\n<span style=\"color: #339966;\">&lt;div class=\"inner\"&gt;You can also position your element only in the vertical or horizontal. This work in IE6+&lt;\/div&gt;<\/span>\r\n<span style=\"color: #339966;\">&lt;\/div&gt;<\/span><\/pre>\n<p><strong>The CSS<\/strong>:<\/p>\n<pre><span style=\"color: #339966;\">body{\r\n                font-family:verdana;\r\n            }\r\n            .center{\r\n                height:50%;\r\n                width:50%;\r\n                background-color: gainsboro;\r\n                position:absolute;\r\n                display:table;\r\n            }\r\n            .centerV {\r\n                top : 25%;\r\n            }\r\n            .centerO {\r\n                left:25%;\r\n            }\r\n            .centerALL{\r\n                top:25%;\r\n                left:25%;\r\n            }\r\n            .center .inner{\r\n                padding:50px;\r\n                display:table-cell;\r\n                vertical-align:middle;\r\n            }<\/span><\/pre>\n<h2>Other solutions:<\/h2>\n<ul>\n<li><a style=\"font-size: 13px;\" href=\"http:\/\/css-tricks.com\/snippets\/css\/absolute-center-vertical-horizontal-an-image\/\">Absolute Center (Vertical &amp; Horizontal) an Image by CSS<\/a><span style=\"font-size: 13px;\"> guru Chris Coyier.<\/span><\/li>\n<li><span style=\"font-size: 13px;\">A solution (for XHTMl) can be found here: <\/span><a style=\"font-size: 13px;\" href=\"Absolutely Centered Layout  Absolute Centering with CSS &amp; (X)HTML\">http:\/\/perishablepress.com\/absolutely-centered-layout\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>These are some really clever solutions prompted by a discussion on the Front End Developers Group in LinkedIn Question posed: Have you ever needed to centralize an absolute element (horizontal and vertical), but the height was variable? Some really interesting solutions \u00c2\u00a0and the browser support for each one. I list a few of the tried [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1000,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[29,1],"tags":[232,268,231,234,230,233],"_links":{"self":[{"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/posts\/992"}],"collection":[{"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/comments?post=992"}],"version-history":[{"count":8,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/posts\/992\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/posts\/992\/revisions\/1001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/media\/1000"}],"wp:attachment":[{"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/media?parent=992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/categories?post=992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cassandraking.net\/wordpressapp\/wp-json\/wp\/v2\/tags?post=992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}