CSS align images and text on same line

后端 未结 9 1094
长情又很酷
长情又很酷 2020-12-05 06:16

I have been searching and trying different methods for hours now. I just can\'t seem to get these two images and text all on one line. I want both the images and both text

相关标签:
9条回答
  • 2020-12-05 07:07

    This question is from 2012, some things are changed from that date, and since it still receives a lot of traffic from google, I feel like completing it adding flexbox as a solution.

    By now, flexbox is the advised pattern to be used, even if it lacks IE9 support.

    The only thing you have to care about is adding display: flex in the parent element. As default and without the need of setting other property, all the children of that element will be aligned in the same row.

    If you want to read more about flexbox, you can do it here.

    .container {
      display: flex;
    }
    
    img {
      margin: 6px;
    }
    <div class="container">
      <img src="https://placekitten.com/g/300/300" /> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>

    0 讨论(0)
  • 2020-12-05 07:08

    See example at: http://jsfiddle.net/6Rpkh/

    <style>
    img.likeordisklike { height: 24px; width: 24px; margin-right: 4px; }
    h4.liketext { color:#F00; display:inline }
    ​</style>
    
    <img class='likeordislike' src='design/like.png'/><h4 class='liketext'>$likes</h4>
    <img class='likeordislike' src='design/dislike.png'/><h4 class='liketext'>$dislikes</h4>
    

    0 讨论(0)
  • 2020-12-05 07:09

    You can either use (on the h4 elements, as they are block by default)

    display: inline-block;
    

    Or you can float the elements to the left/rght

    float: left;
    

    Just don't forget to clear the floats after

    clear: left;
    

    More visual example for the float left/right option as shared below by @VSB:

    <h4> 
        <div style="float:left;">Left Text</div>
        <div style="float:right;">Right Text</div>
        <div style="clear: left;"/>
    </h4>

    0 讨论(0)
提交回复
热议问题