{"id":311,"date":"2008-12-17T17:36:58","date_gmt":"2008-12-17T22:36:58","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=311"},"modified":"2008-12-17T17:36:58","modified_gmt":"2008-12-17T22:36:58","slug":"advanced-css-accordion-effect","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/advanced-css-accordion-effect\/","title":{"rendered":"Advanced CSS Accordion Effect"},"content":{"rendered":"<p><a href=\"http:\/\/cssnewbie.com\/example\/css-only-accordion\/advanced.html\"><img decoding=\"async\" loading=\"lazy\" width=\"579\" height=\"200\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/12\/accordion-main.gif\" alt=\"css accordion technique\" title=\"css accordion technique\" class=\"size-full wp-image-312\" \/><\/a><\/p>\n<p>A while back, I wrote an article demonstrating how the popular \u201caccordion\u201d effect (as in the image above) could be <a href=\"http:\/\/cssnewbie.com\/css-only-accordion\/\">replicated with nothing more than CSS.<\/a> There was one caveat, however: the technique didn\u2019t work in Internet Explorer 6 due to the browser\u2019s extremely limited support of the :hover pseudo-element. <\/p>\n<p>Now, IE6\u2019s market share is decreasing day by day \u2013 and we\u2019re all undoubtedly thankful. But the browser will continue to maintain a healthy market share for quite some time to come. To that end, I\u2019d like to revisit the CSS accordion technique and make a modification or two that will let it work with IE6.<\/p>\n<p>I\u2019m going to admit up front: these modifications require JavaScript. So what\u2019s the point of having a \u201cCSS\u201d accordion technique that uses JavaScript? Well, all those other accordion techniques out there <em>require<\/em> JavaScript, and almost all require an entire JavaScript framework. This technique is merely <em>improved<\/em> by JavaScript \u2013 all modern browsers will handle this technique just fine even without JS enabled.<!--more--><\/p>\n<h3>The Original Markup<\/h3>\n<p>We\u2019re going to have to make a couple of teensy modifications to the CSS later on, but I wanted to show the original markup for consistency\u2019s sake. Here\u2019s the XHTML we originally used:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;div id=&quot;accordion&quot;&gt;\n\t&lt;div id=&quot;part1&quot;&gt;\n\t\t&lt;p&gt;This text is in part 1.&lt;\/p&gt;\n\t&lt;\/div&gt;\n\t&lt;div id=&quot;part2&quot;&gt;\n\t\t&lt;p&gt;This text is in part 2.&lt;\/p&gt;\n\t&lt;\/div&gt;\n\t&lt;div id=&quot;part3&quot;&gt;\n\t\t&lt;p&gt;This text is in part 3.&lt;\/p&gt;\n\t&lt;\/div&gt;\n\t&lt;div id=&quot;part4&quot;&gt;\n\t\t&lt;p&gt;This text is in part 4.&lt;\/p&gt;\n\t&lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n<p>And here is the CSS:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#accordion {\n\twidth: 500px;\n\tmargin: 100px auto; }\n#accordion div {\n\tfloat: left;\n\twidth:25%;\n\theight: 300px;\n\toverflow: hidden;}\n#accordion:hover div {\n\twidth: 20px; }\n#accordion:hover div:hover {\n\twidth: 440px;\n\toverflow: auto; }<\/pre>\n<p>If you\u2019d like to better understand how all of this works, <a href=\"http:\/\/cssnewbie.com\/css-only-accordion\/\">please refer to the original article.<\/a><\/p>\n<h3>Better Living Through JavaScript<\/h3>\n<p>The idea on how to improve this script came to me one day while I was going through <a href=\"http:\/\/cssnewbie.com\/toc\/\">the CSS Newbie archives<\/a> (yes, I have to refer to my own archives to remember how to do things some days!). I was rereading my article on <a href=\"http:\/\/cssnewbie.com\/easy-css-dropdown-menus\/\">Easy CSS Dropdown Menus<\/a> and I realized that the same JavaScript that allows my dropdown menus to be hover-able (that is, <a href=\"http:\/\/htmldog.com\/articles\/suckerfish\/dropdowns\/\">the Suckerfish technique<\/a>) would work just as well on my CSS accordion. Here\u2019s that code modified to effect the accordion:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">accHover = function() {\n\tvar accContainer = document.getElementById(&quot;accordion&quot;);\n\taccContainer.onmouseover=function() {\n\t\tthis.className+=&quot; hover&quot;;\n\t}\n\taccContainer.onmouseout=function() {\n\t\tthis.className=this.className.replace(new RegExp(&quot; hover\\b&quot;), &quot;&quot;);\n\t}\n\t\n\tvar accDivs = accContainer.getElementsByTagName(&quot;div&quot;);\n\tfor (var i=0; i&lt;accDivs.length; i++) {\n\t\taccDivs[i].onmouseover=function() {\n\t\t\tthis.className+=&quot; hover&quot;;\n\t\t}\n\t\taccDivs[i].onmouseout=function() {\n\t\t\tthis.className=this.className.replace(new RegExp(&quot; hover\\b&quot;), &quot;&quot;);\n\t\t}\n\t}\t\n}\nif (window.attachEvent) window.attachEvent(&quot;onload&quot;, accHover);<\/pre>\n<p>Basically, this code loops through all the divs within the accordion, as well as the containing accordion div itself, and applies a mouseover and mouseout function to each. Then when the user mouses over the div, it gains a class of \u201chover\u201d. When the user moves the mouse away from the div, the hover class is removed. <\/p>\n<p>Now all we need to do is edit our CSS to look for either the hover pseudo-class <em>or<\/em> our new hover class, which is made easy with <a href=\"http:\/\/cssnewbie.com\/combating-classitis\/\">sequential selectors:<\/a><\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#accordion {\n\twidth: 500px;\n\tmargin: 100px auto; }\n#accordion div {\n\tfloat: left;\n\twidth:25%;\n\theight: 300px;\n\toverflow: hidden;}\n#accordion:hover div, #accordion.hover div {\n\twidth: 20px; }\n#accordion:hover div:hover, #accordion.hover div.hover {\n\twidth: 440px;\n\toverflow: auto; }<\/pre>\n<h3>Even Easier with jQuery<\/h3>\n<p>Those of you who have been reading for a while know that I\u2019m a big fan of the jQuery JavaScript framework. Thus, when I started to rewrite this script to work for our accordion, I decided to rewrite it in jQuery as well. For those of you who aren\u2019t currently using  jQuery, this script might be a bit overkill, as it\u2019d require your users to download a lot more code in the long run (our tiny script plus the jQuery framework). But if you\u2019re already using jQuery, this cuts our heavy lifting down by quite a bit:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(document).ready(function() {\n\t$(\"#accordion, #accordion div\").mouseover(function() {\n\t\t$(this).addClass(\"hover\");\n\t}).mouseout(function() {\n\t\t$(this).removeClass(\"hover\");\n\t});\n});<\/pre>\n<p>This script does the same thing as our longer JavaScript above, but with far less code. <\/p>\n<p>So that\u2019s all there is to it! <a href=\"http:\/\/cssnewbie.com\/example\/css-only-accordion\/advanced.html\">You can see a demo of this in action here.<\/a> The accordion should function just fine in all major browsers with our without JavaScript enabled, and will function in IE6 with JavaScript. Enjoy!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>A while back, I wrote an article demonstrating how the accordion effect could be replicated with nothing more than CSS. There was one caveat, however: the technique didn\u2019t work in Internet Explorer 6 due to its limited support of :hover. Today I&#8217;m revisiting the CSS accordion technique and will make a modification or two that will let it work with IE6. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/advanced-css-accordion-effect\/\" title=\"Click to read 'Advanced CSS Accordion Effect'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[165,184],"tags":[200,79,283,51,54,55,291],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/311"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}