Vertical align an image and a multiline text

前端 未结 4 988
天命终不由人
天命终不由人 2020-12-09 02:26

I´m trying to align an image and a text vertically:

+-------------------------+ -- Viewport
|         Text text text  | 
| +-----+ text text text  | 
| |IMAGE| te         


        
相关标签:
4条回答
  • 2020-12-09 02:45

    Do you mean something like this demo fiddle?

    HTML:

    <div id="viewport">
        <a href="#">
            <img src="images/arrow_black.png" alt="" />
            <span>Lorem ipsum dolor...</span>
        </a>
    </div>
    

    CSS:

    #viewport {
        background: #bbb;
        width: 350px;
        padding: 5px;
        position: relative;
    }
    #viewport img {
        position: absolute;
        top: 50%;
        margin-top: -30px;  /* = image height div 2 */
    }
    #viewport span {
        margin-left: 68px;  /* = image width + 8 */
        display: block;    
    }
    
    0 讨论(0)
  • 2020-12-09 02:47

    This is a way that I'm not proud of, but it's the only way I know to archive this without js or flexbox.

    #viewport {
        width: 350px;
        padding: 5px;
        position: relative;
    }
    
    #viewport a {
      background: #bbb;
      display: inline-block;
    }
    
    #viewport img {
        display: block;
    }
    
    #viewport i,
    #viewport span {
        display:table-cell;
        vertical-align:middle;
    }
    
    /* Using padding to add gutter
    between the image and the text*/
    #viewport span {
        padding-left: 15px;
    }
    <div id="viewport">
        <a href="#">
            <i><img src="//www.gravatar.com/avatar/be15533afe64a3939c5201a65a19d7ed/?default=&s=80" alt="" /></i>
            <span>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</span>
        </a>
      
        <a href="#">
            <i><img src="//www.gravatar.com/avatar/be15533afe64a3939c5201a65a19d7ed/?default=&s=80" alt="" /></i>
            <span>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</span>
        </a>
    </div>

    This way no matter which element has more height, the text and the image will always be aligned to the middle.

    0 讨论(0)
  • 2020-12-09 03:06

    You said you can't use margin/padding answers because of not knowing size. However, why not just use percentage to put the image halfway down, then split everything up into divs?

    <div id="viewport">
        <div id="image">
            <img src="http://source..." />
        </div>
        <div id="text">
            Whatever
        </div>
    </div>
    

    And then in your CSS, do this:

    #viewport {
      width:800px;
      height:500px;
      border:1px solid #FFF;
    }     
    
    #image {
      width: 400px;
      float:left;
      height:100%;
      text-align: center;
    }
    
    img {
      margin-top:50%;
    }
    
    #text {
      width:300px;
      float:left;
    }
    

    Obviously all the widths and heights can be percentages or however you wish to handle them. However, this should render how you want it to. Hope I am understanding your question correctly.

    0 讨论(0)
  • 2020-12-09 03:08

    If you can estimate the ratio between the image width and text width I'd recommend setting a percentage width on the text span.

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