CSS:删除线未在文本中居中

发布于 2024-12-17 04:52:59 字数 265 浏览 1 评论 0原文

我们使用删除线来实际显示产品的旧价格与新价格。

如果您打开此链接,在页面底部,我们有一款产品正在促销。 http://www.gkelite.com/Gymnastics-Shopby-GiftoftheWeek

删除线因为旧的价格没有垂直居中。 任何人都可以帮忙解释为什么会发生这种情况吗?

We are using strike-through to actually show oldprice vs new price for a product.

If you open this link, at the bottom of the page, we have a product on sale.
http://www.gkelite.com/Gymnastics-Shopby-GiftoftheWeek

The strike-through for the old price is not centered vertically for the text.
Can any one help out as to why it's happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

负佳期 2024-12-24 04:52:59

我找到了一个利用伪元素 http://jsfiddle.net/urjhN/ 的解决方案

.strike-center {
    position: relative;
    white-space: nowrap; /* would center line-through in the middle of the wrapped lines */
}

.strike-center:after {
    border-top: 1px solid #000;
    position: absolute;
    content: "";
    right: 0;
    top:50%;
    left: 0;
}

,但是如果文本在行之间换行,该行将在这些行之间居中。

它似乎适用于所有主要浏览器;对于 IE7(及更早版本),您可以使用条件注释回退到经典的 line-through

I found a solution for it utilizing psuedo elements http://jsfiddle.net/urjhN/

.strike-center {
    position: relative;
    white-space: nowrap; /* would center line-through in the middle of the wrapped lines */
}

.strike-center:after {
    border-top: 1px solid #000;
    position: absolute;
    content: "";
    right: 0;
    top:50%;
    left: 0;
}

However this technique fails if the text is wrapped between lines, the line would be centered among those lines.

It seems to work among all major browsers; for IE7 (and prior) you could just fallback to the classic line-through using conditional comments.

失与倦" 2024-12-24 04:52:59

传统上,删除线是 x 高度(或小写“x”的高度)的某个百分比(70% 到 90%)。如果该行位于大写字母高度的 50%,则删除线可能会位于该组中任何小写字母的上方或顶部。删除线应该是在文本中的所有字母之间划一条线,这就是为什么从 x 高度进行测量的原因。

无论浏览器是否实际上使用 x 高度进行删除线计算,这一传统就是为什么删除线显示在大写字母高度的 50% 以下的原因。

有关术语的一些示例,请参阅下图:

在此处输入图像描述

A strike-through is traditionally some percentage (70% to 90%) of the x-height (or the height of a lower case 'x'). If the line were at the 50% of cap-height, it may be possible the strike-through would be above or at the top of any lowercase letter in the set. A strike-through is supposed to put a line through all letters in the text, which is why is gauged from the x-height.

Whether or not browsers actually use the x-height for their strikethrough calculation, this tradition is why is why strike-throughs are displayed short of the 50% of cap height.

See the following illustration for some examples of the terms:

enter image description here

懒猫 2024-12-24 04:52:59

无法控制删除线的偏移/位置,但是,有一种方法可以使用 text-decoration: underline< 来控制它/code> 使用以下组合:

span {
  background: lightyellow;

  text-decoration: underline;
  text-underline-offset: -40%; /* <-- this */
  text-decoration-skip-ink: none;
}

output { display:inline-block; width:50px; }
<output>-40%</output>
<input type='range' min='-100' max='100' value='-40' oninput='this.nextElementSibling.style.textUnderlineOffset=this.value+"%"; this.previousElementSibling.innerHTML=this.value+"%"'/>

<span>Text with strike-through</span>


There is no way to control the offset/placement of a strike-through, but, there is a way for controlling this using for text-decoration: underline using the below combination:

span {
  background: lightyellow;

  text-decoration: underline;
  text-underline-offset: -40%; /* <-- this */
  text-decoration-skip-ink: none;
}

output { display:inline-block; width:50px; }
<output>-40%</output>
<input type='range' min='-100' max='100' value='-40' oninput='this.nextElementSibling.style.textUnderlineOffset=this.value+"%"; this.previousElementSibling.innerHTML=this.value+"%"'/>

<span>Text with strike-through</span>


???? Here's a Codepen of mine with an alternative strike-through approach:
https://codepen.io/vsync/pen/KKQoVRZ

掩耳倾听 2024-12-24 04:52:59

它帮助我。只需为 iOS 设备设置您的 top 即可。

// Strike through
.strike-center {
    position: relative;
    white-space: nowrap;
}

.strike-center:after {
    border-top: 1px solid #000;
    position: absolute;
    content: "";
    right: 0;
    top: 49%;
    left: 0;
}

@supports (-webkit-touch-callout: none) {

    /* CSS specific to iOS devices */
    .strike-center:after {
        top: 40% !important;
    }
}

It help me. Just setting your top for iOS devices.

// Strike through
.strike-center {
    position: relative;
    white-space: nowrap;
}

.strike-center:after {
    border-top: 1px solid #000;
    position: absolute;
    content: "";
    right: 0;
    top: 49%;
    left: 0;
}

@supports (-webkit-touch-callout: none) {

    /* CSS specific to iOS devices */
    .strike-center:after {
        top: 40% !important;
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文