如何在表格排序标题中使用 CSS 精灵?

发布于 2024-11-29 19:14:40 字数 820 浏览 1 评论 0原文

我有一张已排序的桌子。标题有背景图像(箭头)来显示排序方向。

当前的 CSS 使用 3 个不同的图像,如下所示:

th {
    padding-right: 21px;
}
th.sorting {
    background: #EEEEEC url("table-sort.png") no-repeat center right;
}
th.sorting_asc {
    background: #ECE0EB url("table-sort-asc.png") no-repeat center right;
}
th.sorting_desc {
    background: #ECE0EB url("table-sort-desc.png") no-repeat center right;
}

JSfiddle 中的工作示例

有没有办法将它们减少为一张图像并使用 CSS 精灵?问题是合并图像不能简单地用作标题单元格的背景,因为多个图像可能会同时可见,点此

如果可能的话,我想避免使用额外的元素。 IE7 支持会很棒,但没有它我可能也活不下去。

:after 这样的伪元素可以工作,但我找不到以相同方式定位图标的方法。 JSfiddle 示例

I have a table that gets sorted. The headers have background images (arrows) to show the sorting direction.

The current CSS uses 3 different images like this:

th {
    padding-right: 21px;
}
th.sorting {
    background: #EEEEEC url("table-sort.png") no-repeat center right;
}
th.sorting_asc {
    background: #ECE0EB url("table-sort-asc.png") no-repeat center right;
}
th.sorting_desc {
    background: #ECE0EB url("table-sort-desc.png") no-repeat center right;
}

Working example in the JSfiddle here.

Is there a way to reduce these to one image and use CSS sprites? The problem is that a merged image cannot simply be used as a background to the header cell, because multiple images may become visible at once, like here.

I'd like to avoid using extra elements if possible. IE7 support would be great but I could probably live without it.

Pseudo elements like :after could work, but I can't find a way to position the icons in the same way. JSfiddle example.

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

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

发布评论

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

评论(3

梦一生花开无言 2024-12-06 19:14:40

我找到了一种让伪元素发挥作用的方法。将表格标题设置为 position:relative,然后如下所示:

.sorting:before {
    display: block;
    content: "";
    position: absolute;
    top: 50%;
    right: 8px;
    width: 7px;
    height: 9px;
    margin-top:-4px;
    background: transparent url("http://i.imgur.com/iONZm.png") 0 0;
}

图标位于距顶部 50% 的位置,然后向上移动几个像素以垂直居中。

I figured out a way for pseudo elements to work. Set the table headers as position: relative, then something like this:

.sorting:before {
    display: block;
    content: "";
    position: absolute;
    top: 50%;
    right: 8px;
    width: 7px;
    height: 9px;
    margin-top:-4px;
    background: transparent url("http://i.imgur.com/iONZm.png") 0 0;
}

The icon is positioned 50% from the top, then moved upward a few pixels to be vertically centered.

征﹌骨岁月お 2024-12-06 19:14:40

您可以使用 SpriteMe 在您的网站上生成精灵图像。

排序精灵

.sorting_asc {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px 0px;
}
.sorting {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px -27px;
}
.sorting_desc {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px -53px;
}

You can use SpriteMe to generate sprite images on your site.

sort sprite

.sorting_asc {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px 0px;
}
.sorting {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px -27px;
}
.sorting_desc {
  background-image: url(http://www.jaredhirsch.com/coolrunnings/public_images/5b6b9013a6/spriteme1.png);
  background-position: 32px -53px;
}
高跟鞋的旋律 2024-12-06 19:14:40

快速 Google 搜索 CSS sprite 生成器 提供了一些选项。虽然我自己从未使用过任何这些服务,但我通常会制作一张具有透明度的 PNG 图像。然后,您将像这样引用 CSS:

th {
    padding-right: 21px;
    background: #EEEEEC url("table-sort.png") no-repeat center right;
}
th.sorting {
    background-position: -100px -100px;
}
th.sorting_asc {
    background-position: -200px -200px;
}
th.sorting_desc {
    background-position: -300px -300px;
}

background-position 属性值替换为适当的坐标。我认为 CSS sprite 服务可以在压缩完成后为您创建这些值,但 CSS 坐标可能需要一些调整才能完全达到您想要的效果。

A quick Google search for a CSS sprite generator comes up with a few options. Though I've never used any of these services myself, I have usually made a single PNG image with transparency. Then you would reference your CSS like this:

th {
    padding-right: 21px;
    background: #EEEEEC url("table-sort.png") no-repeat center right;
}
th.sorting {
    background-position: -100px -100px;
}
th.sorting_asc {
    background-position: -200px -200px;
}
th.sorting_desc {
    background-position: -300px -300px;
}

Replace the background-position property values with the appropriate coordinates. I think the CSS sprite services can create these values for you based on their compression once it is done, but the CSS coordinates may need some tweaks to get it exactly how you want.

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