Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text Aligned Next To Photo in CSS

I am wondering if there is some way to align text on the right of a photo, and keep the text in that same "box" even after the image ends using HTML and CSS. A quick "diagram" of what I am attempting to accomplish is below:

------- --------
------- --------
-Image- - Text -
------- --------
------- --------
        --------
        --------

Thanks for any help!

like image 902
PF1 Avatar asked Mar 13 '10 17:03

PF1


2 Answers

If you set a width on the text div and float both the Image and the Text left (float:left), that should do the trick. Clear the floats after both.

<div style="float:left; width:200px">
    <img/>
</div>

<div style="float:left; width:200px">
    Text text text
</div>

<div style="clear:both"></div>
like image 86
typeoneerror Avatar answered Nov 16 '22 01:11

typeoneerror


DEMO: http://jsbin.com/iyeja/5

    <div id="diagram">
            <div class="separator"></div>
            <div class="separator"></div>

            <div id="text_image_box">
              <span class="image"><img src="http://l.yimg.com/g/images/home_photo_megansoh.jpg" alt="" /></span><span class="text"><p>some text</p></span>
              <div class="clear"></div>
            </div>

            <div class="separator"></div>
            <div class="separator"></div>
            <div class="separator"></div>
          </div>

    <style>
      /* JUST SOME FANCY STYLE*/
      #diagram { 
        width:300px;
        border:1px solid #000;
        padding:10px;
        margin:20px;
      }

      .separator { 
        height:2px;
        width:300px;
        border-bottom:1px dashed #333;
        display:block;
        margin:10px 0;
      }

      /* MAIN PART */
      #text_image_box { 
      width:300px;
      margin:0 auto;
      padding:0
      }

      .image { 
        float:left;
        width:180px;
        height:300px;
        overflow:hidden;
         margin:0 auto;
      }
      .text {
    float:right;
    width:100px;
    padding:0;
    margin:0 auto;
  }
  .text p { 
    margin:0;
    padding: 0 5px;
  }
      .clear {
      clear:both
      }
      </style>
like image 45
Luca Filosofi Avatar answered Nov 16 '22 01:11

Luca Filosofi