views:

126

answers:

5

Trying to find out more about css pseudo class a:focus Am trying to adapt some code. Have tried

a:focus img {border:2px solid rgb(155, 205, 255);}  

and

a:focus img {background:rgb(155, 205, 255);}  

Neither work. Yet

a:hover img {background:rgb(155, 205, 255);}  

works fine on hover. Does anyone know what I've done wrong?
Les

Sorry, am using Firefox, but want this to work cross-browser if possible. I get a flash but not the steady border like this gallery: http://www.cssplay.co.uk/menu/gallery.html#nogo

A: 

If your right click on it to give it focus without actually following the link, does it change?

Matt Blaine
Yes. The border stays. What does that mean?
Les
I coded up a little test, it works for me. Is the site you're working on something public we could look at? Either way, maybe try using !important, in case some other style on your site is overriding the ones you posted. http://www.w3.org/TR/CSS2/cascade.html#important-rules
Matt Blaine
I hate giving this link, it's my first site and pretty dire. Trying to fix, so pls excuse.http://www.townsvillemultiples.com.au/photosclk.htmla:focus isn't added as it is not functional. Am working offline to will upload when it works. The relevant css is in the head and I currently have: ul#thumbnails li a:hover {background:rgb(145, 225, 235);} ul#thumbnails li a:focus img {background:rgb(155, 205, 255);} in my offline file. (In the online version, you will see only the first line of the above code)
Les
...............
Les
A: 

Your selectors seem fine.

A few questions: What browser are you using? As Matt said, are you sure you are focusing the tag?

jrallison
Firefox. Added to Q is that wrong to do? The focus works, but it only flashes like hover and active. It should stay until next click.
Les
A: 

If you're using IE8,

Note: Internet Explorer 8 (and higher) supports the :focus pseudo-class if a !DOCTYPE is specified. (Source: http://www.w3schools.com/CSS/pr_pseudo_focus.asp)


Investigation: Seeing that you're using Firefox, click on a blank page and press Tab on keyboard, with the following CSS:

a:focus img {border:2px solid rgb(155, 205, 255); padding: 2px;}

See if this works, and report appropriately.

Also, does the <a> tag has other pseudo-classes outside of focus?


Just a suggestion, based on yoru example http://www.cssplay.co.uk/menu/gallery.html#nogo, they did this:

#container a.pics:focus img.thumb {border:1px solid #fc0}

Why don't you do this?

a img:focus {border:2px solid rgb(155, 205, 255); padding: 2px;}
The Elite Gentleman
Using Firefox, but would like best cross-browser result.
Les
Using tab puts the cursor in FFs URL box. It says: "Firefox doesn't know how to open this address, because the protocol (a) isn't associated with any program." <a> tag hover, active and focus set (in that order) Removing hover and active makes no difference. When I right click the thumbnail, the focus stays. Is this good or bad??? I want it to work like this gallery: http://www.cssplay.co.uk/menu/gallery.html#nogo Does that help at all?
Les
Well, the tutorial on CSS Play, the focus works. Check: http://www.w3schools.com/CSS/css_pseudo_classes.asp to see the order of order of `<a>` pseudo-classes (hope this helps you).
The Elite Gentleman
Check updated post....
The Elite Gentleman
A: 

It looks like clicking a link that takes you elsewhere on the same page is causing that link to loose focus. Not the most elegant solution, but what I can come up with involves javascript.

For the style:

a.selected img{border:9px solid green}  

And then a script:

<script type="text/javascript">
    var last = null;
    function select(element)
    {
        if(last != null)
            last.className = last.className.replace(/ selected/g, "");
        element.className += " selected";
        last = element;
    }
</script>

And then add an onclick event handler to each thumbnail link:

<li><a href="#pic1" onclick="select(this);"><img src="thumbs/whatever.jpg" alt="whatever" /></a></li>
Matt Blaine
Thanks Matt. Have noted that one down. Have begun with php, but no JS yet, so much appreciated :)
Les
A: 

Try setting the border on the a tag rather than the image tag inside of it.

Catfish
Do you mean ul#thumbnails li a:focus {background:green;} This gives the same result. focus works only on right click. Am following the formatting rules for my comments, but see that it isn't working. Sorry for the lousy formatting - don't know how to fix it.
Les