我可以在提交按钮上使用 csssprite

发布于 2024-07-27 03:39:58 字数 979 浏览 4 评论 0原文

我想使用 csssprites 作为提交按钮有两个原因:

  • 我动态创建了按钮,将来可能会本地化
  • 我只需要 1 个 HTTP 请求,即使页面上有 5 个按钮

问题是我想避免按钮使用 javascript,因此我需要使用 input 字段输入图像。 我可以在这个字段上设置背景图像,就像我为任何 csssprite 所做的那样。

问题是我发现我必须将图像源设置为空像素,否则我会得到一个损坏的图像图标。

话虽如此,这是我想出的最佳解决方案:(

<style>
    .csssprite_cat {
        background-image:url(http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg); width: 50px;
        height: 50px;
    }
</style>

<input type=image src="http://www.grasse-ac.com/gif/no_pixel.gif" class="csssprite_cat">
<input type=image class="csssprite_cat">

您可以直接将此文件加载到浏览器中 - 我正在从随机站点借用图像)。

它工作得还不错,但我必须使用我们的好老朋友 pixel.gif。 很怀旧!

没有 javascript 可能没有更好的方法,除非 css 中有一种方法可以隐藏损坏的图像文本和图标。

I have two reasons I want to use csssprites for submit buttons :

  • I have dynamically created button, which in the future might be localized
  • I want just 1 HTTP request even if I have 5 buttons on the page

The problem is that I want to avoid javascript for buttons, and therefore I need to use an input field of type image. I can set the background image on this field just as i would for any csssprite.

The problem is that I found I had to set the image source to an empty pixel or else I get a broken image icon.

With that said, this is the best solution I have come up with :

<style>
    .csssprite_cat {
        background-image:url(http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg); width: 50px;
        height: 50px;
    }
</style>

<input type=image src="http://www.grasse-ac.com/gif/no_pixel.gif" class="csssprite_cat">
<input type=image class="csssprite_cat">

(You can just load this file directly into a browser - i am borrowing images from a random site).

It works kind of OK, but I have to use our good old friend pixel.gif. Very nostalgic!

There probably isn't a better way without javascript, unless there is a way in css to hide the broken image text and icon.

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

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

发布评论

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

评论(2

一曲爱恨情仇 2024-08-03 03:39:58

不要使用图像输入类型,最好使用沼泽标准提交按钮并以这种方式设置样式。

定义背景图像后,您需要为要使用的每个不同按钮定义唯一的背景位置

为了向后兼容,我建议使用类混合(因为到目前为止 IE 仍然不支持 CSS3 属性选择器),如下所示:

<input type="submit" class="submit uniqueButtonClass" value="Submit" />

如果您为 .submit 类提供 background-image< /code> 属性,您可以为每个单独的按钮独立设置 background-position 属性,避免重复多次。

例子:

.submit { background-image: url(path/to/image.png); width: 50px; height: 25px; border: 0; }
/* assuming 25px is the offset you're using */
.uniqueButtonClass { background-position: 0 25px; }

Don't use the image input type, you're much better off using a bog standard submit button and styling it that way.

Once you have defined your background-image, you'll need to define unique background-positions for each different button you'd like to use.

For backwards compatibility I would advise using class mixtures (since as of now IE still doesn't support CSS3 attribute selectors), like so:

<input type="submit" class="submit uniqueButtonClass" value="Submit" />

If you give the .submit class the background-image property, you can set the background-position property independently for each individual button, and avoid having to repeat yourself several times.

Example:

.submit { background-image: url(path/to/image.png); width: 50px; height: 25px; border: 0; }
/* assuming 25px is the offset you're using */
.uniqueButtonClass { background-position: 0 25px; }
遮了一弯 2024-08-03 03:39:58

我会使用上述输入类型,或者您也可以:

<button><img src="http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg" /></button>

按钮标签允许您在其中添加几乎所有内容,并允许更好地控制格式。

在这种情况下,您可以在按钮内或按钮本身设置跨度的样式。

I would use either a input type as above, or you could also:

<button><img src="http://www.mrfreefree.com/images/speciali/211/funny_cat_50.jpg" /></button>

The button tag allows you to pretty much throw anything in it, and allow for greater control on formating.

In this case you could style a span inside the button or the button it's self.

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