I am having trouble vertically aligning background image with text, without assigning constant (and hence, not automatic and reusable) values to height
or line-height
. I was wondering if there actually is a way to vertically center align bg image of, say and a
element, with its text without assigning constant values to
line-heightor
height`?
A live demo is available here: http://cssdesk.com/8Jsx2.
Here's the HTML:
<a class="">background-position: center</a>
<a class="contain">background-size: contain</a>
<a class="line-height">Constant line-height</a>
And here's the CSS:
a {
display: block;
margin: 10px;
width: 200px;
padding-left: 34px;
font-size: 14px;
background: url('http://cdn1.iconfinder.com/data/icons/freeapplication/png/24x24/Thumbs%20up.png');
background-repeat: no-repeat;
background-position: 5px center;
border: 1px solid black;
}
/* I don't want to use constant line-height */
.line-height {
line-height: 24px;
}
/* I don't want to use this, because I want my bg image's size to stay intact */
.contain {
background-size: contain;
}
Try using two words to align your background with css.
background-position: left center;
Reference: http://www.w3schools.com/cssref/pr_background-position.asp
UPDATE:
Adding another link from comments. Grant Winney shared another site that has a much better interface for how this works.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-position