I have buttons on my page, sometimes with icon images and a text label in them, sometimes only with icon, no text.
I've styled the images in the buttons so that there is a margin to the right, to separate the icon from the text.
The problem is that because of that margin, if there is no text in the button, the icon is not in the center of the button, so it looks crappy.
button {
border: none;
border-radius: 0.5rem;
background-color: pink;
line-height: 32px;
font-size: 1rem;
padding: 0.5rem;
}
button > img {
float: left;
margin-right: 0.5rem;
}
<div>
<p>
This looks OK:
</p>
<button>
<img src="https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/star-32.png"> OK
</button>
</div>
<div>
<p>
This doesn't (icon not in the center of the button):
</p>
<button>
<img src="https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/star-32.png">
</button>
</div>
(see fiddle)
An easy and obvious fix would be to put the text in a span element and then style the image element with :only-child.
But then I would have to change a lot of places in our code base that already use this style and I would have to somehow convince my fellow developers to always put their button labels in span elements, only to make the buttons without text labels look OK.
Is there another way that allows me to keep my HTML code the same while getting rid of the margin on the image if it is not followed by text?
I have tried experimenting with :not and :empty pseudo class selectors but couldn't make it work. The problem is that you cannot select or style plain text nodes.
