HighDots Forums  

style based on tag content

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss style based on tag content in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jpolaski@rgs.uci.edu
 
Posts: n/a

Default style based on tag content - 04-27-2006 , 01:17 PM






I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
<td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,

Jeff P.


Reply With Quote
  #2  
Old   
Ken Loomis
 
Posts: n/a

Default Re: style based on tag content - 04-27-2006 , 01:52 PM






On 27 Apr 2006 11:17:13 -0700, jpolaski (AT) rgs (DOT) uci.edu wrote:

Quote:
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...
As you process the data to put it in the table, check to see what it
is and then give the <td> the appropriate class.

For example in php:

$myClass="";
if ($row->status == "pos")
$myClass="pos";
if ($row->status == "pending")
$myClass="pending";

$myData .= "<td class=\"$myClass\">$row->status</td>";

Ken



Reply With Quote
  #3  
Old   
jpolaski@rgs.uci.edu
 
Posts: n/a

Default Re: style based on tag content - 04-27-2006 , 02:20 PM



The data isn't in a database for this one... This data get changed
rarely, so it isn't really worth the extra complexity of stuffing
everyting in a db.

Also, it just seems like there should be a simple way to do it with
style sheets. But it looks like you can do styles with *tags*, but not
*content*. Which is a bummer.

For example, it would be nice to italicize "et. al." or "in vivo"
across a site without having to add <i>, or div or span tags?

Don't you think there should be something like:

td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}

~Jeff P.


Reply With Quote
  #4  
Old   
Ken Loomis
 
Posts: n/a

Default Re: style based on tag content - 04-27-2006 , 02:35 PM



On 27 Apr 2006 12:20:03 -0700, jpolaski (AT) rgs (DOT) uci.edu wrote:

Quote:
Don't you think there should be something like:

td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}
It's called XML.

Ken



Reply With Quote
  #5  
Old   
Ken Loomis
 
Posts: n/a

Default Re: style based on tag content - 04-27-2006 , 02:41 PM



On Thu, 27 Apr 2006 15:35:51 -0400, Ken Loomis
<not_a_real_email (AT) address (DOT) com> wrote:

Quote:
td:content "pos" {
color: red;
}

or
content "in vivo" {
font: italic;
}

It's called XML.
Actually, come to think of it, even XML can't do that.

It has to be done programatically, AFAIK.
Quote:
Ken

Reply With Quote
  #6  
Old   
Martin Eyles
 
Posts: n/a

Default Re: style based on tag content - 05-02-2006 , 03:48 AM



The closest I can think of is attribute selectors.

http://www.w3.org/TR/CSS21/selector....bute-selectors

[att=val]
Match when the element's "att" attribute value is exactly "val".

However I don't think you can have content as the att. Also, this selector
is NOT cross browser friendly, as although my favourite browser (Firefox)
supports it, 90% of the world's browsers (Internet Explorer) do not.

Martin


<jpolaski (AT) rgs (DOT) uci.edu> wrote

Quote:
I'd like to change the color of text in a table data field based on the
content...

For example, I'd like all <td>pos</td> to show the "pos" in red. All
td>pending</td> should show as the normal black. I don't really want
to have to add a class or a style, but that's how I've been doing it so
far...

I seem to remember that this is possible, I just can't remember the
syntax right now and I'm not having any luck with google...

Thanks in advance,

Jeff P.




Reply With Quote
  #7  
Old   
dingbat@codesmiths.com
 
Posts: n/a

Default Re: style based on tag content - 05-02-2006 , 08:08 AM




Ken Loomis wrote:

Quote:
It's called XML.

Actually, come to think of it, even XML can't do that.
Try XSLT

Quote:
It has to be done programatically, AFAIK.
Yes, but that doesn't have to mean a procedural program language.
XSLT's declarative style makes it very easy.

It's pretty much trivial to take an XHTML document, then transform it
with XSLT to add suitable classes and style the results with CSS. The
transformed document can be practically identical to the original, just
with added classes.

Although you don't need to use XHTML to do this, it's notably easy to
do it with XSLT and a slightly modified XSLT identity transform.



Reply With Quote
  #8  
Old   
VK
 
Posts: n/a

Default Re: style based on tag content - 05-03-2006 , 04:32 AM




VK wrote:
Quote:
Use behaviors
Damn... Copying - Pasting code is a real challenge for weak minds like
mine... :-)

Quote:
-moz-binding: url(zebra.xml#default);
behavior: url(bycontent.htc);
of course:
-moz-binding: url(bycontent.xml#default);
behavior: url(bycontent.htc);

and respectively bycontent.xml is:

<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl">
<binding id="default">
<implementation>

<constructor><![CDATA[
var rL = this.rows.length;
var cL = 0;
var cR = null;
for (var i=0; i<rL; i++) {
cL = this.rows[i].cells.length;
for (var j=0; j<cL; j++) {
cR = this.rows[i].cells[j];
switch (cR.innerHTML) {
case 'pos' :
cR.style.color = '#FF0000';
break;
case 'at. al.' :
cR.style.fontStyle = 'italic';
break;
default: /*NOP*/
}
}
}
]]></constructor>


<destructor><![CDATA[
// your code here
]]></destructor>

</implementation>
</binding>
</bindings>


In my code I actually add innerText virtual property to the behavior
(binding) for more uniform looking and reliable code. If you don't use
HTML tags in your table cells like <span>pos</span> then the above is
good enough.



Reply With Quote
  #9  
Old   
jpolaski@rgs.uci.edu
 
Posts: n/a

Default Re: style based on tag content - 05-03-2006 , 12:59 PM



Well, yes it will work, but it's a lot of overhead. In this case I'd
rather just add a class to the columns I want made red, though.

I think it's better to keep things simple. If I went for your solution
(which *will* work) I'd have to convert all the HTML documents I have
into XHTML, and then set up XSLT to do what I want. Doesn't that seem a
bit much?

I'm changing the subject slightly, but doesn't it seem like one should
be able to use a simple CSS technique (like 'td:content "pos"' or
'content "in vivo"' from an earlier post)?

Don't get me wrong, I'm not expecting anyone here to add this feature
to CSS. I'm simply pointing out that, now that I think about it, it
looks like a shortcoming of CSS... CSS is ultimatly about styling
content, isn't it?

I think there should be a way to directly style *content*, not just
*tags*.


Reply With Quote
  #10  
Old   
jpolaski@rgs.uci.edu
 
Posts: n/a

Default Re: style based on tag content - 05-03-2006 , 01:00 PM



Thank you, that's what I was thinking of...


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.