我可以在提交按钮上使用 csssprite
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用图像输入类型,最好使用沼泽标准提交按钮并以这种方式设置样式。
定义
背景图像
后,您需要为要使用的每个不同按钮定义唯一的背景位置
。为了向后兼容,我建议使用类混合(因为到目前为止 IE 仍然不支持 CSS3 属性选择器),如下所示:
如果您为
.submit
类提供background-image< /code> 属性,您可以为每个单独的按钮独立设置
background-position
属性,避免重复多次。例子:
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 uniquebackground-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:
If you give the
.submit
class thebackground-image
property, you can set thebackground-position
property independently for each individual button, and avoid having to repeat yourself several times.Example:
我会使用上述输入类型,或者您也可以:
按钮标签允许您在其中添加几乎所有内容,并允许更好地控制格式。
在这种情况下,您可以在按钮内或按钮本身设置跨度的样式。
I would use either a input type as above, or you could also:
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.