29

I´m trying to align an image and a text vertically:

+-------------------------+ -- Viewport
|         Text text text  | 
| +-----+ text text text  | 
| |IMAGE| text text text  | 
| +-----+ text text text  | 
|         text text text  | 
+-------------------------+ 

This works fine, if the text is not wrapped. If the Text is wider than the viewport-width, it does not work anymore. I think this is caused by setting display: inline-block:

<a href="#">
    <img style="display: inline-block; vertical-align: middle; margin-right: 8px;" src="images/arrow_black.png" /> 
    <span style="display: inline-block; vertical-align: middle;">
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonum eirmod tempor invidunt ut labore et dolore 
    </span>
</a>

The result:

+---------------------------------------------------------------------+ -- Viewport
|                                                                     |                                                            
| +-----+                                                             | 
| |IMAGE| text text text text text text text text text text text text | 
| +-----+                                                             |                                                           
|                                                                     | 
+---------------------------------------------------------------------+ 

+-------------------------+ -- Viewport
| +-----+ Text text text  | 
| |IMAGE| text text text  | 
| +-----+ text text text  | 
| text text text text     | 
+-------------------------+ 

If I try to float the elements, the image will always be on top, but not vertical-aligend in the middle of the text:

    <a href="#">
        <img style="display: block; vertical-align: middle;  margin-right: 8px; float: left;" src="/images/arrow_black.png" /> 
        <span style="display: block; overflow: auto;"> 
            Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 
        </span> 
    </a>

The result:

+-------------------------+ -- Viewport
| +-----+ Text text text  | 
| |IMAGE| text text text  | 
| +-----+ text text text  | 
|         text text text  | 
|         text text text  | 
|         text text text  | 
+-------------------------+ 

I´ve seen several solutions for this problem, using html-tables or css-tables (display: table and display: table-cell), but this is not an option, because it must work with all types of browsers (desktop and mobile).

To that, I do not know any sizes. Neither of the image nor of the text. So I can't use any "margin- or padding-Solution".

EDIT: I´ve created a demo-fiddle (based on the one NGLN has created, BTW: Thanks for that!) that show the result i´m looking for. But I try to archive this without using display: table-cell... any ideas?

Cœur
  • 37,241
  • 25
  • 195
  • 267
bceo
  • 319
  • 1
  • 3
  • 5
  • What browser do not support html-tables? – Kraz Jun 29 '11 at 16:46
  • Is the viewport fixed sized? And is the image fixed sized? – NGLN Jun 29 '11 at 16:52
  • @Kraz: Somewhere I read that some mobile browsers have problems with display: table-cell. And i can´t use html-tables, because of some other restrictions, which are to complex to discuss in full length here. – bceo Jun 29 '11 at 21:10
  • @NGLN: The viewport-size is not fixed nor the image size. BTW: The viewport is only a div-container, which width is equal to the display-width of a mobile device. – bceo Jun 29 '11 at 21:13

4 Answers4

10

Do you mean something like this demo fiddle?

HTML:

<div id="viewport">
    <a href="#">
        <img src="images/arrow_black.png" alt="" />
        <span>Lorem ipsum dolor...</span>
    </a>
</div>

CSS:

#viewport {
    background: #bbb;
    width: 350px;
    padding: 5px;
    position: relative;
}
#viewport img {
    position: absolute;
    top: 50%;
    margin-top: -30px;  /* = image height div 2 */
}
#viewport span {
    margin-left: 68px;  /* = image width + 8 */
    display: block;    
}
NGLN
  • 43,011
  • 8
  • 105
  • 200
  • The only problem here is they said they do not know the size of the image or the text :/ – joshmax Jun 29 '11 at 19:16
  • Yes, i mean something like that. But - as Max has mentioned - i do not know the size of the image nor of the text... The viewport width will be 100% of the body (more precisely it will be equal to the width of a mobile device) – bceo Jun 29 '11 at 21:18
  • @NGLN It could be useful when you know exact size of image which puts in my case :) Thanks . – Emre Aydin May 06 '15 at 14:51
5

This is a way that I'm not proud of, but it's the only way I know to archive this without js or flexbox.

#viewport {
    width: 350px;
    padding: 5px;
    position: relative;
}

#viewport a {
  background: #bbb;
  display: inline-block;
}

#viewport img {
    display: block;
}

#viewport i,
#viewport span {
    display:table-cell;
    vertical-align:middle;
}

/* Using padding to add gutter
between the image and the text*/
#viewport span {
    padding-left: 15px;
}
<div id="viewport">
    <a href="#">
        <i><img src="//www.gravatar.com/avatar/be15533afe64a3939c5201a65a19d7ed/?default=&s=80" alt="" /></i>
        <span>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</span>
    </a>
  
    <a href="#">
        <i><img src="//www.gravatar.com/avatar/be15533afe64a3939c5201a65a19d7ed/?default=&s=80" alt="" /></i>
        <span>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</span>
    </a>
</div>

This way no matter which element has more height, the text and the image will always be aligned to the middle.

0

If you can estimate the ratio between the image width and text width I'd recommend setting a percentage width on the text span.

asteig
  • 97
  • 1
  • 6
  • 1
    My problem: I can´t estimate the ratio. The image will change, based on the site context. Some images are wider than other ones. – bceo Jun 29 '11 at 21:20
0

You said you can't use margin/padding answers because of not knowing size. However, why not just use percentage to put the image halfway down, then split everything up into divs?

<div id="viewport">
    <div id="image">
        <img src="http://source..." />
    </div>
    <div id="text">
        Whatever
    </div>
</div>

And then in your CSS, do this:

#viewport {
  width:800px;
  height:500px;
  border:1px solid #FFF;
}     

#image {
  width: 400px;
  float:left;
  height:100%;
  text-align: center;
}

img {
  margin-top:50%;
}

#text {
  width:300px;
  float:left;
}

Obviously all the widths and heights can be percentages or however you wish to handle them. However, this should render how you want it to. Hope I am understanding your question correctly.

rybo
  • 1,161
  • 2
  • 10
  • 14
  • My Problem: I don´t know the sizes of the viewport (its width is set to 100% - so its equal to the width of a mobile device display) nor of the image nor of the text... If i would know them, i would have not such a problem :) – bceo Jun 29 '11 at 21:24
  • i'm not sure i understand your problem, but why not just leave viewport as 100%, set image to, say, 40% and the text to 60%? – rybo Jul 05 '11 at 15:21
  • Because the viewport size can change and I don't want to scale the image. – bceo Jul 12 '11 at 07:30