Solved

CSS image form button - help with hover

Posted on 2010-11-16
7
706 Views
Last Modified: 2012-05-10
Button Off State Button Over State
Hi,
the following code appears to work OK in Firefox and the image changes when you hover over it, but not in I.E.. Is there a way to achieve this in I.E. with CSS or Javascript?

Also does the CSS code look OK, should I be doing it another way?

Thanks in advance for you feedback.
<html>

<head>

<title>Test</title>

</head>

<body> 

<style type="text/css"> 

 



/* CSS */

.btnExample {

  margin:0; 

  padding:0;

  border:0;

  color: #fff;

  width:136px;

  height:33px;

  background: #333;	

  font-weight: bold;

  background:url(images/but_off.png);

}



.btnExample:hover {

  cursor: pointer; /* cursor: hand; for IE5 */ 

  color: #FFF;

  background:url(images/but_ovr.png);

}







</style>

  

<form id="login_form" name="login_form" enctype="multipart/form-data" action="/process_login.php" method="POST">

 



<input name="Submit" type="submit" class="btnExample" id="Submit" value="submit" />





</form>  

</body>

</html>

Open in new window

0
Comment
Question by:sabecs
  • 4
  • 2
7 Comments
 
LVL 14

Expert Comment

by:ali_kayahan
Comment Utility
Hi Sabecs ; i may suggest you to follow a better way by using one picture to achive the effect that you work on , currently it seems that due to the image size ie cant render your hover image on mouse over .

If you merge two pictures in to one, side by side you can use the code below . (the picture sizes should be same for ex. if normal state has 70px width , hover state should has same width and total image size will be 140px)

In css ;

.btn{
   width:70px; // i know our image is 140px width but we should first render normal state
   height:33px;
   background:url(images/both.png);
}

.btn:hover{
   background-position:top right;// we slide our background image to right
}

Another pitfall of using two different images to achieve this effect , visitor will see a white flash during the loading of hover image.
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
Comment Utility
Actually, input:hover doesn't work on IE.
Maybe you can wrap your button in A tag and use the :hover of that. But I don't know how it would react with rest of you CSS and HTML.
You can try it.
<html>
<head>
<title>Test</title>
</head>
<body> 
<style type="text/css"> 
 

/* CSS */
.btnExample {
  margin:0; 
  padding:0;
  border:0;
  color: #fff;
  width:136px;
  height:33px;
  background: #333;     
  font-weight: bold;
  background:url(images/but-off.png);
}

.btnWrap:hover .btnExample {
  cursor: pointer; /* cursor: hand; for IE5 */ 
  color: #FFF;
  background:url(images/but-ovr.png);
}



</style>
  
<form id="login_form" name="login_form" enctype="multipart/form-data" action="/process_login.php" method="POST">
 

<a href="javascript:void(0);" class="btnWrap"><input name="Submit" type="submit" class="btnExample" id="Submit" value="submit" /></a>


</form>  
</body>
</html>

Open in new window

0
 

Author Comment

by:sabecs
Comment Utility
Thanks for your help,
I tried Ali's suggestion but it did not do anything.
Sudaraka's suggestion seems to be working however the over image looks like its going behind the off image?


<html>

<head>

<title>Test</title>

</head>

<body> 

<style type="text/css"> 

 



/* CSS */

a.btnWrap{ 

  text-decoration:none;

}



.btnExample {

  margin:0; 

  padding:0;

  border:0;

  color: #fff;

  width:136px;

  height:33px;

  background: #333;	

  font-weight: bold;

  background:url(images/but_off.png);



}



.btnExample:hover {

  cursor: pointer; /* cursor: hand; for IE5 */ 

  color: #FFF; 

  background:url(images/but_ovr.png);

}

.btnWrap:hover {

  cursor: pointer; /* cursor: hand; for IE5 */ 

  color: #FFF;

  background:url(images/but_ovr.png);

}





</style>

  

<form id="login_form" name="login_form" enctype="multipart/form-data" action="/process_login.php" method="POST">

 



<a href="#" class="btnWrap"><input name="Submit" type="submit" class="btnExample" id="Submit" value="submit" /></a>





</form>  

</body>

</html>

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
Comment Utility
over image looks like its going behind the off image?

It's because you have assigned the back ground image to .btnWrap, instead what you need to do is assign it to the
0
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 500 total points
Comment Utility
over image looks like its going behind the off image?

It's because you have assigned the back ground image to .btnWrap, instead what you need to do is assign it to the .btnExample that is in .btnWrap:hover state.

.btnWrap:hover .btnExample {...}

P.S. sorry about the partial post above. Mouse slipped :)
0
 

Author Closing Comment

by:sabecs
Comment Utility
It's perfect, thanks.
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
Comment Utility
Glad to help. Thanks for the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now