{"id":3563,"date":"2017-11-17T07:09:30","date_gmt":"2017-11-17T07:09:30","guid":{"rendered":"http:\/\/freshinbox.com\/blog\/?p=3563"},"modified":"2017-12-01T05:41:43","modified_gmt":"2017-12-01T05:41:43","slug":"changing-the-stacking-order-of-elements-without-z-index","status":"publish","type":"post","link":"https:\/\/freshinbox.com\/blog\/changing-the-stacking-order-of-elements-without-z-index\/","title":{"rendered":"Changing the Stacking Order of Elements Without z-index"},"content":{"rendered":"<p>I recently learned that its possible to change the stacking order of elements without using z-index. This can be done by using other CSS properties to create what is called a new &#8220;stacking context&#8221;.<\/p>\n<p>This article goes into the details of <a href=\"http:\/\/vanseodesign.com\/css\/css-stack-z-index\/\" target=_new>stacking context<\/a> and how certain CSS properties affect them.<\/p>\n<h4>Z-Index in Email<\/h4>\n<p>The problem about using z-index to change the stacking order in email is that <a href=\"https:\/\/freshinbox.com\/resources\/css.php\" target=_new>few clients support it<\/a>. This is because for the most part it requires the use of the positioning property which is not supported by the majority of email clients &#8211; especially the Web based ones with the exception of AOL Mail. Yahoo! Mail actually supports relative positioning which we will get to later.<\/p>\n<p>Interestingly we can use other CSS properties to &#8220;bump&#8221; elements that appear under other elements in the DOM above them by creating a new stacking context.<\/p>\n<h4>Creating a New Stacking Context<\/h4>\n<p>In the code below, you can see that the blue box overlaps the red box because the red box appears earlier in the DOM in the natural stacking order. <\/p>\n<pre class=\"lang:default decode:true \" >&lt;div style=\"height:50px;\"&gt;\r\n  &lt;div style=\"width:100px;height:100px;background-color:red;\"&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;div style=\"width:100px;height:100px;\r\n  background-color:blue;margin-left:50px;\"&gt;&lt;\/div&gt;<\/pre>\n<div style=\"height:50px;\">\n<div style=\"width:100px;height:100px;background-color:red;\"><\/div>\n<\/div>\n<div style=\"margin-left:50px;width:100px;height:100px;background-color:blue;\"><\/div>\n<p><BR>By adding any the following properties to the red box, we create a new stacking context for the red box making the red box appear above the blue box.<\/p>\n<pre class=\"lang:default decode:true \" >- display: inline-block\r\n- float\r\n- opacity\r\n- CSS transform\r\n- position (absolute\/relative)<\/pre>\n<p>For example just by adding opacity:0.999 to the red box, the red box will appear above the blue box.<\/p>\n<pre class=\"lang:default mark:2 decode:true \" >&lt;div style=\"height:50px;\"&gt;\r\n  &lt;div style=\"width:100px;height:100px;background-color:red;opacity:0.999\"&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;div style=\"width:100px;height:100px;\r\n  background-color:blue;margin-left:50px;\"&gt;&lt;\/div&gt;\r\n<\/pre>\n<div style=\"height:50px;\">\n<div style=\"width:100px;height:100px;background-color:red;opacity:0.999\"><\/div>\n<\/div>\n<div style=\"margin-left:50px;width:100px;height:100px;background-color:blue;\"><\/div>\n<p><BR><\/p>\n<h4>What this means for email<\/h4>\n<p>Since CSS properties are not uniformly supported by all email clients we have to apply multiple properties to an element to create a new stacking context depending on the client. For most instances, adding display: inline-block to one element is sufficient to achieve this. However what if both elements already have display: inline-block? You can override that by adding another CSS property such as opacity in Gmail or position:relative in Yahoo! Mail.<\/p>\n<h4>Stacking order quirk with scrolling containers in Chrome<\/h4>\n<p>There is one peculiar quirk with creating a new stacking order when it comes to containers that contain a scrollbar when it comes to Chrome &#8211;  display: inline-block does not work as it should. If the blue container contains a scrollbar, applying display: inline-block does not cause the red box to appear above the blue box. In this case we&#8217;re forced to use the other properties such as opacity or position.<\/p>\n<p>This Chrome quirk extends to the Android based clients such as Gmail for Android and the Samsung Galaxy client. Safari based browsers including iOS such as Gmail for iOS do not manifest this display: inline-block issue.<\/p>\n<h4>New stacking context example<\/h4>\n<p>Here&#8217;s an example that uses the new stacking context that I built for Email on Acid. This example had an avatar walking through a landscape of monsters.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.emailonacid.com\/images\/blog_images\/Emailology\/2017\/2017_halloween_scroll\/halloween-scroll.gif\" width=\"274\"><\/p>\n<p><a href=\"https:\/\/marketing.emailonacid.com\/happy-halloween\" target=\"_new\">View the Halloween email here<\/a><br \/>\n<a href=\"https:\/\/www.emailonacid.com\/blog\/article\/email-development\/how-we-created-out-interactive-scrolling-halloween-email\" target=\"_new\">Read the article explaining the techniques behind the email.<\/a><\/p>\n<p><BR><BR><\/p>\n<div style=\"padding:10px; border: 2px solid #99C731;background-color:#FFF8DC;border-radius:5px;\">\n<form action=\"https:\/\/freshinbox1.createsend.com\/t\/i\/s\/adrmi\/\" method=\"post\" id=\"subForm\">\n<span class=\"fisub_header\" style=\"font-weight:bold;\">Subscribe to the #EmailGeeks Newsletter<\/span>\n<p><input id=\"fieldEmail\" name=\"cm-adrmi-adrmi\" type=\"email\" style=\"width:90%\" required placeholder=\"Your Email Address\" \/><\/p>\n<p><button type=\"submit\">Subscribe<\/button><\/p>\n<\/form>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I recently learned that its possible to change the stacking order of elements without using z-index. This can be done by using other CSS properties to create what is called a new &#8220;stacking context&#8221;&#8230;<\/p>\n","protected":false},"author":2,"featured_media":3571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,1],"tags":[],"_links":{"self":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/3563"}],"collection":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/comments?post=3563"}],"version-history":[{"count":9,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions"}],"predecessor-version":[{"id":3572,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions\/3572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/media\/3571"}],"wp:attachment":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/media?parent=3563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/categories?post=3563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/tags?post=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}