Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical middle aligned image and text side-by-side in CSS

Tags:

html

css

I'm trying to print an image and text consecutively with two divs.

I have gained this output so far:

enter image description here sample text

But I need to display the text in the middle after the image, not at bottom.

HTML

<body>
    <div id="qr" style="display:inline-block; min-width:2.2cm; height:3.8cm; align: center;" >
        [ image_url  ]
    </div>
    <div style="display:inline-block; min-width:3.8cm;">
        sample text
    </div>
</body>

CSS

body{
    width:21.1cm;
    height:29.8cm;
    background-color: white;
    margin-top:0.4cm;
}
  • the image is qr code. so i can't use table method
  • the div using in foreach method. so cant change the dimension. how can i?
like image 211
m2j Avatar asked Jan 30 '16 12:01

m2j


4 Answers

Are you after something like this:

<div id="qr" style="display:inline-block; min-width:2.2cm; height:3.8cm; align: center;vertical-align: middle;" >
  <img src="http://i.stack.imgur.com/25Rl3.jpg" style="height:3.8cm;">
</div>
<div style="display:inline-block;vertical-align: middle;">
  sample text
</div>
like image 160
Pmpr.ir Avatar answered Sep 19 '22 14:09

Pmpr.ir


My way of doing this:

By using flex:

<div class="one">
    <div>
        <img src="http://placehold.it/300x150" />
    </div>
    <div>
        sample text
    </div>
</div>

CSS:

.one {
    display: flex;
    align-items: center;
}
like image 33
Jitesh Narula Avatar answered Sep 19 '22 14:09

Jitesh Narula


<div style="float:left"><img.. ></div>
<div style="float:right">text</div>
<div style="clear:both"/>

OR

<div>
    <img style="float: left; margin: ...;" ... />
    <p style="float: right;">Text goes here...</p>
</div>

See this post or

W3Schools

like image 30
Dirk Jan Avatar answered Sep 17 '22 14:09

Dirk Jan


add following styles to text div

height:3.8cm;
line-height:3.8cm;
like image 26
Amir Hoseinian Avatar answered Sep 18 '22 14:09

Amir Hoseinian