Solved

Changing text color in an HTML table

Posted on 2001-08-09
20
250 Views
Last Modified: 2008-03-06
I am attempting to create a table that starts off with a light green background color and dark green text and when the mouse moves over a cell, I want to change the cell to dark green and the text to bright green.  What I can't seem to cause is the initial text to be green.  If I put a <font> tag directly after a <td> tag, the color will be green but it won't change on the mouseover event.  The cell changes but the text does not.

Below is my latest attempt...anyone know how to make this work?



<table border=1 bgcolor="66CC99" width=100% cellspacing=0 cellpadding=0
                                                               >
  <tr>
    <td width=20% onMouseover="this.bgColor='339966',
      onMouseover=this.style.color='33FF99'"
      onMouseout ="this.bgColor='66CC99',
      onMouseout =this.style.color='006633'"><b>Home</td>

    <td width=20% onMouseover="this.bgColor='339966',
      onMouseover=this.style.color='33FF99'"
      onMouseout ="this.bgColor='66CC99',
      onMouseout =this.style.color='006633'"><b>Clinical</td>

    <td width=20% onMouseover="this.bgColor='339966',
      onMouseover=this.style.color='33FF99'"
      onMouseout ="this.bgColor='66CC99',
      onMouseout =this.style.color='006633'"><font color="006633"><b>Financial</font></td>

    <td width=20% onMouseover="this.bgColor='339966',
      onMouseover=this.style.color='33FF99'"
      onMouseout ="this.bgColor='66CC99',
      onMouseout =this.style.color='006633'"><b>Operations</td>

    <td width=20% onMouseover="this.bgColor='339966',
      onMouseover=this.style.color='33FF99'"
      onMouseout ="this.bgColor='66CC99',
      onMouseout =this.style.color='006633'"><b>Administration</td>

  </tr>

</table>
0
Comment
Question by:mskvarenina
  • 7
  • 6
  • 2
  • +3
20 Comments
 
LVL 2

Expert Comment

by:tewald
ID: 6371367
All you'd have to do is set the initial style of the cells.  See the <style> tag in the <head> section - this sets the color of all table cells to #339966 (or whatever you want).  Then your mouse events will overide the initial color value.

<html>
<head>
<title>Font Color</title>
<style type="text/css">
td {
     color: #339966;
}
</style>
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" cellspacing="0" cellpadding="0">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
   <td width=20% onMouseover="this.bgColor='339966',
     onMouseover=this.style.color='33FF99'"
     onMouseout ="this.bgColor='66CC99',
     onMouseout =this.style.color='006633'"><b>Home</td>

   <td width=20% onMouseover="this.bgColor='339966',
     onMouseover=this.style.color='33FF99'"
     onMouseout ="this.bgColor='66CC99',
     onMouseout =this.style.color='006633'"><b>Clinical</td>

   <td width=20% onMouseover="this.bgColor='339966',
     onMouseover=this.style.color='33FF99'"
     onMouseout ="this.bgColor='66CC99',
     onMouseout =this.style.color='006633'"><font color="006633"><b>Financial</font></td>

   <td width=20% onMouseover="this.bgColor='339966',
     onMouseover=this.style.color='33FF99'"
     onMouseout ="this.bgColor='66CC99',
     onMouseout =this.style.color='006633'"><b>Operations</td>

   <td width=20% onMouseover="this.bgColor='339966',
     onMouseover=this.style.color='33FF99'"
     onMouseout ="this.bgColor='66CC99',
     onMouseout =this.style.color='006633'"><b>Administration</td>

 </tr>
</table>
</body>
</html>
0
 

Author Comment

by:mskvarenina
ID: 6371912
tewald:  I havn't worked with CSS yet, I've been doing HTML for about a month (18 year veteran programmer now moving to web development).  One question though...for this page I have 4 tables actually and the one shown is the only one I want to effect.  Sounds like your solution would 'override' the text color for each table?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6372664
put this in the head

<style>
   .class1 {background-color:#339966;color:#33ff99}
   .class2 {background-color:#66cc99;color:#006633}
</style>

Then do the table this way.

<table border=1 bgcolor="66CC99" width=100% cellspacing=0 cellpadding=0
                                                              >
 <tr>
   <td width=20% class='class2" onMouseover="this.className='class1'"
       onMouseout ="this.className='class2'"><b>Home</td>

   Just repeat the same thding for each cell

 </tr>

</table>


Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6372668
BTW,

Does not work in Netscrap 4.x, because it does not support mouse events on cells

Cd&
0
 
LVL 2

Expert Comment

by:tewald
ID: 6373004
mskvarenina, you are correct.  By setting the style for a <td> tag (as I showed) you define the appearance for all <td> tags on the page.  You get around this by using the technique COBOLDinosaur demonstrated: class selectors.  CD's answer is the correct approach, but the following is just to help clarify how to use a class selector.

Instead of using td in the style, use a name that makes sense for what you're applying the style to.  The name must follow a "." as shown below.  Within the brackets you may list as many elements as you'd like, but each must be separated by a ";".

<head>
<style type="text/css">
.initialColor { color: #339966; }
</style>
</head>

Then, using one of your cells as an example, you apply the style using the following syntax:

<td class="initialColor" width=20%
 onMouseover="this.bgColor='339966',
 onMouseover=this.style.color='33FF99'"
 onMouseout ="this.bgColor='66CC99',
 onMouseout =this.style.color='006633'"><b>Home</td>

Using this approach let's you specify which style, if any, to apply to each <td> element.  You could setup different class selectors and apply them to just about any element on the page, not just <td>s.

tewald
0
 
LVL 5

Expert Comment

by:raizon
ID: 6376137
mskvarenina, the simplist way would be to follow the steps that COBOL layed out for you.
0
 

Expert Comment

by:lauciricad
ID: 6389729
ALCON,

I'll throw some points out there if someone can show code modifications for changing the text font color of hyperlinked text, using the code COBAL desplayed.

Good question mskvarenina, sorry for the intrude.

Don L.
0
 

Expert Comment

by:chromonium
ID: 6394323
use .a { ... } would change all hyperlinks

sure it wouldnt matter?

just insert in href tag or <a>
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6394393
Using a tag would only chane the link not the cell background... time to lock it... anyone object?

Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6394394
Using a tag would only chane the link not the cell background... time to lock it... anyone object?

Cd&
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:chromonium
ID: 6394428
what do you mean... lock it??
0
 
LVL 5

Expert Comment

by:raizon
ID: 6394438
Lock it Cobol.  The question has been answered with code.
0
 

Expert Comment

by:chromonium
ID: 6394552
yes I object
0
 

Expert Comment

by:chromonium
ID: 6394553
use .a { ... } would change all hyperlinks

sure it wouldnt matter?

just insert in href tag or <a>
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 50 total points
ID: 6394587
chromonium,

Welcome to Experts-exchange.

If you read the question you will note the the questioner wants to change the attributes on a cell. Using a link will only change the link, not the cell.

Answer in code supplied above.

Cd&

0
 

Expert Comment

by:chromonium
ID: 6394610
yeah, using css will work regardless of the content, providing it exists
0
 

Expert Comment

by:chromonium
ID: 6394645
why lock it???
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6394710
>>> why lock it?

Because it is answered the questioner has not commented since Aug 10 when they got their answer and left.

lauciricad,

If you want use that technique for links set the styles this way:

<style>
  .class1 a {background-color:#339966;color:#33ff99}
  .class2 a {background-color:#66cc99;color:#006633}
</style>

Then put the link in a span with the class applied.


or just apply the class to the link.

Cd&
0
 

Author Comment

by:mskvarenina
ID: 6397006
COBOLdinosaur, I didn't leave I was just out of town.  Your solution looks great...thanks for the help!
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 6397070
Glad to help thanks for the A. :^)

Cd&
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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