FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Change background of a DIV (no JS)

 


m-productions
As the title says, I want to make it so when your mouse goes over a div, the background color will change..., However, I can not use JS for this, needs to be 100% CSS, now im hopeing theres another way to do this, but so far, ive been told of one way (and I cant even get that to work) its as follows, I make a div like this

Code:

 <a href="http://m-productions.deviantart.com"><div class="jcustom">The following is a test for my new layout, please do no look at this box.</div></a>


now my Css for jcustom is as follows.
Code:

.journalbox .journaltext .jcustom {
margin: 0px 1px 0px 1px !important;
background-color: #414141 !important;
color: #222222 !important;
font-size: 13px !important;
padding: 0px 15px 15px 15px !important;
display: block !important;
border-style: dashed !important;
border-width: 1px 1px 1px 1px !important;
border-color: #222222 !important;
width: auto !important;
}

.journalbox .journaltext .jcustom a {

}
.journalbox .journaltext .jcustom a:hover {
background-color: #FFFFFF !important;
}


Now get this, when your mouse goes over a link, the links backgound turns white -_-, when you go over the DIV (that is a link.. you can see your mouse turn into a link click) that background does not change color...any ideas??

ps: you can see what I mean here
AftershockVibe
I tried to do something like this a while back and, unfortunately, never found a solution. I think I managed to con Internet Explorer into doing it but not Firefox.

I too wanted to make a "button" using a DIV with text inside. Now, I found I had problems when I tried to encapsulate the ?<div> within the <a> tag because <a> is supposed to be inline and <div> isn't and you're not supposed to put non-inline elements inside inline ones becasue then they'd be inline too, or somesuch.

Anyway, I managed to make the <div> change colour when you hovered, but unfortunately the link would only work when you clicked on the text itself. So while teh button would look like a button it would only actualyl work if you clicked on the text itself.

At the time I decided I'd prefer a tree-style menu anyway so stopped working on it. However, I have thought of a possible workaround since.

I don't know if this will work but you might want to try it:
- Put the <a> inside the <div>
- Force the size of the <a> element to be the same size as the <div> using CSS. Manual width/height or 100% should work.
- Use the hover effect as before on the <div> or possibly the <a> (I'm not sure which the browser will pick as being hovered over (both??))

Not sure if this will help but good luck.
Ranfaroth
As said, you can't put a div in a a.
And it's useless, since the simple way to achieve what you want is this single line :

#myDivID:hover {background-color : mycolor}
m-productions
Ranfaroth wrote:
As said, you can't put a div in a a.
And it's useless, since the simple way to achieve what you want is this single line :

#myDivID:hover {background-color : mycolor}


Ive tried this as well... but it does not work.
snowboardalliance
m-productions wrote:
Ranfaroth wrote:
As said, you can't put a div in a a.
And it's useless, since the simple way to achieve what you want is this single line :

#myDivID:hover {background-color : mycolor}


Ive tried this as well... but it does not work.


That should work though. Just not as a link as AftershockVibe said.

Alternatively, setting a to display:block could allow the a to fill the div when you set the size
Azmo
I'm just curious why you would want to use a div as a link?
welshsteve
Firstly, you cannot place a block level element such as a DIV inside an inline element such as an anchor tag. Therefore, you need to do what snowboardalliance suggested and set the anchor tag's display to block.

Here's the code you need.

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

   <title>TEST</title>

   <style type="text/css">
   .bgchange { width:200px; height:200px; border:1px solid #000; background:#ffa; }
   .bgchange:hover { width:200px; height:200px; border:1px solid #000; background:#aaa; }
   .bgchange a:link, .bgchange a:visited { display:block; color:#00f; text-decoration:none; height:100%;width:100%;}
   .bgchange a:hover, .bgchange a:active { display:block; color:#f00; text-decoration:underline; height:100%;width:100%; }
   </style>


</head>

<body>

<div class="bgchange">
   <a href="#">Some Link Text</a>
</div>


</body>

</html>




Hope that helps.
Ranfaroth
m-productions wrote:
Ive tried this as well... but it does not work.
Just try again carefully, because it works...
welshsteve
Use my code above. It works in all main browsers
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.