0

I've got three divs inside another div.

<div id="parentDiv" style="float:right;">
   <div id="childDiv1" style="float:left;">
       <b>Text1</b>
   </div>
   <div id="childDiv2" style="float:left;">
       <img src="smiley.gif" alt="Smiley face" height="42" width="42">
   </div>
   <div id="childDiv3" style="float:left;">
       <input type='checkbox' />
   </div>
<div>

I need them to appear side-by-side, so I've set them as float:left.

The trouble is that they appear like this...

Incorrect layout image

...with the text vertically aligned at the top. I need it to be in the middle.

N.B. I've found a post about a very similar issue, but it seems that floating causes it to fail.

Community
  • 1
  • 1
Urbycoz
  • 7,247
  • 20
  • 70
  • 108

4 Answers4

1

You can achieve that easily with tables

       <table>
            <tr>
                <td> <b>Text1</b></td>
                <td><img src="smiley.gif" alt="Smiley face" height="42" width="42"></td>
                <td><input type='checkbox' /></td>
            </tr>
        </table>

Or you really need to stick with divs? Hope this helps. :)

Jude Duran
  • 2,195
  • 2
  • 25
  • 41
1

Use the following code: (I advise you to avoid tables as much as possible)

<div id="parentDiv" style="float:right;">
   <div id="childDiv1" style="float:left; line-height: 42px;">
       <b>Text1</b>
   </div>
   <div id="childDiv2" style="float:left;">
       <img src="smiley.gif" alt="Smiley face" height="42" width="42">
   </div>
   <div id="childDiv3" style="float:left; line-height: 42px;">
       <input type='checkbox' />
   </div>
<div>
user2255273
  • 948
  • 1
  • 11
  • 29
1

If you really need DIVs, you can use the "display: inline;" instead of float. And then vertical-align: middle; to make the elements centered.

    <div id="parentDiv" style=" ">
       <div id="childDiv1" style="display: inline-block; vertical-align: middle;">
           <b>Text1</b>
       </div>
       <div id="childDiv2" style="display: inline-block; vertical-align: middle;">
           <img src="smiley.gif" alt="Smiley face" height="42" width="42">
       </div>
       <div id="childDiv3" style="display: inline-block; vertical-align: middle;">
           <input type='checkbox' />
       </div>
    </div>

Hope this helps.

Jude Duran
  • 2,195
  • 2
  • 25
  • 41
0

For them to appear side by side, you don't need them to float, just set them as they are. As for the image, just add a CSS property for it: vertical-align:middle and that should align it in the middle of the text.

casraf
  • 21,085
  • 9
  • 56
  • 91