I'm trying to elegantly solve a styling issue with some buttons and images. My goal is to have buttons that look like the following:
(Hacky mspaint mockup)
However, I have a set of criteria I'd like to meet:
- I also need to be able to have the icon image exist as a button alone, with no text (i.e. the button background is the icon image and the button width/height = icon image width/height).
- The icon-only button needs to support
:hover
states that change the background icon. - The text buttons need to support the gradient background changing on
:hover
, but the icon itself does not have to change.
I have both the icon image and gradient image available to me in a vertical CSS sprite image.
Currently, I can only think to do this with separate sets of styles for the text buttons and icon-only buttons, along the lines of:
<!-- 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>
I don't particularly like using the inner <div>
for the text button, since it seems to add more markup that might be unnecessary. It also makes it so that if I wanted to go back later and add text to a button, I'd have to change several components instead of just changing the button class. It also means that I probably have to define the image positions for my icon sprites for several different combinations of button/div styles, which isn't DRY.
To summarize my question: How can I do this with a single top-level style on the <button>
, rather than using nested elements or separate styles for the icon- vs text- buttons? It doesn't necessarily have to be a <button>
either, it could be any element if it accomplishes this elegantly.
I don't really need a full code sample, just a suggestion for how it might be accomplished best.