Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strike through obliquely with css

I need something like this:

How can achieve this with css? I know that one way is use background image, but can I achieve this only with css without any image?

like image 268
Jaroslav Klimčík Avatar asked Oct 06 '22 04:10

Jaroslav Klimčík


People also ask

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 I make an oblique line in CSS?

In order to make oblique lines with CSS, there are two main approaches. The first approach involves the clip-path property of CSS and in the second approach, we use transform property with skew() of CSS. Approach 1: Using clip-path property: The clip-path property is generally used to clip an element to a basic shape.

How do I strikethrough a div?

In the same manner as underlining a text, you can do a strikethrough, where a horizontal line goes through the text. Strikethrough/line through can only be done in one way: STYLE="text-decoration: line-through". The style has to be used on a tag for text sections, e.g. <P>, <DIV> and <SPAN>.

Why some CSS are strikethrough?

If you can see your new CSS in the Styles pane, but your new CSS is crossed out, it means that there's some other CSS that is overriding your new CSS. In CSS terminology this concept is called Specificity. Chrome DevTools can help you find the old CSS that is causing your new CSS to not be applied.


1 Answers

There is a hacky way to do this, using the :before pseudo element. You give the :before a border, then rotate it with a CSS transform. Doing it this way adds no extra elements to the DOM, and adding/removing the strikethrough is a simple as adding/removing the class.

Here's a demo

Caveats

  • This will only work down to IE8. IE7 does not support :before, however will degrade gracefully in browsers that do support :before but don't support CSS transforms.
  • The angle of rotation is fixed. If the text is longer, the line will not touch the corners of the text. Be mindful of this.

CSS

.strikethrough {
  position: relative;
}
.strikethrough:before {
  position: absolute;
  content: "";
  left: 0;
  top: 50%;
  right: 0;
  border-top: 1px solid;
  border-color: inherit;
  
  -webkit-transform:rotate(-5deg);
  -moz-transform:rotate(-5deg);
  -ms-transform:rotate(-5deg);
  -o-transform:rotate(-5deg);
  transform:rotate(-5deg);
}

HTML

<span class="strikethrough">Deleted text</span>
like image 220
Bojangles Avatar answered Oct 08 '22 17:10

Bojangles