Usually, a FAQ page is that long page with lots of questions and answers, the one we are searching for when we need some extra info regarding a subject. So, for example, if you own a website that sells stuff, then you will need a page like that.
In this article I’ll show you how to create a fancy FAQ page using CSS3 only, no JavaScript.
The idea
When I visited Facebook’s Help Center section (theirs FAQ’s), I noticed a cool effect for the answers previews. They show a small, faded and clipped text preview for the answer, and then, when the question is clicked, the complete answer is revealed.
After seeing it, of course I immediately thought about how can I create a similar effect using CSS3 only. So, further you’ll see how I made it.
The HTML
We will start as usual with the markup structure:
<section class="faq-section"> <input type="checkbox" id="q1"> <label for="q1">Question?</label> <p>... The intro paragraph that will be clipped ...</p> <p>... Extra and optional paragraph ...</p> </section>
- In the above image, the label is the proper heading of the section. But, if you want to use better semantic, you can wrap the
label
into ah1
. - Using
label::before
allow us to create the right triangle shape. On a side note, double colon for pseudo-elements is the CSS3 way. - The first paragraph for each section is the intro preview for the complete answer. For this example, I used the adjacent sibling combinator to target it.
How it works?
There’s no rocket science here. The technique we will use today is called the checkbox hack and it relies on the ability of toggle-ing an <input type="checkbox" id="abc">
using the<label for="abc">
. Also, in the same time, the checkbox input will be hidden.
I played before with this cool technique, but never had the opportunity to create a practical example actually. So, this is my shot! :)
If you want to read more about this technique, Chris Coyier wrote a while ago an article where he shows some cool stuff you can do with the checkbox hack.
The CSS
Below you have the styles, I commented some lines for a better understanding:
/*Add some spacing*/ .faq-section{ margin: 40px 0; } /*Hide the checkboxes and paragraphs*/ .faq-section input, .faq-section p{ display: none; } /*Show only the clipped intro */ .faq-section label+p{ display: block; color: #999; font-size: .85em; transition: all .15s ease-out; /* Clipping text */ text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } /*If the checkbox is checked, show all paragraphs*/ .faq-section input[type=checkbox]:checked~p{ display: block; color: #444; font-size: 1em; /* restore clipping defaults */ text-overflow: clip; white-space: normal; overflow: visible; } /*Style the label*/ .faq-section label{ font-size: 1.2em; cursor: pointer; background: #eee; display: block; position: relative; padding: 7px 10px; font-weight: bold; border: 1px solid #ddd; border-left: 3px solid #888; text-shadow: 0 1px 0 rgba(255,255,255,.5); transition: all .15s ease-out; } /*Remove text selection when toggle-ing*/ .faq-section label::selection{ background: none; } .faq-section label:hover{ background: #f5f5f5; } /*If the checkbox is checked, style the label accordingly*/ .faq-section input[type=checkbox]:checked~label{ border-color: #ff7f50; background: #f5deb4; background-image: linear-gradient(to bottom, #fff, #f5deb4); box-shadow: 0 0 1px rgba(0,0,0,.4); } /*Label's arrow - default state */ .faq-section label::before{ content: ''; position: absolute; right: 4px; top: 50%; margin-top: -6px; border: 6px solid transparent; border-left-color: inherit; } /*Update the right arrow*/ .faq-section input[type=checkbox]:checked~label::before{ border: 6px solid transparent; border-top-color: inherit; margin-top: -3px; right: 10px; }
Browser support
What about the older browsers? That’s a normal question, and the answer is graceful degradation:
Using the following snippet, we’re targeting browsers like IE8 and below. So, we’ll enable the HTML5 elements like section
and then add some custom styles in order to keep the FAQ’s content readable.
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<style>
.faq-section label,
.faq-section label:hover{
cursor: default;
background: #eee;
}
body .faq-section p{ /* Increase specificity */
display: block;
color: #444;
font-size: 1em;
text-overflow: clip;
white-space: normal;
overflow: visible;
}
</style>
<![endif]-->
Done!
That’s all, I hope you liked this article. Please feel free to comment and share your thoughts/ideas about the result.