Reputation: 171351
Consider the following example: (live demo here)
HTML:
<div id="outer_wrapper">
<div class="wrapper">
<a><img src="http://img.brothersoft.com/icon/softimage/s/smiley.s_challenge-131939.jpeg" /></a>
</div>
<div class="wrapper">
<a><img src="http://assets.test.myyearbook.com/pimp_images/home_page/icon_smiley.gif" /></a>
</div>
<div class="wrapper">
<a><img src="http://thumbs3.ebaystatic.com/m/mvHqVR-GDRQ2AzadtgupdgQ/80.jpg" /></a>
</div>
<div class="wrapper">
<a><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/718smiley.png/60px-718smiley.png" /></a>
</div>
</div>
CSS:
#outer_wrapper {
background-color: #bbb;
width: 350px;
}
.wrapper {
display: inline-block;
border: 1px solid black;
width: 90px;
height: 100px;
text-align: center;
margin-right: 20px;
}
a {
display: inline-block;
width: 80px;
height: 80px;
border: 1px solid red;
}
The output is:
wrapper
s are not vertically aligned ? How could I fix that ?Please do not change the HTML, if possible.
Upvotes: 0
Views: 3004
Reputation: 215
may be this will help you
http://css.flepstudio.org/en/css-tutorials/centered-vertical-horizontal-align.html
it helped me :)
Upvotes: 0
Reputation: 90752
Observe that it is the base of the images which are aligned. This is to do with the vertical-align
; if you use a value for vertical-align
on .wrapper
other than baseline
, like top
, middle
or bottom
, it will fix it. (The difference between these will only be apparent if you put some text inside the div
as well.)
Then you want to centre the images in their 80x80 spots. You can do that with display: table-cell
and vertical-align: middle
on the a
(and add line-height: 0
to fix a couple more issue). You can then play further with mixing these groups of styles in the a
tag, the .wrapper
, or even throwing away the .wrapper
if it isn't necessary (it would only be needed - if it is at all - if you're putting text in with it).
Result, with no further tweaks than what I've mentioned here: http://jsfiddle.net/jESsA/38/.
This will work on all decent browsers, and even on IE8/9, but it won't work on IE6/7. A technique for solving this which should work in IE6/7 is this: on the a
, set display
to block
and alter the line-height
from 0
to 78px
(I'm not entirely clear on why 80px
makes it shift down one pixel, but it does; if I thought about it long enough I could probably figure out why), and shift the vertical-align: middle
to the img
child. Final result: http://jsfiddle.net/jESsA/44/
Upvotes: 3
Reputation: 7303
You could check this out: http://www.brunildo.org/test/img_center.html
Upvotes: 0
Reputation: 272106
You can try assigning a vertical-align
attribute on the img
tag. Vertical align is relative to the line box which means you need to set the line box as tall as the height of the a
tag. So these changes are needed in your CSS markup:
#outer_wrapper {
overflow: hidden; /* required when you float everything inside it */
}
.wrapper {
/* display: inline-block is not required */
/* text-align: center is not required -- see below */
float: left; /* float all wrappers left */
}
a {
display: block; /* block display required to make width and height behave as expected */
margin-left: 4px; /* shift the block to make it horizontally centered */
margin-top: 9px; /* shift the block to make it vertically centered */
text-align: center; /* center inline content horizontally */
line-height: 80px; /* line height must be set for next item to work */
}
img {
vertical-align: middle; /* presto */
}
Upvotes: 2
Reputation: 4373
Take a look at this:
Basically you use float: left
to put your boxes inline and a background image instead of an img tag. Because you are using float, you need to clear after to cancel the float effect on other elements.
I changed the DIV tags to A tags so you can have a link on the hole block and keep it simple. But you can keep it as a DIV tag and put an A block inside though (or use JavaScript)
Upvotes: 1