/    Sign up×
Community /Pin to ProfileBookmark

Button colour

Hi,

Does anyone know how to change the colour of a standard button from boring grey to another colour when using the onMouseOver and onMouseOut events.

An example would be great

Alan

to post a comment
HTML

11 Comments(s)

Copy linkTweet thisAlerts:
@fredmvDec 15.2003 — In Gecko (Mozilla, K-Melon, Galeon, etc.) you can use pure CSS to do this by using the [font=courier]hover[/font] pseudo-class like this, for example:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<style type="text/css">
/*<![CDATA[*/
#btn {
background-color: #d4d0c8;
}

<i> </i> #btn:hover {
<i> </i> background-color: #0000ff;
<i> </i> }
<i> </i> /*]]&gt;*/
<i> </i> &lt;/style&gt;
<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;
<i> </i> &lt;input type="button" id="btn" value="Hello, World!" /&gt;
<i> </i>&lt;/body&gt;
&lt;/html&gt;
However, IE (and other incredibly lame browsers) only support [font=courier]hover[/font] for [font=courier]<a>[/font] elements. In that case, you'd have to use JavaScript and do something like this:&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="button" id="btn" value="Hello, World!" onmouseover="style.backgroundColor='#0000ff';" onmouseout="style.backgroundColor='#d4d0c8';" /&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@storeyaauthorDec 16.2003 — Thanks,

The IE example is exactly what I am looking for.

I would also like to change the button text colour as the background changes.

Any ideas?

Where do I look for a full list of modifiable attributes ( such as style.backgroundColor) of not just buttons but other objects as well, or can generalised terms be used on various objects?

I will be making reference to your support in my University web assignment.

Alan
Copy linkTweet thisAlerts:
@fredmvDec 16.2003 — [i]Originally posted by storeya [/i][B]

I would also like to change the button text colour as the background changes.[/B]
[/QUOTE]
All you need to do now is change the [font=courier]color[/font] CSS property of the button. However, once you want to change more and more properties of the button it usually makes sense to make another class and then simply switch the class of the element. Although, if you only want one more property to be changed, I don't see too much of a problem with that. Here's an example which changes the background-color and color of the element (in this case, a button):&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="button" id="btn" value="Hello, World!" onmouseover="style.backgroundColor='#0000ff';style.color='#ff0000';" onmouseout="style.color='#000000';style.backgroundColor='#d4d0c8';" /&gt;
&lt;/body&gt;
&lt;/html&gt;
[i]Originally posted by storeya [/i]

[B]Where do I look for a full list of modifiable attributes ( such as style.backgroundColor) of not just buttons but other objects as well, or can generalised terms be used on various objects?[/B][/QUOTE]
I would recommend checking the [url=http://www.w3.org/TR/REC-CSS1]CSS1 specification[/url]. Find the property that you'd like to apply to one of your elements, and then to script the property from JavaScript, all you do is convert the letter right after the hyphen in the property (if there is one) to uppercase. So just like in my example, we're changing the [font=courier]background-color[/font] therefore, to script the property from JavaScript, it becomes:backgroundColor Then for example, [font=courier]font-size[/font] would become:fontSizeGood luck with everything.
Copy linkTweet thisAlerts:
@storeyaauthorDec 16.2003 — Thanks again.

The CSS spec looks good.

What about the style function (excuse my terminology).

Is there another list detailing functions (like style) to use on properties such as backgroundColor



Regards,

Alan
Copy linkTweet thisAlerts:
@fredmvDec 16.2003 — You're welcome. In this case, it would be called the [font=courier]style[/font] [i]property[/i] since it is a property of the [font=courier]HTMLElement[/font] objects, in this specific instance, an [font=courier]HTMLInputElement[/font] object. Anything else is kind of irrelivant in relation to changing the appearance (style) of an element since CSS is what you really want to be using to style elements. However, you can check [url=http://devedge.netscape.com/central/javascript/]JavaScript Central[/url] at Netscape DevEdge for more properites and methods you may be interested in.
Copy linkTweet thisAlerts:
@storeyaauthorJan 17.2004 — Hello again,

Just to let you know that I've submitted my web assignment and mentioned you within the bibliography.

Thanks!

Any idea where I can pickup the javascript for one of those hyperlinks that automatically follows you as you browse down a page?

regards,

Alan
Copy linkTweet thisAlerts:
@fredmvJan 17.2004 — Sounds excellent. Well, as for the script, guess what? You don't need JavaScript for that. All you'd need is pure CSS using [font=courier]position: fixed[/font]. This, of course, works perfectly fine in Gecko, but fails in IE and you'll have to use [url=http://devnull.tagsoup.com/fixed/]this hack[/url] to get it working correctly. Here's a simple example:&lt;div style="position: fixed; top: 50px; left: 230px;"&gt;foo&lt;/div&gt;
Copy linkTweet thisAlerts:
@storeyaauthorJan 17.2004 — Hi,

Fastest reply Ive ever had !

Your obviously a Gecko fan, but after 12 weeks on a web course I feel a bit disappointed with Netscape.

I was astounded that it could not play simple wav files, video, accept some formatting (eg cell border colours), display alt text like IE does without the need to get a plugin.

I had downloded the latest version which concerned me even more?

I tried preferences but could not see much in there.

I expect such a program to work out of the box anyway.

What do you think?

Alan
Copy linkTweet thisAlerts:
@fredmvJan 17.2004 — [i]Originally posted by storeya [/i]

[B]Your obviously a Gecko fan[/B][/QUOTE]
That sounds about right. [url=http://www.mozilla.org/newlayout/]Gecko[/url] is excellent, if you're not yet aware it is a rendering engine in which powers browsers such as Mozilla, Netscape 6+, K-Meleon, Galeon and more.[i]Originally posted by storeya [/i]

[B]but after 12 weeks on a web course I feel a bit disappointed with Netscape.[/B][/QUOTE]
What version are you referring to? Possibly Netscape 4.x? Either way, Mozilla is better. [i]Originally posted by storeya [/i]

[B]I was astounded that it could not play simple wav files, video[/B][/QUOTE]
It can. You need to get the plug-ins.[i]Originally posted by storeya [/i]

[B]accept some formatting (eg cell border colours)[/B][/QUOTE]
It can do that with ease. Using CSS or deprecated attributes possibly.[i]Originally posted by storeya [/i]

[B]display alt text like IE does without the need to get a plugin.[/B][/QUOTE]
The fact that IE displays [font=courier]alt[/font] text is [b]incorrect[/b]. The text defined in the [font=courier]alt[/font] attribute is supposed to be for [i]alt[/i]ernate content, and is not supposed to be tooltip text. That is what the [font=courier]title[/font] attribute is for.[i]Originally posted by storeya [/i]

[B]I had downloded the latest version which concerned me even more?[/B][/QUOTE]
The latest version of? [i]Originally posted by storeya [/i]

[B]I tried preferences but could not see much in there.[/B][/QUOTE]
Preferences for what?[i]Originally posted by storeya [/i]

[B]I expect such a program to work out of the box anyway.[/B][/QUOTE]
It does work out of the box. However, I suppose you do have to do a little bit more work to get plug-ins and the the like working properly, but it really isn't that big of a deal if you ask me and is actually well worth it since Mozilla is such a great browser. You should try installing some extensions or themes and see how incredibly easy they are to get working (not to mention extensions can add even more functionality to an already very functional browser).
Copy linkTweet thisAlerts:
@storeyaauthorJan 17.2004 — Hi,

Can you help me further with the sliding hyperlink.

I looked at the hack but couldnt understand it.

An example for IE5 and above would be great.

Alan
Copy linkTweet thisAlerts:
@fredmvJan 18.2004 — I believe there is an example on the link I provided &#8212; check it out. ?
×

Success!

Help @storeya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...