
It is great how challenging things can get when you leave your standard C++ environment and start dealing with some web stuff, which is quite new to me. There are plenty of things to learn!
I am sure that web design is a piece of cake to many people out there, but since I have just dived into baking myself, I do not have much experience. To help other bakers trying to get things right, I am going to share the things that were difficult for me to figure out.
I started creating the landing page for our upcoming game and thought “well, some HTML here and a bit of CSS there, and we are ready to go”. This worked out quite well until I came to the part where I wanted a simple button to appear on the site that changed when the mouse hovers over it. Believe me, there are as many ways to do this as there are different browsers out there… Which is no coincidence at all. It is just that there is no general solution to this problem that works the same way in every browser!
The first thing I had to do was to focus on the browsers I want to support. At the moment, my choice is “IE, FireFox, Opera”. This should do for a start.
I tried different methods, sometimes including JavaScript. Personally, there is one thing I don’t like about JavaScript: It is often assumed to be unsafe. Depending on your security settings, you might always get asked by IE7 if you want to enable JavaScript on the website you are currently viewing or turn it off. I think this might frighten off some people which IMHO is bad.
So after trying different methods that only worked on two of the three browsers I wanted to support, I finally found the solution that I was satisfied with and that does not use JavaScript. Here is how it works:
You define your button as a cell of a table, known as “table data”. The tag is <td>.
The HTML code for the button is as easy as this:
<!-- mybutton -->
<table border="0" cellspacing="0" cellpadding="0" width="120px" height="24px">
<tr>
<td class="mybutton" width="100%" height="100%">
<a href="my hyperlink reference">Button Text</a>
</td>
</tr>
</table>
<!-- mybutton -->
The important thing is that you use the class “mybutton” for the table, so we are able to define the style for the table data, which is our button, in the CSS file.
So the CSS style is the interesting part. Check this out, it is quite self explanatory:
table.mybutton
{
width: 120px;
height: 24px;
margin: 0px;
padding: 0px;
}
This defines the size for the button. Remember, the table data sort of IS the button.
td.login-button a, td.login-button a:link
{
display:block;
width:100%;
height:100%;
margin:0px;
padding: 0px;
font-size:12px;
font-family:Verdana, Arial;
font-weight:normal;
color:#FFFFFF;
text-decoration:none;
text-align:center;
line-height:22px;
background-image:url(../images/button_normal.gif);
}
Nothing weird going on here. We just define the table data and the link inside it. We give it width and height 100% in order to make it just as big as we defined the table in the HTML file. The background-image contains the graphics for the button in its normal state and is also just as big as the table which in our case is 120 by 24 pixel.
Defining a AND a:link is necessary to make it work in Opera. Only defining a:link is not sufficient for that browser.
One thing to remember is:
Make sure to set all default values the way you want them.
For example, different browsers may have different default values for padding. If you don’t explicitly set them to 0, things might look different in different browsers. You can’t be sure every browser sets default values to 0. You must do this on your own.
td.login-button a:hover
{
color:#FFFFFF;
background-image:url(../images/button_hover.gif);
}
td.login-button a:visited
{
color:#FFFFFF;
}
I guess you already know what this is for. We simply set the image for the state of the link where the mouse hovers over it to the one with the “button-hover” graphics in it. We also make sure that the color always stays white in our button, no matter how normal links are treated.
The reason why I manipulate the Link (a) in here and not the table or table data itself is plain simple: IE does not support the “hover” feature for any other element than the <a> element. So :hover for table data won’t work. Don’t ask why, I don’t know.
I found the information for this on one of the most extensive HTML info websites I know, which is unfortunately not available in English at the moment but only in German and French: Selfhtml.
Thanks for reading and stay tuned. I am going to tell you how the vertical alignment of the button text works after my next visit to hell.
Popularity: 2% [?]