Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the strike-out / line-through thickness in CSS?

I'm using the text-decoration: line-through in CSS, but I can't seem to find any way to vary the line thickness without inelegant hacks like <hr> or image overlays.

Is there any elegant way to specify the thickness of a line-through?

like image 613
1077 Avatar asked Mar 29 '10 15:03

1077


People also ask

How do I change the thickness of a line in CSS?

If you want to change the thickness, or height of your horizontal line, add the height property to your <hr> style. In this case, you can also set the background-color property for the thick horizontal line.

How do you do a strikethrough in CSS?

You can also use the <del> tag for crossed-out text, which is more semantically correct. However, the <del> tag doesn't always work in all browsers. So, if you need to ensure that your text is readable in any browser, the <strike> tag is a good fallback. CSS can also be used to style strikethrough text.

How do you make a line thinner in CSS?

Simply use opacity to give the impression that the line is thinner than 1px.


1 Answers

The modern solution is to use the text-decoration-thickness property.

text-decoration-thickness: 1px; 

You can also change the color using the text-decoration-color property.

text-decoration-color: #ff0000aa; 

For older browsers, you can use one of these workarounds:

Here's a pure CSS method that doesn't require any unnecessary wrapper elements. As an added bonus, not only can you adjust the thickness of the strikeout, but you can control its color separately from the text color:

.strikeout {   font-size: 4em;   line-height: 1em;   position: relative; } .strikeout::after {   border-bottom: 0.125em solid red;   content: "";   left: 0;   margin-top: calc(0.125em / 2 * -1);   position: absolute;   right: 0;   top: 50%; }
<span class="strikeout">Struck out text</span>

Use RGBa colors to make the strikeout semi-transparent:

.strikeout {   font-size: 4em;   position: relative; } .strikeout::after {   border-bottom: 0.125em solid rgba(255, 0, 0, 0.5);   content: "";   left: 0;   line-height: 1em;   margin-top: calc(0.125em / 2 * -1);   position: absolute;   right: 0;   top: 50%; }
<span class="strikeout">Struck out text</span>

Or even make the strikeout a gradient! Just use a background combined with a height, in place of a border:

.strikeout {   font-size: 4em;   line-height: 1em;   position: relative; } .strikeout::after {   background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 0, 0, 1), rgba(0, 255, 0, 1), rgba(0, 0, 255, 1), rgba(255, 255, 255, 0));   content: "";   height: 0.125em;   left: 0;   margin-top: calc(0.125em / 2 * -1);   position: absolute;   right: 0;   top: 50%; }
<span class="strikeout">Struck out text</span>

This works in IE9 (sans gradient) and up – or even IE8 if you use the single-colon :after syntax and manually write the negative margin-top value instead of using calc().

The main downside is that this only works on a single line of text. But hey, you take what you can get ;-)

like image 179
daGUY Avatar answered Nov 04 '22 04:11

daGUY