Explain Codes LogoExplain Codes Logo

Css strikethrough different color from text?

html
css
strikethrough
browser-compatibility
Nikita BarsukovbyNikita Barsukov·Aug 8, 2024
TLDR

Quickly achieve a strikethrough with a different color from the text using text-decoration-color in CSS:

<!-- Because we all love multicolored texts, don't we? --> <span style="text-decoration: line-through; text-decoration-color: red; color: blue;">Strikethrough in red, text in blue. What a sight!</span>

This snippet offers a red strikethrough over blue text. A stunning visual effect for your website.

Detailed application and use-cases

Let's delve deeper into advanced techniques and important considerations when implementing a strikethrough with a different color.

Custom strikethrough creation

By leveraging the CSS pseudo-element ::after along with border-top, you can custom design your strikethrough. Typically, this approach is advantageous for angling your strikethrough or adjusting its precise placement over the text:

/* Because right angles are mainstream */ .strikethrough-custom::after { content: ''; position: absolute; left: 0; right: 0; top: 50%; border-top: 2px solid red; transform: rotate(-10deg); }

Compatibility across browsers

The text-decoration-color property enjoys support in most modern browsers. However, Edge and IE (Internet Explorer) might not render it correctly. Always check browser compatibility and provide fallbacks or alternatives for users on different browser versions.

Design considerations

To ensure readability, select contrasting colors for your strikethrough and text. Be sure to test your design on devices of various sizes to maintain visual consistency.

Extra tips

For performance considerations, use the ::after method cautiously on large blocks of text. Also, be mindful that relying on !important in CSS might introduce styling conflicts.

Special effects and cases

Applying hover effects

The :hover pseudo-class allows you to apply a strikethrough on hover. However, in IE7, ensure an href exists in the <a> tag for the hover to take effect, a behavior not consistent with Firefox or WebKit-based browsers.

Implementing angled strikethroughs

Use CSS transform on the ::after pseudo-element for angled strikethroughs to add visual appeal and dynamism.

Strikethrough animation

Using keyframes and animations in CSS, create exciting strikethroughs that animate.

Upcoming CSS3 features

Stay updated with emerging CSS3 features for potential new ways to style text decoration like text-decoration-thickness for adjusting strikethrough width.