Tuesday 17 September 2013

Theming JQuery UI Accordion

In this post we are going to create something real and functional, and we are going to make use of the structural selectorbox-sizing and CSS3 gradients from our previous lessons to create a custom theme for jQuery UI Accordion and sharpen our CSS skills.
Here is what the final result is going to look like:

HTML Markup

To begin with, let’s put the following div with the id accordion that will contain the entire accordion’s content.
  1. <div id="accordion">  
  2.     <!-- the content -->  
  3. </div>  
We will have five random sections of web-related topics for the accordion’s content, such asHTML5, CSS3Sass, Dreamweaver and jQuery. This is just an example; you can actually put anything for the content, as long as you keep the markup changed accordingly. Here is the markup based on these 5 topics.
  1. <h3><a href="#">HTML5</a></h3>  
  2. <div>  
  3.     <p>  
  4.         <strong>HTML5</strong> is a markup language for structuring and presenting content for the World Wide Web, and is a core technology of the Internet originally proposed by Opera Software. It is the fifth revision of the HTML standard (created in 1990 and standardized as HTML4 as of 1997) and, as of July 2012, is still under development.  
  5.     </p>  
  6. </div>  
  7. <h3><a href="#">CSS3</a></h3>  
  8. <div>  
  9.     <p>  
  10.         Unlike CSS 2, which is a large single specification defining various features, CSS 3 is divided into several separate documents called "modules". Each module adds new capabilities or extends features defined in CSS 2, over preserving backward compatibility. <em><strong>Source:</strong> <a href="http://en.wikipedia.org/wiki/Css">WikiPedia.org</a></em>  
  11.     </p>  
  12. </div>  
  13. <h3><a href="#">Sass</a></h3>  
  14. <div>  
  15.     <p>  
  16.         <strong>Sass</strong> makes CSS fun again. Sass is an extension of CSS3, adding nested rules, variables, mixins, selector inheritance, and more. It's translated to well-formatted, standard CSS using the command line tool or a web-framework plugin.  
  17.     </p>  
  18. </div>  
  19. <h3><a href="#">Dreamweaver</a></h3>  
  20. <div>  
  21.     <p>  
  22.         <strong>Adobe Dreamweaver</strong> (formerly Macromedia Dreamweaver) is a proprietary web development application originally created by Macromedia. It is now developed by Adobe Systems, which acquired Macromedia in 2005.  
  23.     </p>  
  24. </div>  
  25. <h3><a href="#">jQuery</a></h3>  
  26. <div>  
  27.     <p>  
  28.         <strong>jQuery</strong> is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig. Used by over 55% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.  
  29.     </p>  
  30. </div>  
Each section consists of an h3 that will become the section’s header and a div that will wrap the section’s content entirely, and then we put them inside the accordion div that we’ve created before.
At this point, we just work on the HTML markup so nothing will happen yet when we view it in the browser.

JQuery UI Accordion

Our accordion will be based on the jQuery UI, so we need both the jQuery and jQuery UI libraries to be included in the head tag and we will link those files directly from the CDN, as follows.
  1. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
  2. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>  
Then, put the following script after them.
  1. $(function() {  
  2.     $("#accordion").accordion();  
  3. });  
Now, the accordion should already work. When you click on the header, the content will expand, and it will collapse when we click on another header.
However, our accordion still looks plain white, so let’s add some styles to it.

Basic Styles

First, we need to reset all the default element styles to maintain style consistency across different browsers with Normalize.css, and then we add a background color in the html tag. We also remove the default underline from the link as well as the outline when it is in active or focus state.
  1. html {  
  2.     height: 100%;  
  3.     background#e3e3e0;  
  4. }  
  5. a {  
  6.     text-decorationnone;  
  7. }  
  8. :focus, :active {  
  9.     outline: 0;  
  10. }  
Further Reading: Reviewing Styles Priority
We will also apply border-box model for box-sizing in all elements, as follows:
  1. * {   
  2.     -moz-box-sizing: border-box;   
  3.     -webkit-box-sizing: border-box;   
  4.     box-sizing: border-box;  
  5. }  
Further Reading: CSS Box-sizing

Styling The Accordion

In this section we will start styling the accordion sections. First, we need to specify the accordiondiv container for 300px and center it.
  1. #accordion {  
  2.     width300px;  
  3.     margin50px auto;  
  4. }  
The accordion’s content will have a slightly white color while the text inside it will have a dark grey color #777, then we will also reduce the font size to 10pt.
  1. #accordion .ui-accordion-content {  
  2.     width: 100%;  
  3.     background-color#f3f3f3;  
  4.     color#777;  
  5.     font-size10pt;  
  6.     line-height16pt;  
  7. }  
The content is wrapped with a paragraph tag, this tag by default has a top and bottom margin, so here we will remove the margins and replace them with 20px of padding.
In case you did not wrap the content within the <p> tag or have other elements, rather than select specifically for p, we will select any direct elements from the .ui-accordion-content like this.
  1. #accordion .ui-accordion-content > * {  
  2.     margin: 0;  
  3.     padding20px;  
  4. }  
Also, if we have links in the content, we will turn the color to dark grey #777.
  1. #accordion .ui-accordion-content a {  
  2.     color#777;  
  3. }  
Next, we will style the accordion header; the accordion header is an h3 tag which by default has a top and bottom margin as well, so we need to remove them both.
  1. #accordion .ui-accordion-header {  
  2.     background-color#ccc;  
  3.     margin0px;  
  4. }  
We also need to put some styles to the anchor tag inside the header. We will turn the text on the anchor tag white, indent the text for 10px, and lastly reduce the font size to 12pt.
  1. #accordion .ui-accordion-header a {  
  2.     color#fff;  
  3.     line-height42px;  
  4.     displayblock;  
  5.     font-size12pt;  
  6.     width: 100%;  
  7.     text-indent10px;  
  8. }  
We will give each header a different color. This can be done by selecting each header with the structural selectors, and as for the first header, we can select it by using the :first-of-type;
  1. #accordion .ui-accordion-header:first-of-type {  
  2.     background-color#fa9300;  
  3.     background-image: -moz-linear-gradient(top,  #fa9300 0%, #dc621e 100%); /* FF3.6+ */  
  4.     background-image: -webkit-gradient(linear, left topleft bottombottomcolor-stop(0%,#fa9300), color-stop(100%,#dc621e)); /* Chrome,Safari4+ */  
  5.     background-image: -webkit-linear-gradient(top,  #fa9300 0%,#dc621e 100%); /* Chrome10+,Safari5.1+ */  
  6.     background-image: -o-linear-gradient(top,  #fa9300 0%,#dc621e 100%); /* Opera 11.10+ */  
  7.     background-image: -ms-linear-gradient(top,  #fa9300 0%,#dc621e 100%); /* IE10+ */  
  8.     background-image: linear-gradient(to bottombottom,  #fa9300 0%,#dc621e 100%); /* W3C */  
  9.     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fa9300', endColorstr='#dc621e',GradientType=0 ); /* IE6-9 */  
  10. }  
For the second to the fourth header we can select them using the :nth-of-type(n) selector, as follows;
  1. #accordion .ui-accordion-header:nth-of-type(2) {  
  2.     background-color#389abe;  
  3.     background-image: -moz-linear-gradient(top,  #389abe 0%, #2a7b99 100%); /* FF3.6+ */  
  4.     background-image: -webkit-gradient(linear, left topleft bottombottomcolor-stop(0%,#389abe), color-stop(100%,#2a7b99)); /* Chrome,Safari4+ */  
  5.     background-image: -webkit-linear-gradient(top,  #389abe 0%,#2a7b99 100%); /* Chrome10+,Safari5.1+ */  
  6.     background-image: -o-linear-gradient(top,  #389abe 0%,#2a7b99 100%); /* Opera 11.10+ */  
  7.     background-image: -ms-linear-gradient(top,  #389abe 0%,#2a7b99 100%); /* IE10+ */  
  8.     background-image: linear-gradient(to bottombottom,  #389abe 0%,#2a7b99 100%); /* W3C */  
  9.     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#389abe', endColorstr='#2a7b99',GradientType=0 ); /* IE6-9 */  
  10. }  
  11. #accordion .ui-accordion-header:nth-of-type(3) {  
  12.     background-color#f87aa0/* Old browsers */  
  13.     background-image: -moz-linear-gradient(top#f87aa0 0%, #c86585 100%); /* FF3.6+ */  
  14.     background-image: -webkit-gradient(linear, left topleft bottombottomcolor-stop(0%,#f87aa0), color-stop(100%,#c86585)); /* Chrome,Safari4+ */  
  15.     background-image: -webkit-linear-gradient(top#f87aa0 0%,#c86585 100%); /* Chrome10+,Safari5.1+ */  
  16.     background-image: -o-linear-gradient(top#f87aa0 0%,#c86585 100%); /* Opera 11.10+ */  
  17.     background-image: -ms-linear-gradient(top,  #f87aa0 0%,#c86585 100%); /* IE10+ */  
  18.     background-image: linear-gradient(to bottombottom,  #f87aa0 0%,#c86585 100%); /* W3C */  
  19.     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f87aa0', endColorstr='#c86585',GradientType=0 ); /* IE6-9 */  
  20. }  
  21. #accordion .ui-accordion-header:nth-of-type(4) {  
  22.     background-color#a8b700;  
  23.     background-image: -moz-linear-gradient(top,  #a8b700 0%, #82922a 100%);  
  24.     background-image: -webkit-gradient(linear, left topleft bottombottomcolor-stop(0%,#a8b700), color-stop(100%,#82922a)); /* Chrome,Safari4+ */  
  25.     background-image: -webkit-linear-gradient(top,  #a8b700 0%,#82922a 100%);   
  26.     background-image: -o-linear-gradient(top,  #a8b700 0%,#82922a 100%);  
  27.     background-image: -ms-linear-gradient(top,  #a8b700 0%,#82922a 100%);  
  28.     background-image: linear-gradient(to bottombottom,  #a8b700 0%,#82922a 100%);  
  29.     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a8b700', endColorstr='#82922a',GradientType=0 ); /* IE6-9 */  
  30. }  
… for the fifth or in this case the last header, we can select it by using the :last-of-type;
  1. #accordion .ui-accordion-header:last-of-type {  
  2.     background-color#b3bec4;  
  3.     background-image: -moz-linear-gradient(top,  #b3bec4 0%, #95a0a4 100%);  
  4.     background-image: -webkit-gradient(linear, left topleft bottombottomcolor-stop(0%,#b3bec4), color-stop(100%,#95a0a4));  
  5.     background-image: -webkit-linear-gradient(top,  #b3bec4 0%,#95a0a4 100%);  
  6.     background-image: -o-linear-gradient(top,  #b3bec4 0%,#95a0a4 100%);  
  7.     background-image: -ms-linear-gradient(top,  #b3bec4 0%,#95a0a4 100%);  
  8.     background-image: linear-gradient(to bottombottom,  #b3bec4 0%,#95a0a4 100%);  
  9.     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b3bec4', endColorstr='#95a0a4',GradientType=0 ); /* IE6-9 */  
  10. }  
Alright, now let’s have a look at the result for a while.
It starts looking good, however, you may have realized that we did not explain everything about the structural selector in details, so if you are confused about how this type of selector works, we suggest you read our previous post first, CSS3 First-of-type, and come back afterward.

Last Details

Let’s put more details for our accordion, as one pixel can make a lot difference in the final result.
  1. #accordion .ui-accordion-header a {  
  2.     text-shadow1px 1px 0px rgba(0,0,0,0.2);  
  3.     text-shadow1px 1px 0px rgba(0,0,0,0.2);  
  4.     border-right1px solid rgba(0, 0, 0, .2);  
  5.     border-left1px solid rgba(0, 0, 0, .2);  
  6.     border-bottom1px solid rgba(0, 0, 0, .2);  
  7.     border-top1px solid rgba(250, 250, 250, .2);  
  8. }  
In the snippet above, we added border-rightborder-leftborder-bottom for one pixel withrgba color mode.
The borders color are actually black, but since we lower the intensity to 20%, the color became transparent and produced a sort of darker-color-version of the background.
We also did the same for the border-top, but we apply a white color instead of black to create a sort of highlighting effect.
However, if you take a look at the header closely, the top side just doesn’t look right; it seems like the header has no border at the top, so here we will do a trick to mimic the border-top as we are not able to add border-top twice at a single rule set.
Here is how we do it. We add an inner shadow with a negative value for the vertical length, but since we don’t need this inner shadow for the last section, we will just remove it.
We also add another inner shadow but this shadow will appear at the top, so it will look like the header’s shadow.
  1. #accordion .ui-accordion-content {  
  2.     box-shadow: inset 0px -1px 0px 0px rgba(0, 0, 0, .4),  
  3.                 inset 0px 1px 1px 0px rgba(0, 0, 0, .2);  
  4. }  
  5. #accordion .ui-accordion-content:last-of-type {  
  6.     box-shadow: inset 0px 1px 1px 0px rgba(0, 0, 0, .2),  
  7.                 inset 0px 0 0px 0px rgba(0, 0, 0, .5);  
  8. }  

Internet Explorer

Last but not least, let’s don’t forget our friend Internet Explorer. The :nth-of-type selector and its kind currently is not supported in IE6 to IE8, so should you want better compatibility in IE, you can use Mootools and Selectivizr.js.
Put the following two lines, before the jQuery and jQuery UI:
  1. <!--[if lte IE 8]>  
  2. <script type="text/javascript" src="js/mootools.js"></script>  
  3. <script type="text/javascript" src="js/selectivizr.js"></script>  
  4. <![endif]-->  
Finally, we can now see the live demo and download the source of our accordion from the links below.

No comments:

Post a Comment