Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS OnClick Active state modifications to existing code

Status
Not open for further replies.

pdeman

Programmer
Feb 17, 2010
39
GB
Hello

I have the following code which creates menu or rollover buttons with rounded corners using only CSS and no images and works in IE.

I need to modify the CSS to include the OnClick or Active state which I think it is called in CSS so that the button Background or Border or Text changes to a different colour when clicked.

Can anyone help me by modifying the code?

Thank you and best regards

****************************CODE START***************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
ul.qcorners {
list-style-position: outside;
list-style-type: none;
font-family:Arial, Helvetica, sans-serif;
font-size:1.3em;
width:33%;
}
ul *{zoom:1;position:relative;}/*ie fix*/

.qcorners li{border:solid 1px #666;border-top:1px solid #666;margin-bottom:6px;}
.qcorners a{background:#ccc;display:block;margin:-2px 0;border-top:1px solid #666;border-bottom:1px solid #666;text-decoration:none;color:#CC0000;}
.qcorners strong{background:#ccc;display:block;margin:-2px 1px;border-top:1px solid #666;border-bottom:1px solid #666;padding:5px;}
.qcorners a:hover,.qcorners a:hover strong{background:#fff;}
-->
</style>
</head>

<body>
<ul class="qcorners" >
<li><a href="#"><strong> some text </strong></a></li>
<li><a href="#"><strong> another item</strong></a></li>
<li><a href="#"><strong> all lists and link tags... </strong></a></li>
<li><a href="#"><strong> with a little strong </strong></a></li>
<li><a href="#"><strong> stretches in 2 directions </strong></a></li>

</ul>
</body>
</html>
****************************CODE END****************************
 
Code:
.qcorners a:active,.qcorners a:active strong{background:blue;}
Add this to the end of your css section.

Please note that this will change will take effect when you are actively pressing the button (clicking on it). Once you're no longer clicking on it, it will revert back to hover (if the mouse is still on) or link (if the mouse is off).

If you need to have additional state for links that denote the current page or something, you will have to add it as a class to the relevant link.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Hello Vragabond

Thank you for your reply.
I tired your modification however it does not seem to work.
If I delete the Strong from the CSS then it works but only if you click in a certain place which is no good. Anyway deleting the Strong from the CSS changes the way the script works which also is no good.
I just thought it might mean something to someone who understands what the code is doing and might be able to help me make the OnClick or Action style work with the existing code.

Thank you and best regards
 
Please check your example with my addition here: active example.

This works as I said it would and uses the code I gave you. When clicking anywhere in the button (tested in IE7 and FF3.6), the background of the button changes to blue. IE7 interprets this wrong and keeps the blue background until you click somewhere else (this would actually be :focus, rather than :active), while FF interprets it correctly and displays blue only while the button is being pressed (clicked on with a mouse).

To explain the code, it means:
- comma separates two independent selectors that both use the same declaration
- first selector says: when a link element that is inside an element with a class named "qcorners" is active (being clicked), change the background colour to blue.
- second selector says: when a link element, which itself is inside an element with a class named "qcorners", has a strong element as a child and is active (being clicked), change the background colour of the strong element to blue.

Hope it makes sense. Again, if the :active or the effect of mouse clicking on the link is not what you're after, you will have to go down another route.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Hello Vragabond

Thank you for your reply.
I clicked on your link 'active example' and it still won’t work for me.
I’m running IE8.
Interestingly when I change active with focus like you mention it does work so this must be the solution I guess. Thank you for your help and time I have learnt something new. Not quite sure why focus works for me and active doesn’t but the important thing is it does.

Thank you and best regards
 
Yeah, IE8 doesn't seem to like it. IE8 in compatibility mode works.

IE: The bane of web developers. It sure would be nice if IE managed to be consistent with other browsers and itself one of these days.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top