I want to set vertical alignment of image inside a div. I use img { vertical-align:middle} but it is not working.
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
See this awser: How to vertical align image inside div
If you want to align horizontally also, add the
right
andleft
, like this:If you're trying to do what I think, vertical align isn't going to work; you'll need to use positioning.
In general, position the container relative, and then position the image absolute, with top and left set to 50%, and then move the image back to the center by setting negative margins equal to half the width / height.
Here's a working example: http://jsbin.com/evuqo5/edit
Basic CSS is this:
If you set the div
display
attribute totable-cell
thenvertical-align: middle;
will work.The
vertical-align
rule only affects table cells or elements withdisplay: table-cell
.See this article from SitePoint for a detailed explanation.
Using the
line-height
property will solve the problem:The following post has some useful references:
Text Alignment w/ IE9 in Standards-Mode
Also, depending on which version of IE you are testing against, you may end up needing some browser-specific hacks or some jQuery/JavaScript code.
If you have to, use a one-row-one-cell table and take advantage of the
vertical-align
property. This is brute-force, not overly semantic, but it works.This is a solution that doesn't require JavaScript (as my previous solution did).
You can achieve what you want by assigning
display: table-cell
to the containing div. Here's an example: http://jsbin.com/evuqo5/2/editI feel I must warn you that you will need to test this in every browser you intend to support. Support for the
table-cell
value is fairly new, particularly in Firefox. I know it works in Firefox 4, but I don't know about any of the 3.x iterations. You'll also want to test in IE (I've only tested in Chrome 10 and Firefox 4).The CSS:
You won't need the
div#container img
styles if you don't also want to horizontally align the image.