views:

3018

answers:

3

The HTML elements del or strike, and the CSS text-decoration property with a value line-through, may all be used for a text strike-through effect. Examples:

    <del>del</del>

...gives: del

    <strike>strike</strike>

....gives: strike

    <span style='text-decoration:line-through'>
     text-decoration:line-through
    </span>

...will also look the same as: text-decoration:line-through

However, the strikethrough line is typically the same color as the text.

Can CSS be used to make the line a different color?

+20  A: 

Yes, by adding an extra wrapping element. Assign the desired line-through color to an outer element, then the desired text color to the inner element. For example:

   <span style='color:red;text-decoration:line-through'>
    <span style='color:black'>black with red strikethrough</span>
   </span>

...or...

   <strike style='color:red'>
    <span style='color:black'>black with red strikethrough<span>
   </strike>


To make the strikethrough appear for a:hover, an external stylesheet must be used. (The :hover pseudo-class can't be applied with inline styles.) For example:

<head>
 <style>
  a.redStrikeHover:hover {
   color:red;
   text-decoration:line-through;
  }
 </style>
</head>
<body>
 <a href='#' class='redStrikeHover'>
  <span style='color:black'>hover me</span>
 </a>
</body>

(IE7 seems to require some 'href' be set on the <a> before :hover has an effect; FF and webkit-based browsers do not.)

gojomo
+1 I am delighted this works.
Thomas L Holaday
So much for my "that's impossible!" answer.
John Kugelman
Can this be done for a:hover?
tharkun
Yes, in my tests in FF3+, IE7+, Chrome. See added section, above.
gojomo
A: 

Brilliant! Thank you.

John