Spelchan.com Logo

From Scratch Web Games: A Beginners Guide to Game Development using HTML, CSS, and JavaScript

Chapter 3.10: Link Style

While you have been experimenting with CSS, you may have noticed something interesting happens when you change the color of a link. This causes the link to stay just the color that you have set it to. You no longer have different colors for visited links. Many elements have different states. When you set the element style, you are also applying that style to all the states of that element. CSS gets around this potential problem by letting you apply styles to the different states of an element by using the colon to indicate what state you are applying the style to. The anchor tag has four different states which are a:link, a:visited, a:hover, a:active.

An unvisited link uses the a:link, and represents the anchor when the link has never been clicked. Browsers tend to determine visited links by seeing if the link is in its history so this is not always accurate. When you have visited a link the a:visited definition gets displayed. This allows visited links to look different from unvisited links, though many sites set these to the same style.

The a:hover state is tied to the mouse pointer so has no real effect on touch-based input. When the mouse pointer is over the link then the hover style is shown. If the link is being clicked, and touching a link is the same as clicking a link, then the a:active state is displayed.

By taking advantage of the box model and link types, we can style a link to act like a button. First, we need to create a style that looks like a button. By taking advantage of rounded borders in combination with the style of the border we can start to create an element that looks like a button. One issue, however, is we want to make sure that the button box is considered. I am using buttons for navigation so my class will be called nav_btn, but I only want the button properties to be shown when it is an anchor. CSS supports doing this by letting you specify the element that you want and then how a class of that element will work:

a.nav_btn {

While it is not necessary to specify the font properties for the button, fonts can be part of your design decision. Based on your needs you may want different defaults or no default and rely on the default font for the div the button is in. For my design I want a bold font with centered text. I want a font size of 15 points, with no text decorations. I want my button to be purple, so I am setting the background color to purple and the text to be a saturated purple. All of this is straightforward CSS attribute settings.

font-weight: bold;
text-align: center;
font-size: 15pt;
text-decoration: none;
color: #CAD;
background-color: #250030;

A solid block of color doesn’t really look like a button. I like my buttons to be slightly rounded with borders that look like the button is above the screen. Looking at borders, we can easily set rounded borders and looking at the different border styles, an outset border does have a button-like look to it, so we will use those.

border: 3pt outset #101;
border-radius: 1em;

Now we simply need to adjust the padding and margins of the font.

padding: 1em;
margin: 5pt;

A bit of testing and we seem to have a bit of a problem. It seems the inline type of display that the button uses simply does not honor our padding. The display property lets us use inline to allow us to create spans, and blocks to allow us to create divs, but we want a span that enforces it’s container so we will be using a new display type called inline-block. This is a bit of an advanced feature which we will be covering in chapter 6 in more detail when we are discussing layouts. While we are using advanced features, lets also add a box shadow to the button to give it a bit more of a pop.

display: inline-block;
box-shadow: 3px 3px 5px rgba(0,0,0,.80);

The result of all this work is a link that looks like a button. However, because we are overriding the anchor tag, this has the effect of also altering the hover and active states of the anchor. This means that moving the mouse over the button or clicking on the button does nothing to the button. It would be nice to have some effects happen when the mouse was over or the button was clicked. The states of an element can also be adjusted in a class so, we simply need to make some modifications to those states to get color changing effects to indicate that that something is happening with the button. We can also change the border style from outset to inset to try and create a button being pressed type of effect.

a:hover.nav_btn {
background-color: #400030;
color: #ECF;
a:active.nav_btn {
background-color: #800070;
color: #FDF;
border: 3pt inset #101;

To use these buttons, you simply need to add the nav_btn class to an anchor such as:

<a class="nav_btn" href="#">To top</a>
To top

And it is that easy to have fancy links for your pages. Making lists more interesting will be our next goal.

As this chapter was being released in December, a Christmas Total Conversion of this Chapter's project was released.

Chapter contents

Chapter 3 Contents

3.1 CSS Basics Cheat Sheet

A cheat sheet covering the basics of CSS.

3.2 What is CSS

A brief look at what CSS is and how it came to be.

3.3 Setting up Style Sheets

A look at the three different ways to add style to your document.

3.4 Blocks, Spans, and Selectors

Setting the style for different types of elements.

3.5 Classes

An easy way of having multiple styles on the same element type and across different elements.

3.6 Colours

The use of named colors, the RGB color model, and creating RGB colors.

3.7 Hexadecimal color codes

Using hexadecimal color codes and other color models.

3.8 Fonts

Controlling what text looks like.

3.9 Box Model

The box model controls how elements are positioned within their layout box.

3.10 Link styles

Anchor element states and turning a link into a button.

Christmas Bonus

Hunt the Santa logo
A total conversion of Hunt the Webbus with a Christmas Theme.

3.11 Styling lists

Lists can have style applied to their layout, placement, and bullet.

3.12 Styling tables

Tables have a grid as well as rows and columns that can be controlled.

3.13 Webbus project

The project for this chapter is a variation of a classic text game

3.14 Webbus solution

My solution for the Webbus project

3.15 Christmas Total Conversion

Converting the Webbus game into a Christmas game.

← previous section
Box Model
next section →
Styling Lists
Table of Contents