Advertisement
Tech

CSS Tricks: Using CSS to Build a Multiple Choice Quiz

If you want to use CSS to make your pages more interactive, it will provide a platform-independent solution. One novel use of CSS is to construct a multiple choice quiz, which allows you to test the page reader’s knowledge with only CSS and HTML.

By Profacgillies
Desk Tech
Reading time 2 min read
Word count 419
Web development Internet Web design
CSS Tricks: Using CSS to Build a Multiple Choice Quiz
Advertisement
Quick Take

If you want to use CSS to make your pages more interactive, it will provide a platform-independent solution. One novel use of CSS is to construct a multiple choice quiz, which allows you to test the page reader’s knowledge with only CSS and HTML.

On this page

Introduction

Cascading Style Sheets (CSS) offer platform-independent solutions to a range of problems. In a previous article (Using CSS to add photo galleries to your website), we have seen how to create a photo album, and in another how to create an enhanced menu system. In this article, we will use CSS tricks to create a simple multiple choice quiz. You can see the quiz in action at https://www.alangillies.info/demos/cancer/quiz1.html .

How The Quiz Works

The opening screen offers a question with four potential answers. The code uses CSS similar to a simple menu structure to select the correct answer:

Advertisement

a:link { text-decoration: none; color: white; font-size:16px ;font-family:“Georgia”,“Book Antiqua”,“Times New Roman”,“Times” , serif;}

a:visited { text-decoration: none; color: white; font-size:16px ;font-family:“Georgia”,“Book Antiqua”,“Times New Roman”,“Times” , serif;}

Advertisement

a:active { text-decoration: none; color: white; font-size:16px ;font-family:“Georgia”,“Book Antiqua”,“Times New Roman”,“Times” , serif;}

a:hover { text-decoration: none; color: black; font-size:16px ;font-family:“Georgia”,“Book Antiqua”,“Times New Roman”,“Times” , serif;background-color:silver;}

Advertisement

Once an answer is selected a simple hyperlink takes the reader to a new page based upon whether the correct answer is selected:

What kind of organisation is behind the https://www.discern.org.uk site?

A wrong answer takes the reader to a page with a different image with a negative facial expression and a message over the image which tells the reader it is the wrong answer.

Advertisement

A correct answer takes the reader to the next question accompanied by an image with a positive facial expression and a message over the image which tells the reader it is the correct answer.

The code to generate the messages is the same as described elsewhere to provide additional explanation to a CSS menu:

Advertisement

HTML:

Advertisement

CSS:

#leftimg a span img

Advertisement

{

border: 1px solid black;

Advertisement

float:right;

margin-left:10px;

Advertisement

margin-bottom:5px;

}

Advertisement

#leftimg a:hover span

{

Advertisement

display: block;

position:relative;

Advertisement

color: black;

font:14px;

Advertisement

margin-top: -300px;

padding: 10px;

background-color: #ffff88;

border: 1px solid black;

margin-left: -50px;

}

Conclusions

There are more sophisticated ways to generate interactive questions using Javascript, or within Flash-based or PHP/SQL-based, but the simplicity of this solution makes it easy to implement and reliable across many contexts. It can be hosted anywhere without the need for access to SQL databases, and the knowledge required to implement and manage such solutions.

Flash-based solutions can be very visually attractive, but a CSS solution offers better accessibility and search engine optimisation.

Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Web design
Advertisement