{"id":5293,"date":"2011-02-21T20:21:36","date_gmt":"2011-02-21T19:21:36","guid":{"rendered":"https:\/\/www.creativejuiz.fr\/blog\/en\/?p=5293"},"modified":"2015-08-05T20:51:32","modified_gmt":"2015-08-05T18:51:32","slug":"advanced-drop-shadows-with-css3","status":"publish","type":"post","link":"https:\/\/www.creativejuiz.fr\/blog\/en\/tutorials\/advanced-drop-shadows-with-css3","title":{"rendered":"Advanced drop shadows with CSS3"},"content":{"rendered":"<p><abbr>I use the name &#8220;CSS<\/abbr>3&#8243; only for its &#8220;commercial&#8221; aspect. Actually, we will use my two favorite pseudo-elements <code>:after<\/code> and <code>:before<\/code> which are available since CSS2.1. I mean they are available since a long time ago\u2026 Demos you see in the <a title=\"CSS3 Box-Shadow sur :before et :after\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/css3-box-shadow-after-before\" hreflang=\"en\">demo page<\/a> are composed with only one unique <code>&lt;div&gt;<\/code> and some CSS properties.<\/p>\n<p><!--more--><\/p>\n<p>For me, the best rendering is made with Firefox browser, but you should see those advanced shadows on all modern browsers, so use the one ou prefer.<\/p>\n<p>In this <a title=\"CSS3 Box-Shadow sur :before et :after\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/css3-box-shadow-after-before\" hreflang=\"en\">demonstration<\/a> I will use some advanced CSS properties (<code>transform<\/code>, <code>box-shadow<\/code>) directly on <code>:before<\/code> and <code>:after<\/code> pseudo-elements. Those pseudo-elements allow to build a kind of &#8220;shadow-dow&#8221; inside the existing DOM (Document Object Model). It&#8217;s an element built with CSS, or a sort of\u2026 Lets do it!<\/p>\n<h2><code>:after<\/code> and <code>:before<\/code> pseudo-elements<\/h2>\n<p>Imagine: in your website, just after a link, you want to add the language of the resource you linked to. (reminder: this information is given by the <code>hreflang<\/code> attribute in a <code>&lt;a&gt;<\/code> element). You can do it using those some lines of CSS:<\/p>\n<pre class=\"code\"><code class=\"language-css\">a[hreflang]:after {\r\n\tcontent : \" (\"attr(hreflang)\")\";\r\n}<\/code><\/pre>\n<p>With that code, all the links with the <code>hreflang<\/code> attribute will be ended by &#8220;(en)&#8221;, in the case of an english reference:<br \/>\n<a id=\"testouille\" href=\"#testing\">My link (en)<\/a><\/p>\n<h2>The working basis<\/h2>\n<p>Under the same principle, we will use two pseudo-element to built several styles of dropped shadows, the same you can analyze in the <a title=\"CSS3 Box-Shadow sur :before et :after\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/css3-box-shadow-after-before\" hreflang=\"en\">demo page<\/a>.<\/p>\n<p>As base of HTML, we have a single <code>div<\/code> element:<\/p>\n<pre class=\"code\"><code class=\"language-markup\">&lt;div class=\"block\"&gt;Without drop shadow&lt;\/div&gt;<\/code><\/pre>\n<p>This <code>div<\/code> element has a <code>block<\/code>, class value which will visually create the block (some basic styles for the demo). The <code>without<\/code> class in the first block is just here to define the block without any shadow. No big deal.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1067\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-simple-bloc.jpg\" alt=\"\" width=\"291\" height=\"178\" \/><\/p>\n<p>You will find the corresponding CSS below. The main properties are those about positioning and dimensions. It&#8217;s not mandatory to fix dimension of the block, here is for the demo. So, the styles:<\/p>\n<pre class=\"code\"><code class=\"language-css\">.block {\r\n\tposition: relative;\r\n\tdisplay: inline-block;\r\n\tvertical-align: top;\r\n\tmargin: 35px 26px;\r\n\tpadding: 25px;\r\n\twidth: 220px;\r\n\theight: 110px;\r\n\tbackground: #f0e8d8;\r\n\tfont-size: 1.6em;\r\n\tline-height: 5em;\r\n\tfont-family: sans-serif;\r\n\ttext-align: center;\r\n\tbackground-image: linear-gradient(to bottom, #dacdb1 11%, #f0e8d8 56%);\r\n\tbox-shadow: 0 0 65px #cdbe9f inset, 0 0 20px #beae8c inset;\r\n}<\/code><\/pre>\n<p>Then, we build our pseudo-elements for all the cases. It&#8217;s important not forget the empty <code>content<\/code> property.<\/p>\n<pre class=\"code\"><code class=\"language-css\">.block:after,\r\n.block:before {\r\n\tcontent: \"\";\r\n\tposition: absolute;\r\n\twidth: 50%;\r\n\theight: 100px;\r\n\tz-index: -10;\r\n}\r\n\r\n.block:before { background: green;}\r\n.block:after { background: red; }\r\n<\/code><\/pre>\n<p>Pseudo-blocks have an absolute position and a negative <code>z-index<\/code>, the goal is to place those elements under their parent. (the <code>&lt;div&gt;<\/code> they depend)<\/p>\n<p>Colors of pseudo-elements are added to understand how it works, how those elements are positioned, rotated, etc. I had planned to invite you to use <a title=\"Fabuleux plugin de Joe Hewitt (nouvelle fen\u00eatre)\" href=\"https:\/\/addons.mozilla.org\/fr\/firefox\/addon\/firebug\/\" target=\"_blank\">Firebug<\/a> to play with the <code>.block<\/code> background, but thanks to a suggestion by <a href=\"https:\/\/twitter.com\/PhilippeVay\">Philippe<\/a>, I developed a plaything to watch those pseudo-elements without effort. :p<\/p>\n<p>But let us come back to the topic.<\/p>\n<p>Pseudo-blocks are ready, we just have to add them a drop shadow and a precise positioning. With some cases, we will need other properties, we will see later together.<\/p>\n<p>The following codes will be applied to all elements that have the <code>block<\/code> class combined to the other class associated. In other words: one class by type of shadow.<br \/>\nFor example, for the next effect, the HTML element need those classes:<\/p>\n<pre class=\"code\"><code class=\"language-markup\">&lt;div class=\"block simple\"&gt;Soft Drop Shadow&lt;\/div&gt;<\/code><\/pre>\n<p class=\"message note\">Some CSS3 properties have to be prefixed (-webkit-, -moz-, etc.), depending on the browsers support you need, and the browsers evolutions.<\/p>\n<h2>Simple shadow<\/h2>\n<p>It&#8217;s never too early to remind basis about <code>box-shadow<\/code> usage.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1070\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-box-shadow.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<pre class=\"code\"><code class=\"language-css\">.simple {\r\n\tbox-shadow: 0 0 65px #cdbe9f inset, 0 0 20px #beae8c inset, 0 0 5px #816f47;\r\n}\r\n\/* we don't need pseudo-elements this time *\/\r\n.simple:after,\r\n.simple:before {\r\n\tdisplay: none;\r\n}<\/code><\/pre>\n<p>Here, we don&#8217;t need pseudo-elements, so we hide them.<\/p>\n<p>We have the same <code>box-shadow<\/code> code than the base, but we added another shadow to the list. As you can see, we can combine inset shadow and drop shadow.<\/p>\n<p>The basic syntax:<\/p>\n<pre class=\"code\"><code class=\"language-css\">box-shadow: 1px 2px 9px 0 #000;<\/code><\/pre>\n<p>allow to create a box shadow, with those parameters, in the order:<br \/>\n<code>1px<\/code> of horizontal move (to the right), <code>2px<\/code> of vertical move (to the bottom), <code>9px<\/code> of blur, <code>0<\/code> of spread (optional), <code>#000<\/code> for the shadow color.<br \/>\nAn optional parameter, the keyword <code>inset<\/code>, put at the end of the shadow value, makes the shadow inner instead of generating a drop shadow. Without this parameter, the shadow is a classical drop shadow.<\/p>\n<p>This set of parameters can be duplicated and comma separated to create multiple shadow.<\/p>\n<h2 id=\"lateral-shadows\">Lateral shadows<\/h2>\n<p>This is how we will really use our pseudo-element to create advanced effects.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1072\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-lateral-shadows.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>We will put our pseudo-blocks in a certain position, and give them dimensions and &#8220;personal&#8221; shadow. Let&#8217;s see!<\/p>\n<pre class=\"code\"><code class=\"language-css\">.laterals:before {\r\n\ttop: 15px;\r\n\tleft: 0;\r\n\twidth: 100%;\r\n\theight: 130px;\r\n\t\r\n\tborder-radius: 12px \/ 65px;\r\n\tbox-shadow: 0 0 10px #555;\r\n}\r\n.laterals:after { display: none; }\r\n<\/code><\/pre>\n<p>The <code>border-radius<\/code> property add a curled effect in our shadows. It&#8217;s more realist.<br \/>\nDon&#8217;t forget to use the X-Ray feature to <a title=\"Aller \u00e0 la page de d\u00e9monstration\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/css3-box-shadow-after-before\/index.html\" hreflang=\"en\">watch pseudo-element<\/a> in action.<\/p>\n<h2 id=\"curved-lateral-shadow\">Curved lateral shadow<\/h2>\n<p>Simple but not too simple. I mean, it&#8217;s simple if you get in mind that there is only one shadow in one side.<br \/>\nSo! This time, again, we will use only one of both pseudo-elements.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1074\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-lateral-single-shadow.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>We have to use a rotation to give the shadow its effect.<\/p>\n<pre class=\"code\"><code class=\"language-css\">.lateral:before {\r\n\ttop: 10px;\r\n\tleft: 8px;\r\n\twidth: 50%;\r\n\theight: 130px;\r\n\t\r\n\tbox-shadow: -7px 0 10px #555;\r\n\ttransform: rotate(-5deg);\r\n}\r\n.lateral:after { display: none; }\r\n<\/code><\/pre>\n<p>You may see the negative value of box-shadow property. This value moves the shadow to the left to make it more visible below the block.<\/p>\n<h2>Hozizontal shadows<\/h2>\n<p>It&#8217;s the same principle than <a href=\"#lateral-shadows\">lateral shadows<\/a>, but this time I use the both of pseudo-elements. In fact, it&#8217;s rare to know in advance the height of an item. Using <code>:before<\/code> and <code>:after<\/code> to make this effect allows me to have a dynamic-height item.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1076\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-horizontal-shadows.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>The first pseudo-element is positionned regarding to the top of the block, the second one regarding to the bottom.<\/p>\n<pre class=\"code\"><code class=\"language-css\">.horizontal:before {\r\n\ttop: 0px;\r\n\tleft: 5%;\r\n\twidth: 90%;\r\n\theight: 50px;\r\n\t\r\n\tborder-radius: 125px \/ 12px;\r\n\tbox-shadow: 0 0 8px #555;\r\n}\r\n.horizontal:after { \r\n\tbottom: 0px;\r\n\tleft: 5%;\r\n\twidth: 90%;\r\n\theight: 50px;\r\n\t\r\n\tborder-radius: 125px \/ 12px;\r\n\tbox-shadow: 0 0 8px #555;\r\n}<\/code><\/pre>\n<p>I volontarily duplicated styles to the both declarations, but you can merge those duplicated properties in a single declaration to be more maintainable. It&#8217;s up to you.<\/p>\n<h2>Simple horizontal shadow<\/h2>\n<p>It&#8217;s the more complex part of that article, I think.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1077\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-horizontal-shadow.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>By adding <code>single<\/code> class to our element:<\/p>\n<pre class=\"code\"><code class=\"language-markup\">&lt;div class=\"block horizontal single\"&gt;Single horizontal&lt;\/div&gt;<\/code><\/pre>\n<p>and applying this CSS code (in addition of our previous effect):<\/p>\n<pre class=\"code\"><code class=\"language-css\">.single:before { display: none; }<\/code><\/pre>\n<p>It&#8217;s not a big deal, indeed \ud83d\ude09<br \/>\nOf course, instead of creating styles for <code>:before<\/code> and hide it, we can also just create styles for <code>:after<\/code> and hide <code>:before<\/code> directly.<\/p>\n<h2>Curved shadows<\/h2>\n<p>It was (and it is, maybe?) a trendy effect to add a touch of depth in some web designs.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1079\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-curved-shadows.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>To make this effect, we will use our both pseudo-elements, and add a rotation to them, like the <a href=\"#curved-lateral-shadow\">Curved Lateral Shadow<\/a> seen previously.<\/p>\n<pre class=\"code\"><code class=\"language-css\">.curve:before {\r\n\ttop: 52px;\r\n\tleft: 5px;\r\n\r\n\ttransform: rotate(-5deg);\r\n\tbox-shadow: 7px 6px 15px #333;\r\n}\r\n.curve:after {\r\n\ttop: 52px;\r\n\tright: 5px;\r\n\t\r\n\ttransform: rotate(5deg);\r\n\tbox-shadow: -7px 6px 15px #333;\r\n}<\/code><\/pre>\n<p>Too easy!<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1080\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-curved-shadow.jpg\" alt=\"\" width=\"299\" height=\"184\" \/><\/p>\n<p>With the same principle, you can hide one of both shadows to create this more simple effect. Use the\u00a0 <code>single<\/code> class, seen previously, or just don&#8217;t style one or the other pseudo-elements.<\/p>\n<h2>More experimental shadow<\/h2>\n<p>So! This one, the X-Ray feature will not help you, not this time.<br \/>\nI use entirely the both pseudo-elements, not only their <code>box-shadow<\/code> property.<\/p>\n<p class=\"center\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1082\" style=\"border: 0;\" title=\"\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-experimental-shadows.jpg\" alt=\"\" width=\"306\" height=\"205\" srcset=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-experimental-shadows.jpg 306w, https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2011\/02\/css3-experimental-shadows-300x200.jpg 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/p>\n<p>The corresponding CSS code is bigger than the previous we made, but this experimentation is good to help you during your front-end work \ud83d\ude42<\/p>\n<pre class=\"code\"><code class=\"language-css\">.drop:before {\r\n\tleft: -5px;\r\n\ttop: 167px;\r\n\twidth: 280px;\r\n\theight: 4px;\r\n\tbackground: #aaa;\r\n\r\n\tborder-radius: 140px \/ 2px;\r\n\tbox-shadow: 0 0 5px #aaa, 0 0 10px #888, 0 0 15px #666;\r\n}\r\n\r\n.drop:after {\r\n\tleft: 8px;\r\n\ttop: 168px;\r\n\twidth: 255px;\r\n\theight: 2px;\r\n\tbackground: #666;\r\n\t\r\n\tborder-radius: 125px \/ 1px;\r\n\tbox-shadow: 0 0 5px #444, 0 0 8px #333, 0 0 10px #666;\r\n}<\/code><\/pre>\n<p>To be more efficient, you could (or should) use <code>rgba()<\/code> instead of hexadecimal colors.<\/p>\n<p>The <code>:before<\/code> pseudo-element is used for the large shadow.<br \/>\nThe <code>:after<\/code> pseudo-element is used for the fine shadow,\u00a0 at the center of this complex large shadow.<br \/>\nIn both cases, we need to transform them into <a title=\"Tutoriel sur les ellipses en CSS3, par Geoffrey Crofte, sur Alsacreations.com (nouvelle fen\u00eatre)\" href=\"http:\/\/www.alsacreations.com\/astuce\/lire\/979-ovale-forme-elliptique-css3-sans-image.html\" target=\"_blank\">ellipses (fr)<\/a> (french article, but understandable code).<\/p>\n<h2>Bugs<\/h2>\n<p>If it happens shadow not showing up, you may be in the following case: the elements affected by the shadows are in another parent element. In other words, you have a container that containing the elements you tried to make shadowy.<br \/>\nIf that container has a background (color or image), the shadows become non-visible.<\/p>\n<p>It&#8217;s a <code>z-index<\/code> issue, you have to reorder it.<br \/>\nIn our example, you just have to put the <code>position<\/code> property to <code>relative<\/code> (or other value like <code>fixed<\/code> or <code>absolute<\/code>, depending on your layout) and a <code>z-index<\/code> with <code>0<\/code> as value:<\/p>\n<pre class=\"code\"><code class=\"language-css\">.container {\r\n   position: relative;\r\n   z-index: 0;\r\n}<\/code><\/pre>\n<h2>The ending word<\/h2>\n<p>I hope this tutorial was easy to follow for you. Some explainations are short, but if you have question, the comment form is there for you.<br \/>\nThe X-Ray feature should help you to find some answers, just click it!<\/p>\n<p>This topic about advanced drop shadows is also available here <a title=\"Article sur Drop Shadow en CSS3 (nouvelle fen\u00eatre)\" href=\"http:\/\/nimbupani.com\/drop-shadows-with-css3.html\" target=\"_blank\">Nimbupani<\/a> and there <a title=\"D\u00e9monstration de Nicolas Gallagher (nouvelle fen\u00eatre)\" href=\"http:\/\/nicolasgallagher.com\/css-drop-shadows-without-images\/demo\/\" target=\"_blank\" hreflang=\"en\">NicolasGallagher<\/a>.<\/p>\n<p>Thank you for your reading!<\/p>\n<div class=\"sources\">\n<h2>Useful links<\/h2>\n<ul>\n<li><a title=\"CSS3 Box-Shadow sur :before et :after\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/css3-box-shadow-after-before\" hreflang=\"en\">The demo page<\/a><\/li>\n<li><a title=\"Le labo de Creative Juiz\" href=\"http:\/\/www.creativejuiz.fr\/trytotry\/\" hreflang=\"en\">The TryToTry researches<\/a><\/li>\n<li><a title=\"Article sur Drop Shadow en CSS3 (nouvelle fen\u00eatre)\" href=\"http:\/\/nimbupani.com\/drop-shadows-with-css3.html\" target=\"_blank\">A demo at Nimbupani<\/a><\/li>\n<li><a title=\"D\u00e9monstration de Nicolas Gallagher (nouvelle fen\u00eatre)\" href=\"http:\/\/nicolasgallagher.com\/css-drop-shadows-without-images\/demo\/\" target=\"_blank\" hreflang=\"en\">Other examples by Nicolas Gallagher<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I use the name &#8220;CSS3&#8243; only for its &#8220;commercial&#8221; aspect. Actually, we will use my two favorite pseudo-elements :after and :before which are available since CSS2.1. I mean they are available since a long time ago\u2026 Demos you see in the demo page are composed with only one unique &lt;div&gt; and some CSS properties.<\/p>\n","protected":false},"author":4,"featured_media":1065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bluesky_dont_syndicate":"","_bluesky_syndication_accounts":"","_bluesky_syndication_text":"","footnotes":""},"categories":[652,650,649,647],"tags":[],"coauthors":[597],"class_list":["post-5293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css3","category-development","category-graphism","category-tutorials"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/5293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/comments?post=5293"}],"version-history":[{"count":0,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/5293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media\/1065"}],"wp:attachment":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media?parent=5293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/categories?post=5293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/tags?post=5293"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/coauthors?post=5293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}