我正在尝试用一些按钮和图像优雅地解决样式问题。我的目标是拥有如下所示的按钮:
(Hacky mspaint 样机)
(来源:robhruska.com)
但是,我有一组我想满足的标准:
- 我还需要能够让图标图像单独作为按钮存在,没有文本(即按钮背景是图标图像,按钮宽度/高度 = 图标图像宽度/高度)。
- 仅图标按钮需要支持
:hover
更改背景图标的状态。 - 文字按钮需要支持渐变背景的变化
:hover
,但图标本身不需要变化。
我在垂直 CSS 精灵图像中同时拥有图标图像和渐变图像。
目前,我只能考虑为文本按钮和纯图标按钮使用单独的样式集,如下所示:
<!-- text-button -->
<!-- button.text-button would have gradient background and border -->
<!-- button.text-button:hover would change to the hover gradient -->
<!-- .icon would have the icon background (icon border is included in image) -->
<button class="text-button">
<div class="icon-delete">Button Text</div>
</button>
<!-- icon-only button -->
<!-- button.icon-button would have the specific icon image background -->
<!-- button.icon-button:hover would change to the hover state background -->
<!-- button styling is different than .text-button, since icon border is included in image -->
<button class="icon-button-delete"></button>
我不是特别喜欢使用内部<div>
文本按钮,因为它似乎添加了更多可能不必要的标记。这也使得如果我想稍后返回并向按钮添加文本,我必须更改几个组件,而不仅仅是更改按钮类。这也意味着我可能必须为按钮/div 样式的几种不同组合定义图标精灵的图像位置,这不是 DRY。
总结一下我的问题:如何使用 上的单个顶级样式来做到这一点<button>
,而不是使用嵌套元素或图标-与文本-按钮的单独样式?它不一定必须是一个<button>
,它可以是任何元素,如果它优雅地完成了这一点。
我真的不需要完整的代码示例,只是建议如何最好地完成它。