Reputation: 3602
I have a few buttons in my ui that have to have images floating next to the text and this is working well. However, I'm having issues vertically aligning that image to the text.
I've tried multiple things along with vertical-align: center;
, trying to use absolute positioning, and I even tried some transform technique that failed spectacularly.
Now the content for these buttons is coming from users so it is dynamic, otherwise I would try to do something with line height and call it a day.
Any help would be amazing!
My html looks something like this.
<div class='btn-primary col-md-4'>
<img src='https://developers.google.com/web/fundamentals/imgs/placeholder--small.png'/ >
<span class='imgInfo'>This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. This is info. </span>
<p class='clearfix'></p>
</div>
and the css looks something like this.
img{
vertical-align: middle;
float:left;
width:50%;
}
.imgInfo{
margin-left: 0px;
overflow:auto;
display:block;
float:right;
width:50%;
padding-left:1em
}
and a jsFiddle to plat with http://jsfiddle.net/52VtD/7356/
Upvotes: 0
Views: 135
Reputation: 11496
css
img{
float:left;
width:50%;;
position: absolute;
top:0;
bottom:0;
margin: auto;
}
(ref: Smashing Magazine)
After researching specs and documentation, this is how Absolute Centering works:
margin: auto;
equals ’0′ for the top and bottom.position: absolute;
breaks the block out of the typical content flow, rendering the rest of the content as if that block weren’t there.top: 0; left: 0; bottom: 0; right: 0;
gives the browser a new bounding box for the block. At this point the block will fill all available space in its offset parent, which is the body or position: relative;
container. Developer.mozilla.org1918: For absolutely positioned elements, the top, right, bottom, and left properties specify offsets from the edge of the element’s containing block (what the element is positioned relative to).width
or a height
prevents the block from taking up all available space and forces the browser to calculate margin: auto
based on the new bounding box. Developer.mozilla.org1918: The margin of the [absolutely positioned] element is then positioned inside these offsets.margin-top
and margin-bottom
centering the element in the bounds set earlier.Absolute Centering appears to be the intended use for margin: auto;
based on the spec and should therefore work in every standards compliant browser.
TL;DR: Absolutely positioned elements aren’t rendered in the normal flow, so margin: auto;
centers vertically within the bounds set by top: 0; left: 0; bottom: 0; right: 0;
.
Upvotes: 1
Reputation: 8521
You could use -
img{
position: absolute;
width: 50%;
top: 0;
bottom: 0;
left: 5px;
margin: auto;
}
The top: 0;
and bottom: 0;
will prevent each other to touch the edge.
And margin: auto;
will correctly align the img
to center.
Working FIDDLE
Upvotes: 0
Reputation: 63
I would suggest that you use
.img {
margin-top:40%
}
instead of vertical-align.
Upvotes: 0