HTML Tutorials, tips & tricks.
In: CSS for advanced
22 Feb 2012With the new CSS3 animation functions we can now create a pure css3 accordion without any javascript at all, I assume you already know the transition functions because we are going to use it for the slide effect.
Here is the DEMO, I have used a touch of Metro UI because I like the general ideea of simplicity and functionality
Our accordion will be cross browser compatible (the animation works on all browsers, including IE 10+ but not lower versions). So, let’s skip to the tutorial part.
First, the HTML markup, we are going to use divs, dl and dd tag (if you don’t know what dl and dd tag is, you can see the definition in the HTML tags section)
<div class="accordionContainer"> <span id="tab1"></span> <span id="tab2"></span> <span id="tab3"></span> <div class="tabContainer"> <dl class="tab1"> <dd> <a href="#tab1">This is the 1st tab</a> <div><p>CSS3 delivers a wide range of stylization and effects, enhancing the web app without sacrificing your semantic structure or performance. Additionally Web Open Font Format (WOFF) provides typographic flexibility and control far beyond anything the web has offered before.</p></div> </dd> </dl> <dl class="tab2"> <dd> <a href="#tab2">This is the 2nd tab</a> <div> <p> <iframe width="560" height="309" src="http://www.youtube.com/embed/l3yAx2uCoHs" frameborder="0" allowfullscreen></iframe> </p> </div> </dd> </dl> <dl class="tab3"> <dd> <a href="#tab3">This is the 3rd tab</a> <div><p>Giving meaning to structure, semantics are front and center with HTML5. A richer set of tags, along with RDFa, microdata, and microformats, are enabling a more useful, data driven web for both programs and your users.</p></div> </dd> </dl> </div> </div>
So, as you can see, we used the definition list (dl) tag to along with a class attached as a wrapper, and then the definition description (dd) wich holds the actual tab along with it’s content.
Now, the CSS part, wich is a little bit longer.
*{ margin:0; padding:0; } body { font-family: 'Segoe UI', Arial, Tahoma, sans-serif; background: #fafafa; } header { margin: 20px auto; width: 500px; text-align: center; } .content { margin:0 auto; width:900px; } .accordionContainer { color: #000000; position: relative; width: 600px; margin: 0 auto; } .accordionContainer span { display: none } .tabContainer { background-color: #FFFFFF; overflow: hidden; } .tabContainer dl dd a { background-color: #4d99f6; color: #fff; display: block; font-size: 18px; line-height: 30px; padding: 10px 15px; text-decoration: none; -webkit-transition: 0.4s; -moz-transition: 0.4s; -ms-transition: 0.4s; -o-transition: 0.4s; transition: 0.4s; } .tabContainer dl dd div { background-color: #f0f0f0; height: 0; overflow: hidden; -webkit-transition: all 0.4s ease-in-out; -moz-transition: all 0.4s ease-in-out; -o-transition: all 0.4s ease-in-out; -ms-transition: all 0.4s ease-in-out; transition: all 0.4s ease-in-out; } .tabContainer dl dd div p { color: #444444; font-size: 12px; padding: 20px; } .tabContainer dl dd a:hover { background: #4082d3; } .tabContainer dl dd a:active { background: #3975be; } #tab1:target ~ .tabContainer .tab1 dd div { height: 80px; } #tab2:target ~ .tabContainer .tab2 dd div { height: 345px; } #tab3:target ~ .tabContainer .tab3 dd div { height: 80px; } #tab1:target ~ .tabContainer .tab1 dd a, #tab2:target ~ .tabContainer .tab2 dd a, #tab3:target ~ .tabContainer .tab3 dd a { background: #3975be; }
As a side note, you must use fixed height for each tab class in order to preserve the height & the animation of the tabs, also this according is compatible with IE, but the animation won’t work. I hope you learned something from this tutorial.
You can also download this css3accordion with css3.
With the new CSS3 attributes, we can now create gradient backgrounds without using any kind of images at all, only pure CSS, at the end of this tutorial you will achieve the following box, wich is cross-browser compatbile, like Internet Explorer 10, Opera 11.10, Chrome and Firefox wich is pretty nice that all the browsers starts to adopt the new standards, so the result:
Let’s get started.
First, the HTML part, wich is pretty simple, only a div with some text inside of it.
<div class="gradient">This box have a gradiented background</div>
Now, the CSS3 part where we will create the actual gradient (notice that I used a class on the div, not an ID, but works with both).
.gradient { /* For Chrome 10+ */ background: -webkit-linear-gradient(right, #000, #cc0000); /* For Firefox 3.6+ */ background: -moz-linear-gradient(right, #000, #cc0000); /* For Internet Explorer 10+ */ background: -ms-linear-gradient(right, #000, #cc0000); /* For Opera 11.10+ */ background: -o-linear-gradient(right, #000, #cc0000); height: 100px; width: 100px; color: #fff; }
Now, let’s explain the code, before the linear-gradient function you can see the prefix of the browsers, like ‘webkit’, ‘ms’ and so on, you must specify that for each browser as shown in example, now what’s inside the brackets works like this, the first argument is from wich side will start the linear gradient, in our case from right, the next values are the colors, first color is black, wich will start from right, and the second it’s a nice red.
This is a problem across all the browsers, happens when a div inside another div have the the float attribute, wich will remove any height from it.
The fix is really simple, just use the overflow attribute, and set it to auto to get it right.
#main_div { overflow: auto; /*This will fix the height problem*/ }
In this tutorial I’m going to show you how to change the background color of the text when you select a text using ::selection attribute. The attribute is cross-browser compatible, so it works perfect on Chrome, Internet Explorer, Firefox and other CSS3 browsers.
Code
::selection { background:#DD4B39; color:#fff; } ::-moz-selection { background:#DD4B39; color:#fff; } ::-webkit-selection { background:#DD4B39; color:#fff; }
The text resulted will look like this: Text while is selected.
In: CSS Properties
8 Mar 2011The white-space property specifies how the white-space inside an element should be handled.
Example
p { white-space:nowrap; }
In: CSS Properties
8 Mar 2011The vertical-align property specifies the vertical position of an inline container. It have several possible values, some of them are: baseline, sup, super, top, middle, bottom, etc.
Example
h1 { vertical-align: text-top; font-size: 10px; }
In: CSS Properties
8 Mar 2011The text-indent property specifies the indentation of the first line of text in a block container.
Example
p { text-indent: 1.5em; }
In: CSS Properties
8 Mar 2011The text-transform property specifies if the text will be uppercased, lowercased, capitalized or normal.
Example
h2 { text-transform: uppercase }
Recent Comments