0

Scenario

I am trying to vertical align an img inside a div, using the table cell method ('6 Methods For Vertical Centering With CSS' @Vanseo Design):

<style> .container{border: solid 1px #000;display:table;} .contentItem{display:table-cell;vertical-align:middle;} .contentImg{float:left; width: 50px; margin: 3px;} .contentDiv{width: 400px; padding: 10px 2px 2px 2px;} </style> <div class="container"> <div class="contentItem"> <img class="contentImg" title="test" src="tv1.gif"/> <div class="contentDiv">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vel scelerisque enim.</div> </div> </div> 

Issue

It simply does not work. Here is a (not)runnig jsFiddle example.

Facts

  • I am using Chrome
  • The image width must be set at fixed value (50px for instance)
  • The image height is variable and not known before the image load
  • The contentDiv div tag cannot be removed. Actually it contains a complex structure, not a simply text, with dynamic data.
1
  • see the answer below. I just added vertical-align:text-top; on image and removed the container of text. Commented May 22, 2013 at 9:27

4 Answers 4

3

Try this

<div class="container"> <div class="contentItem"> <img class="contentImg" title="test" src="data:image/gif;base64,R0lGODlhJAAaAPf/ADdqhGBeXheC1CMhJRB1xAVwwmC55zqSz2fI+cXGxiiDySmJzAEAACN5vFOk1xYhKlaWt0ac0glww1Wl1RYcIpGSlayqqmG87Uqh1RcWGhZGdDY3OAlHh7m7vB1+xzeMzVar2AQAAAZv0AhHiBV/yFSr2CknKy0rKw1swTeQzCWEya6trAYAAF+66DWNzRp5xWBjZkGX0CyJzDWS0Adv0AoCBBx5xhB1w1qjySJ/xwYNFSmCwOLi4S2U3q6tq4KBg5qbniaJzAZvzwVswQRzy0GX0Uac0QZ201WVtiNVejItLQlswQdGh0eb0Rx6xw4ZKECX0BgeJBYeJF5eXViq2UhISQ51xAM9cTKNzZKUlyQjKJOVmFWWtxIeKB14xEqh1Euh1Q0ZJjiS0BgcJFRWVaqppyIfJSYkKkGVzjmNzt7f3laVtxFswKeoqiGEyR5/yBp+xwprwQZFiLGztRcdIxp+yEqg1C4rLAwWJRkfJiYlKScnK/P19Qtswkqi1TSNzkSc0qqqqYmLjleWuAhuwiQlKEec1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znmFIGOKgt2cYww5ERghCGGRACoEU1E0IQRfkZgKKJ+FgqIok0Ykg44eSqjCzFgiIMBGBgw44cfGHzxBQYY2MHpqF/YQWoznJbqzD65LP/4wDzhtDLBrZhM4MCumDhwq64O9IqrrrnuGmw/AMiKgyelUPGLK65AIq20IIDgChXRgkAFFZCUQIW1kLiiLSQXJBvAA+8gYEALLazbbrvrxsuuu/DS6y4CyT4jxTfADAMBBGv8iwTA/w4CARdIcGHwvwAPgjASxiSMwyuSwLDMPXlknEcUG9PhcRQU0MFxFHRQMAsFKHtc8hh0SEFHHjpoggoDLIQQAgs426yzzTWzkEwNOjMgNM8341wDA4XMQU8WFTTNdCIVbAGEI0AgsoUg0vhThSCIAJFJG444ncUWW4xtCxD/pK322v8ssssifCjCBygDTLGJIrsk0A0PbPcu7XfaapRDxjijzNHGBjMlwgk1ZFTByt+Qr61GAsV0AIQlP2RuSQUd+NJN5P8EBAA7"/> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vel scelerisque enim. </div> </div> 

CSS

.container{border: solid 1px #000;display:table;} .contentItem{display:table-cell;vertical-align:middle;} .contentImg{float:left; width: 50px; margin: 3px;vertical-align:text-top;} .contentDiv{width: 400px; padding: 10px 2px 2px 2px;} 

If you cant get rid of divs then simply change css

.contentDiv{width: 400px; padding: 0px 2px 2px 2px;} 
Sign up to request clarification or add additional context in comments.

3 Comments

I cannot get rid of the content div. Actually it contains a complex structure, not a simpy lorem ipsum.
The data inside the div are dynamic data. The lenght may change. Not solved by a sttic change in the padding.
I am sorry, but in the your fiddle I see the image aligned at the top of the container, not at the middle.
1

Your vertical alignment doesn't work as you are floating the image. if you remove the float from the image and then make the text div an inline-block element instead then your alignment will work:

.contentImg{width: 50px; margin: 3px;} .contentDiv{width: 400px; padding: 10px 2px 2px 2px; display:inline-block;} 

http://jsfiddle.net/Z8GaT/1/

2 Comments

No, if the contentDiv increases its length, images seems to align at the bottom http://jsfiddle.net/Z8GaT/4/.
@ADC Are you able to wrap the image in a tag? If you are: jsfiddle.net/Z8GaT/10
0

Or simply:

.yourClass{ position: relative; top: 50%; margin-top: - "the height of your image" / 2+px; } 

Note, the margin-top value should be minus the image's height divided by 2 if that was unclear.

1 Comment

Image height is variable and not known before the image load.
0

please check Demo here

remove

.contentDiv{width: 400px; padding: 10px 2px 2px 2px;} 

may be this solve your problem :).

1 Comment

No, the data in the contentDiv may change. I edited the facts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.