DEV Community

Samantha Ming
Samantha Ming

Posted on β€’ Originally published at samanthaming.com

28 4

Strikethrough using HTML5

Code Tidbit by SamanthaMing.com

CSS is great for styling, but not so good for providing meaning of your content. That's what HTML semantic tags are for. When you're trying to strikethrough content, there's typically a reason you're doing that. And that reason can be described using the proper HTML tag. Communicate that using <del> or <s> and give your code semantic meaning πŸ™Œ

<s>Text no longer relevant</s>

Text no longer relevant

<del>Text removed from document</del>
Text removed from document

<s> vs <del>

Yes, they are both strikethroughs. However, they convey different meaning about the content. So they are not interchangeable. You want to pick the one that reflects what you are trying to achieve. Let's start with the definition.

<s>

Use this when you are trying to represent things that are no longer relevant or not longer accurate.

βœ… A good example of this is for Price Discount

<p><s>$100</s></p>
<p>$999.99</p>

On sale!

$100
$899

❌ However, this tag shouldn't be used when indicating document edits. That's where <del> come in.

<del>

Use this when you want to indicated something is removed from the document

βœ… A good example of this is for Todo List

<p>TODO</p>

<ul>
  <li><del>Get a Job</del></li>
  <li>Become a Senior Developer</li>
</ul>

TODO

  • Get a Job
  • Become a Senior Developer

Rule

Here's how I remember which to use. If the content represents something deleted, use <del>. And for all other instances, use the <s> πŸ‘


Using <del> with <ins>

Here's where I think <del> really shines. You can pair it up for <ins> and use them together to create an interface that track and record changes that happened in your document.

Example: Text Editor

<p>My name is
  <del>Smanta</del>
  <ins>Samantha</ins>
</p>

My name is Smanta  Samantha

Example: Git Interface

<table>
  <tr>
    <td><del>+ function TEA() {</del></td>
    <td><ins>- function tea() {</ins></td>
  </tr>
</table>
function TEA(){- function tea(){

<strike>

You might have seen the <strike> tag. This is actually the old tag that was deprecated in HTML4. It was replaced with a more semantically appropriate <s> and <del> tags, which was introduced in HTML5.

The <del> and <s> is quite well supported, even in Internet Explorer. Although some browser may still support the <strike> tags, it's best practice to avoid it πŸ™…β€β™€οΈ


Accessibility Concerns

These particular tags, unfortunately, won’t be read by most screen readers. But you can use ::before and ::after property to have it announced. However best not to abuse this technique as some people deliberately disable announcing content that creates verbosity ✨

<del>

del::before,
del::after {
  clip-path: inset(100%);
  clip: rect(1px, 1px, 1px, 1px);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

del::before {
  content: " [deletion start] ";
}

del::after {
  content: " [deletion end] ";
}

/* MDN */

<s>

s::before, 
s::after {
  clip-path: inset(100%);
  clip: rect(1px, 1px, 1px, 1px);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

s::before {
  content: " [start of stricken text] ";
}

s::after {
  content: " [end of stricken text] ";
}

/* MDN */

Community Inputs

πŸ’¬ What are some HTML strikethrough use cases you've seen? Let's take a look at what the community said πŸ‘€

  • @its4zahoor: I have seen it a lot in Price Discount of Product promotions Screens. Case: Strike through the real price & show discounted one along

  • @jamielarchin__: In an account creation form when there is a password field, list the password requirements and then strikethrough them as the user has completed each one.

  • @kotpes: Chat apps

  • @ultrasamad: Seen in completed todo list items


Resources


Thanks for reading ❀
Say Hello! Instagram | Twitter | Facebook | Blog | SamanthaMing.com

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (7)

Collapse
 
skhmt profile image
Mike πŸˆβ€β¬› β€’ β€’ Edited

<s> doesn't seem to work on chrome, none of the <s> examples in your article show a strikethrough to me. Chrome 75, Win64.

edit: nevermind, inspected the doc and the examples just aren't wrapped with <s> in the article I'm seeing.

Collapse
 
samanthaming profile image
Samantha Ming β€’

Cool, glad you got it to work πŸ‘

Collapse
 
cristiano profile image
cristiano β€’

Amazing! Thank you for adding a few remarks on accessibility as well. πŸ™Œ

Collapse
 
samanthaming profile image
Samantha Ming β€’

Totally! Accessibility is a topic that I missed when I first started web programming. But it’s super important. We talk about how the internet is inclusive, accessibility is a big part for that. So it’s a topic I believe every web developer need to be incorporate in their code πŸ˜ŠπŸ‘

Collapse
 
mercier_remi profile image
RΓ©mi Mercier β€’

Constantly amazed by HTML's semantic. Thanks for sharing this + accessibility tricks! πŸ‘

Collapse
 
samanthaming profile image
Samantha Ming β€’

You’re welcome! Accessibility is a topic I want to talk more about! So expect more, possibly as a series πŸ˜πŸ‘

Collapse
 
its4zahoor profile image
Abdul Zahoor Malik πŸ‡΅πŸ‡° β€’

LoL I see my feedback above :-). I just joined dev.to and was searching my welcome post. Searched my username and landed here :-D. Thanks for adding my valuable feedback :-)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay