?
Solved

Changing text color in an HTML table

Posted on 2001-08-09
20
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 

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 200 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 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