Forum Moderators: not2easy

Message Too Old, No Replies

Can CSS swap text?

Looking for a funciton of CSS

         

JoeVodkaSauce

12:40 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Sorry for the very vague question but I'm relatively new to CSS and through my recent crash course in CSS I have not seen a particular function nor could I find an example for the following scenario:

We have a list of categories for our store which are currently styled in a table using CSS for text properties and link properties (color change and underlining). We are trying to write this menu so that when a user hovers over an item the "text" itself will swap to some other text (i.e. the word "antipasto" turns to "appetizers" on hover).

I have seen a lot of suggestions to use images for the swap or even have the text disappear and display a background image with the second text and I'm not opposed to trying these options but I'd like to keep the menu as clean as possible and not have to create a ton of images each time our categories change.

Does anyone know if this is possible and could they point me in a direction? I have no problem researching and doing my homework on this so no freebies necessary - just looking for a "yes this can be done in CSS" and here's a place to start or "no it cannot be done in CSS". Thanks in advance and sorry if this forum isn't the right place for this question.

Thanks in advance!

Regards,

Joe

Marshall

4:25 am on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Joe,

Welcome to WebmasterWorld.

There is a thread on this subject, but I do not remember where except it is under the CSS forum. Check the forum library. I do recall it has to do with putting a <span> in the <a> and the span is hidden on a:link and visible on a:hover. It's something like this:
<a href="whatever">LINK<span>Alt Text</span></a>

You can even assign exact positioning to the <span> too.

Off the top of my head, I cannot recall more specifics. Hope this helps.

Marshall

vincevincevince

5:57 am on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Building on Marshall's solution:


<style>
.switchable .alt {display:none}
.switchable:hover .default {display:none}
.switchable:hover .alt {display:inline}
</style>
<a href="#" class="switchable"><span class="default">Default Text</span><span class="alt">Alternative Text</span></a>

The above is tested in Firefox 1.5. The <a href=""> is used because that's the only element which Internet Explorer supports the :hover psuedoclass for. If you want to remove the underline and the colour, just add this to your style section:


.switchable {color:black;text-decoration:none}

JoeVodkaSauce

2:39 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



Thanks Marshall and vincevincevince!

I have been looking around this site - fantastic info out here I can't believe I didn't find you guys sooner! I'll take another look into the forum library as you suggested and also take a stab at the code with the suggestions you both gave.

I'll let you know what I find. Again - many thanks!

Marshall

2:45 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JoeVodkaSauce,

Glad to be of help.

vincevincevince,

Do you remember the thread I was talking about? It put the alt text for all the links in the same place, though your css works wonderfully. Anyway, I've been lookng for it, but finding the right search parameters can be a pain.

Marshall

JoeVodkaSauce

2:49 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



It worked! Thanks so much vincevincevince for the code snippet and also Marshall for the span suggestion! That's exactly what I was looking for. I can't believe how simple it was - I've been using the span tags and hover or a ton of other items and never thought about the "display:none" . Thanks once again! Hopefully I'll be able to learn enough of this to contribute as well!

JoeVodkaSauce

3:56 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



This is what I wound up using for my menu - obviously there's some extra styling I use for my layout. I am also sure that I am bloated with my code and need to learn how to clean it up. If you do come across that thread I'd love ot read it too. Thanks once again!

.switchableText {
font-family: Arial, sans-serif;
font-size: 12px;
text-transform: none;
color: #888785;
font-weight: 400;
text-indent: 10px;
line-height: 16px;
}
.switchableText .default {text-decoration: none; color: #888785;}
.switchableText .alt {display:none}
.switchableText:hover .default {display:none}
.switchableText:hover .alt {display:inline; text-decoration: underline; color: #234d9c;}

encyclo

8:31 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without wishing to throw a spanner in the works ;) I think it's important to consider the accessibility aspects of this technique. If CSS is disabled, you will see simply the following:

Default TextAlternative Text

CSS is not really designed for this, you should either consider using Javascript to display the alternative text, or find a method which is semantically more comprehensible, at the very least separating the two versions of the link text - for example, two separate links, one positioned over the other.

londrum

9:00 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if has a (tiny) downside with SEO as well, because the anchor text might not make much sense, or it might end up talking about two different things.
it is ingenious though

JoeVodkaSauce

3:46 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Hello all - thanks for the great advice - I actually thought life was great - tested on different versions of IE but Safari and FireFox have thrown in a monkey wrench. it seems that something in my CSS isn't playing nice non-IE browsers. Again - I'm a new one to CSS (about 3 weeks into it) so I haven't had the pleasure of experiencing all of these headaches. I'm done my homework for browser visitors to my site over the years and luckily 87%-ish are IE, 9% Safari and the rest Firefox and mixed but I still don't want to alienate anyone - especially new potential customers.

So - I think I'll play around with my stylesheet for a bit and if I can't find the issue then it's off to JavaScript.

Funny about the browsers and SEO - I figured CSS would be the most compatible and most SEO friendly choice - that's why I was staying away form images with hover and trying to use just text wherever possible. Shouldn't this be the case?

Marshall

4:19 am on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JoeVodkaSauce,

There are many points of view on image vs. text links and I am sure it will be a debate that will never end, kind of like table vs. tableless layout. Regardless, IMHO, text links are better than images on so many levels:

1) SEO,
2) Download time,
3) Images that fail to download/browsers with images turned off,
4) Images are not necessarily fluid,
5) Accessibility,
6) and text is not at all confusing.

Need I say more.

Marshall

JoeVodkaSauce

1:22 am on Aug 25, 2007 (gmt 0)

10+ Year Member



Thanks for the info Marshall - I actually wound up changing over to a javascript text swap function. I couldn't get the safari and firefox browsers to see my text swap and also the formatting I wanted to achieve. this was very helpful though for me the css newbie and I will be using these boards a lot!

Thanks again!

--Joe

JoeVodkaSauce

9:34 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



Update on my CSS Text vs. JS Text vs. JS ImageSwap for a category menu issue:

I've done my homework and built a nice little menu in JS which will swap text. It works great in all browsers. The kicker is this - I've done a lot "more" homework and am finding that having a JS based menu may be detrimental to one's customer base. Meaning if end users don't have JS enabled then they're stuck with nowhere to go. I'm posting this back in the CSS forum because after many hours of thinking, talking, and reading on-line CSS text menus seem to be the most failsafe way to go.

So my problem is that the CSS menu with the text swap works great in IE 7. In FireFox it swaps text but the underline is always present, in IE 6 it swaps but then sticks and won't swap back when the mouse is moved off and in Safari it doesn't swap at all and the underline is always present.

I know we're not supposed to post URL's in this site so I've tried to explain what's happening the best I can. I'm wondering if anyone could take a look at my CSS code and then my menu code and see if I'm doing something way wrong here. Or at least point me in a good starting direction. For this example I've changed one item in the menu, "Formaggio" which is supposed to swap to "Cheese" on mouse over.

Thanks in advance for any help!

CSS code:

.switchableText {
font-family: Arial, sans-serif;
font-size: 12px;
text-transform: none;
color: #888785;
font-weight: 400;
text-indent: 10px;
line-height: 16px;
}

.switchableText .default {text-decoration: none; color: #888785;}
.switchableText .alt {display:none}
.switchableText:hover .default {display:none}
.switchableText:hover .alt {display:inline; text-decoration: underline; color: #234d9c;}

HTML:

<tr>
<td colspan="3">
<a href="merchant.mvc?Screen=CTGY&Store_Code=CFF&Category_Code=cheese" class="switchableText"><span class="default">Formaggio</span><span class="alt">Cheese</span></a></td>
</tr>