Aligning images

Discussion in 'CSS' started by Crusader, Oct 22, 2006.

  1. #1
    Is there a way in CSS to get the same affect as the following without using tables. I want an image next to text. The text can consists of multiple paragraphs. The image needs to be to the left of the text and align to the vertical center of the paragraph(s).

    With tables it's easy:
    <table><tr><td><img src="theimage.jpg"/></td><td><p>Paragraph text</p><p>Paragraph text 2</p></td></tr></table>
    HTML:
    But how can I achieve the same in CSS and ensure that it looks the same in all browsers (without using hacks). Any ideas?
     
    Crusader, Oct 22, 2006 IP
  2. Corey Bryant

    Corey Bryant Texan at Heart

    Messages:
    1,126
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can float the image to the left / right.
    <img src="theimage.jpg" alt="text" style="float:left" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla orci odio, facilisis ut, condimentum quis, vulputate vitae, enim. Cras urna elit, semper ut, mattis quis, sagittis eget, pede. Etiam dapibus augue aliquam nunc. Nunc eget leo ut lorem fringilla suscipit. Aliquam mattis nonummy pede. Fusce sit amet sapien. Nulla suscipit, diam nec interdum pellentesque, elit lectus facilisis odio, ac fermentum lorem ante ut est. Morbi id velit a purus tempor euismod. Ut a velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec et augue sit amet purus sagittis ultrices.
    <br /><br />
    Nam vitae orci. Donec tincidunt lacus in orci. Sed et massa sed libero gravida molestie. Curabitur vel tellus. Nunc nec odio id purus porttitor lobortis. Donec lobortis ultrices urna. Cras pede sapien, feugiat sed, auctor cursus, placerat sed, augue. Fusce porttitor. Curabitur ante ligula, ornare vel, lobortis sit amet, ornare nec, pede. Nam justo. Nulla dignissim, risus sit amet blandit pretium, nibh diam facilisis est, a varius nisi dui non purus. Phasellus vitae nisi. Mauris vel tortor. Etiam eget diam ut dolor imperdiet vehicula.
    Code (markup):
    might be what you are looking for.
     
    Corey Bryant, Oct 22, 2006 IP
  3. Crusader

    Crusader Peon

    Messages:
    1,735
    Likes Received:
    104
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Floating the image works for short paragraphs, but when it comes to longer paragraphs/mulitple paragraps. The text continues under the image, which I don't want.
     
    Crusader, Oct 22, 2006 IP
  4. Corey Bryant

    Corey Bryant Texan at Heart

    Messages:
    1,126
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It shouldn't. I use it all the time. You might want to add some padding to the bottom perhaps to give it a little room?
     
    Corey Bryant, Oct 22, 2006 IP
  5. Crusader

    Crusader Peon

    Messages:
    1,735
    Likes Received:
    104
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've added padding. Doesn't quite look the way I want it, but it will have to do.
     
    Crusader, Oct 22, 2006 IP
  6. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #6
    See vertically centering content ....

    This is the applicable css;
    
    .outer {
        position: relative;
        display: table;
        vertical-align: middle;
        height: 300px;
        width: 300px;
        margin: 25px auto 0;
        border: 1px solid black;
        }
    
    .inner {
        position: relative;
        display: table-cell;
        vertical-align: middle;
        width: 100%;
        }
    
    .mid {
        border: 1px dashed silver; /*for clarity*/
        }
    
    /*Now the hack for obsolete browsers-ok, just IE*/
    /* \*/
    * html .inner {
        top: 50%;
        left: 0;
        }
    
    * html .inner .mid {
        position: relative;
        top: -50%;
        }
    /* */
    Code (markup):
    cheers,

    gary
     
    kk5st, Oct 22, 2006 IP