Forum Moderators: not2easy

Message Too Old, No Replies

changing the background color of a div using only css

change background color on mouse over

         

pixelpixie

3:53 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



Hi, i wonder if anyone can offer me some advice, i am working on a website and i have managed to get the color of a background division to change on mouse over. I have tried to use the same effect on another website but it just wont work. please see [comparegasandelectric.org...]

view the source and click the link for the css file and you will see i have simply used this format

#div-id{
background-color:#f36;
}

#div-id :Hover{
background-color:#fff;
}

It works great yet when i try to reuse this style on any other website it doesnt work, im seriuosly confused! please help me!

I am now using js to change the background color on mouseover but i know it is pos with css because its working - take a look. Its on the menu system

Please note i have temporerily abandoned this project for now, but i want to use it on a new project im working on and in all projects in the future.

Major_Payne

4:51 pm on Sep 29, 2010 (gmt 0)



First, always use lowercase. Might help...

You may not know it, but a link has four different states that it can be in. CSS allows you to customize each state. Please refer to the following keywords that each correspond to one specific state:

* link - this is a link that has not been used, nor is a mouse pointer hovering over it
* visited - this is a link that has been used before, but has no mouse on it
* hover - this is a link currently has a mouse pointer hovering over it/on it
* active - this is a link that is in the process of being clicked

Using CSS you can make a different look for each one of these states:

a:link {
color:#006;
text-decoration:none;
cursor:pointer;
}

a:visited {
color:#369;
}

a:hover {
color:#f60;
text-decoration:underline;
}

a:focus {
outline: none; /* remove the dotted outline added by Firefox */
}

a:active {
color:#fc9;
cursor:wait;
}

a:link {color: #090;}
a:visited {color: #999;}
a:hover {color: #333;}
a:focus {color: #333;}
a:active {color: #090;}

Order matters. If "a:active" precedes "a:hover", the effects in "a:hover" will take precedence. So, in this example, you would not see the color change when the user clicks down on a link.

Pseudo Classes

You can set links contained in different parts of your web page to be different colors by using the pseudo class. For example, lets say you want your links in the content area to have a different color then the links in the left or right column of your webpage.

You can do this in the following fashion:

#pseudo_content a:link {color: #090;}
#pseudo_content a:visited {color: #999;}
#pseudo_content a:hover {color: #333;}
#pseudo_content a:focus {color: #333;}
#pseudo_content a:active {color: #090;}

Now assuming that you have your main content in a division named "content" all links within that division will now be styled by this new style selector. Should your selector have a different name, just change the #pseudo_content selector to match your division name.

Then for the links in a column you could use the following:

#pseudo_column a:link {color: #090;}
#pseudo_column a:visited {color: #999;}
#pseudo_column a:hover {color: #333;}
#pseudo_column a:focus {color: #333;}
#pseudo_column a:active {color: #090;}

Once again, this assumes the name of the column division, just change the name to match yours.

This same method can be accomplished by declaring a class instead of an id.

a.pseudo_column:link {color: #090;}
a.pseudo_column:visited {color: #999;}
a.pseudo_column:hover {color: #333;}
a.pseudo_column:focus {color: #333;}
a.pseudo_column:active {color: #090;}

Though in this case you will need to add a class to each link

<a class="pseudo_column" href="" title="">some link text</a>

But, there is still yet an easier way

.pseudo_column a:link {color: #090;}
.pseudo_column a:visited {color: #999;}
.pseudo_column a:hover {color: #333;}
.pseudo_column a:focus {color: #333;}
.pseudo_column a:active {color: #090;}

Then in the (X)HTML file

<div class="pseudo_column">
<a href="" title="Link Description">Link Text Description</a>
</div>

pixelpixie

5:22 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



I understand how to change the link properties but to change the background color of the whole division not just the text or the small area around the text,

see the difference in my menu's

[3dbabiecasts.co.uk...] where i am changing the a: link properties and [comparegasandelectric.org...] where i am changing the whole background color ( i have used the color white just to test it.)

Major_Payne

5:28 pm on Sep 29, 2010 (gmt 0)



You were using the "hover" and IE does not work well with the pseudo-classes. Play with this and see if it'll work for you:

<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<title>Change Background Color on Mouseover of Text Box</title>

<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="created" content="Mon, 27 Sep 2010 16:22:27 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="PayneLess Designs">
<meta name="copyright" content="Jan 2004 to present.">

<style>
#wrap {
width: 410px;
height: 410px;
margin: 0 auto;
border: 1px solid #f0f;
}

a {
display: block;
width: 400px;
height: 400px;
padding: 5px;
text-decoration: none;
font-size: 1.25em;
color: #0f0;
}

a:link { background: #00f; }
a:visited { background: #f00; }
a:hover { background: #ff0; }
a:active { background: #f0f; }
</style>


<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="wrap"><a href="#nogo" title="Link Description">Unvisited: Blue.<br>Visted (Clicked): Red.<br>Hover: Yellow.<br>Active: Fuchsia.</a></div>
</body></html>
<!-- HTML 5 -->

pixelpixie

8:46 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



could we talk on skype or msn because im sure you dont understand what im trying to say. your still trying to tell me how to change the properties of an a href. i simply want to change the color of the whole division, i repeat what i said before i know how to manipulate a href from the a properties. i think the only way to show you exactly what im talking about and clear this up is to talk live and i can show you using team viewer. your help is greatly appreciated major payne. add me on skype dunebuggyqaz or msn dmc@live.co.uk please reply here when you have added me just to let me know thanks.

Major_Payne

10:25 pm on Sep 30, 2010 (gmt 0)



Did you try the code I posted. It changes the background of the div as you wanted on mouseover. What do you think I am missing? "hover" is a property of the link tags. If you want to make changes without using Javascript, then my code works just fine. I've tested it.

tedster

3:15 am on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While that code does create an effect along the same lines, it isn't exactly what the OP is asking about. pixelpixie wrote, "It works great yet when i try to reuse this style on any other website it doesnt work, im seriuosly confused"

The :hover pseudo-class is available for all kinds of elements, not just anchor tags. In certain cases, applying it to the <a> element will still create the same effect. However, the question is about a set of CSS rules that worked on one page and not another. The option of changing to display:block for an anchor tag is not always what a situation calls for.

#special{background-color:#f36;}
#special:hover{background-color:#fff;}

...that basic code should do it for a div with the attribute id="#special" - so there's almost got to be some conflicting CSS rules on the non-functioning page. Some kind of DOM inspector (Dragonfly, which is native to the Opera browser, for instance) would be the debug tool I would use.