Usually, an 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 FAQ page or a FAQ section.
In this article, you will learn how to create a fancy FAQ design using CSS only, no JavaScript.
The FAQ page idea
I visited Facebook’s Help Center section - their FAQ’s page - and I noticed a cool effect for the answers previews. So they were showing a small, faded and clipped text preview for the answer, and then, when the question was clicked, the complete answer was revealed.
After seeing it, I immediately thought about how can I create a similar effect using CSS only.
The FAQ markup
We will start as usual with the markup structure:
-
In the above image, the
label
is the proper heading of the section. But, if you want to use better semantic, you can wrap thelabel
into ah1
for example. -
Using the
label::before
pseudo-elements selector allow us to create the right triangle shape design. As a rule, using double colon (::
) for the CSS pseudo-elements is used instead of single colon (:
) to distinguish CSS pseudo-classes from pseudo-elements. -
The first paragraph for each section is the intro paragraph for the complete answer. For this example, I used the CSS 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 toggling an <input>
using the adjacent <label>
. 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. But 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:
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.
Update: i0S support
You asked for it, now you have it: iOS browser support. I had some time to think about it and I made updates regarding hiding the checkbox.
Here’s my fix, tested on iPhone and iPad using the latest iOS versions:
-
position: absolute
- While.faq-section
wrapper is relative positioned, we’ll need this to visually place our checkbox above the label. -
z-index: 2
- Make sure it will be above section content, including label. -
cursor: pointer
- Optionally, this will add a pointer cursor when you hover on it. -
opacity: 0
anddisplay: none\9
- Visually hide the checbox, while on browsers like Internet Explorer 8 and below will be hidden. -
margin: 0
- Remove default margin. -
width: 100%
andheight: 36px
- The checkbox height value matches the height of the label. Also, using 100% for the width will expand the checkbox in order to fully cover the label.
That’s it!
I hope you found this article useful, so feel free to to share your thoughts and ideas with me about the result.