04.06.2013 Views

Head First HTML with CSS

Head First HTML with CSS

Head First HTML with CSS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

How can you style elements<br />

based on their state?<br />

A link can be in a few states: it can be unvisited, visited,<br />

or in the “hover” state (and a couple of other states too).<br />

So, how do you take advantage of all those states? For<br />

instance, it would be nice to be able to specify what the<br />

visited and unvisited colors are. Or maybe highlight the<br />

link when the user is hovering over it. If only there were<br />

a way...<br />

Well, of course there is, but if we told you it involves<br />

using pseudo-classes you’d probably just decide you’ve read<br />

enough for the night, and close the book. Right? But<br />

hold on! Pretend we never said the word pseudo-class and<br />

let’s just look at how you can style your links:<br />

Notice we have the element , followed by a<br />

“:”, followed by the state we want to select.<br />

a:link {<br />

color: green;<br />

}<br />

a:visited {<br />

color: red;<br />

}<br />

a:hover {<br />

color: yellow;<br />

}<br />

Exercise<br />

This selector is applied<br />

to links when they are<br />

in an unvisited state.<br />

And this selector is<br />

applied to links when<br />

they are visited.<br />

And this selector<br />

is applied when you<br />

hover over a link.<br />

Add these rules to the bottom of your<br />

“lounge.css” file and then save and reload<br />

“lounge.html”. Play around <strong>with</strong> the links to<br />

see them in each state. Note that you might<br />

have to clear your browser history to see the<br />

unvisited color (green). Once you’re done,<br />

make sure you take these rules out of your<br />

“lounge.css” file before you continue.<br />

there are no<br />

Dumb Questions<br />

divs and spans<br />

Q: What happens if I just style the <br />

element like a normal element? Like:<br />

a { color: red; }<br />

A: You certainly can do that, but then your links<br />

will look the same in all states, which makes your<br />

links less user-friendly because you can’t tell which<br />

ones you’ve visited and which ones you haven’t.<br />

Q: What are the other link states you<br />

mentioned?<br />

A: There are two others: focus and active. The<br />

focus state occurs when the browser focuses on your<br />

link. What does that mean? Some browsers allow you<br />

to press your tab key to rotate through all the links on<br />

your page. When the browser comes to a link, that<br />

link has the “focus.” The active state occurs when<br />

the user first clicks on a link. One word of warning<br />

about these two states: they aren’t well supported by<br />

all browsers yet, so make sure and test them if they<br />

are important to your design.<br />

Q: Can’t my links be in multiple states at<br />

the same time? For instance, my link could be<br />

visited, have the mouse hovering over it, and the<br />

user could be actively clicking on it all at once.<br />

A: They sure can. You determine which style<br />

is applied by the ordering of your rules. So, the<br />

right ordering is generally considered to be: link,<br />

visited, focus, hover, and then active. If you use that<br />

ordering, you’ll get the results you expect.<br />

Q: Okay, I give. What’s a pseudo-class?<br />

A: Only one of the most confusing words in the<br />

<strong>CSS</strong> language. But, as you’ve seen, styling links is<br />

pretty straightforward. So, let’s talk about pseudoclasses...<br />

you are here 469

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!