HTML元素del、strike或s都可以用于文本删除效果。例子:

<del>del</del>

....德尔给:

<strike>strike</strike> and <s>strike</s>

....给予:罢工和罢工

值为line-through的CSS text-decoration属性也可以类似地使用。代码…

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

...也会渲染成这样:文本装饰:线贯穿

但是,划线通常与文本的颜色相同。

CSS可以用来使线的颜色不同吗?


当前回答

下面是一种使用渐变来伪造线条的方法。它使用多行换行,不需要额外的DOM元素。但由于它是背景渐变,所以它在文本后面……

del, strike {
  text-decoration: none;
  line-height: 1.4;
  background-image: -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0.63em, transparent), color-stop(0.63em, #ff0000), color-stop(0.7em, #ff0000), color-stop(0.7em, transparent), to(transparent));
  background-image: -webkit-linear-gradient(top, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  background-image: -o-linear-gradient(top, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  background-image: linear-gradient(to bottom, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  -webkit-background-size: 1.4em 1.4em;
  background-size: 1.4em 1.4em;
  background-repeat: repeat;
}

参见fiddle: http://jsfiddle.net/YSvaY/

渐变色位和背景大小取决于行高。(我使用LESS进行计算,然后使用自动修复…)

其他回答

将所需的贯穿颜色分配给父元素也适用于删除的文本元素(<del>)—假设客户端将<del>呈现为贯穿颜色。

http://jsfiddle.net/kpowz/vn9RC/

我使用了一个空的:after元素,并在它上面装饰了一个边框。您甚至可以使用CSS转换来旋转斜线。结果:纯CSS,没有额外的HTML元素!缺点:不跨多行换行,尽管在我看来,你不应该在大块文本上使用擦除线。

s, strike { text-decoration: none; /*we're replacing the default line-through*/ position: relative; display: inline-block; /* keeps it from wrapping across multiple lines */ } s:after, strike:after { content: ""; /* required property */ position: absolute; bottom: 0; left: 0; border-top: 2px solid red; height: 45%; /* adjust as necessary, depending on line thickness */ /* or use calc() if you don't need to support IE8: */ height: calc(50% - 1px); /* 1px = half the line thickness */ width: 100%; transform: rotateZ(-4deg); } <p>Here comes some <strike>strike-through</strike> text!</p>

根据我的经验

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

不是最好的选择。我有一个同事在没有测试跨浏览器的情况下使用了这个方法,所以我不得不回去修复它,因为它在firefox中引起了问题。我个人的建议是使用:after选择器来创建擦除线。这样它就可以回到IE8,如果你真的想,没有任何风格冲突,以及在所有其他浏览器坚实。

它还创建了更少的标记和大致相同的样式,在我看来这是一个相当大的问题。

所以如果其他人遇到类似的问题,希望这能帮助他们:

.lineThrough {
    position: relative;

   &:after {
      content: "  ";
      display: block;
      width: 60px;
      height: 1px;
      background: red;
      position: absolute;
      top: 49%;
      left: 50%;
      margin-left: -30px;
   }
}

显然,你可以使用transform: translate来代替margin,但是这个例子是在IE8上运行的

是的,通过添加额外的包装元素。为外部元素指定所需的贯穿线颜色,然后将所需的文本颜色分配给内部元素。例如:

< span风格= '颜色:红色;文字修饰:线穿过的> <span style='color:黑色'>黑色加红色擦除线</span> < / span >

…或… <罢工风格= '颜色:红色' > <span style='color:黑色'>黑色加红色擦除线<span> > < /罢工

(但是请注意,<strike>在HTML4中被认为已弃用,在HTML5中已过时(参见W3.org)。推荐的方法是使用<del>如果删除的真正含义是,或者使用<s>元素或样式与文本装饰CSS在这里的第一个例子。)

To make the strikethrough appear for a:hover, an explicit stylesheet (declared or referenced in <HEAD>) must be used. (The :hover pseudo-class can't be applied with inline STYLE attributes.) 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.)

下面是一种使用渐变来伪造线条的方法。它使用多行换行,不需要额外的DOM元素。但由于它是背景渐变,所以它在文本后面……

del, strike {
  text-decoration: none;
  line-height: 1.4;
  background-image: -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0.63em, transparent), color-stop(0.63em, #ff0000), color-stop(0.7em, #ff0000), color-stop(0.7em, transparent), to(transparent));
  background-image: -webkit-linear-gradient(top, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  background-image: -o-linear-gradient(top, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  background-image: linear-gradient(to bottom, transparent 0em, transparent 0.63em, #ff0000 0.63em, #ff0000 0.7em, transparent 0.7em, transparent 1.4em);
  -webkit-background-size: 1.4em 1.4em;
  background-size: 1.4em 1.4em;
  background-repeat: repeat;
}

参见fiddle: http://jsfiddle.net/YSvaY/

渐变色位和背景大小取决于行高。(我使用LESS进行计算,然后使用自动修复…)