Vertically align text right of floated image, image sizes varied, responsive

后端 未结 1 636
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 16:39

I\'d like to vertically align multiple lines of text to the right of a left-floated image.

However

  • The image size (height and width) will vary and is
1条回答
  •  生来不讨喜
    2021-01-02 17:11

    This will get you started: jsFiddle example - look below for a better method.

    Basically, vertical-align:middle and display:inline-block are used on both the p and the img elements for centering.

    HTML

    Lorem Ipsum is simply dummy text

    CSS

    .element {
        background:rgb(134, 226, 255);
        margin:10px;
    }
    p {
        display:inline-block;
        margin:0px;
        width:70%;
        background:white;
        vertical-align:middle;
    }
    img {
        display:inline-block;
        vertical-align:middle;
    }
    

    Here is better approach using display:table/display:table-cell Same HTML..

    jsFiddle example - semi-responsive... Other jsFiddle example - responsive img elements..

    CSS

    .element {
        width:100%;
        display:table;
        background:rgb(134, 226, 255);
        margin:10px 0px;
        padding:10px;
    }
    p {
        display:table-cell;
        height:100%;
        vertical-align:middle;
        background:white;
    }
    img {
        display:table-cell;
        width:100%;
        height:auto;
    }
    

    Yet another update using media queries

    You could obviously use whatever breakpoints you want. I use 480px, as this is just for example purposes. Try resizing the window. jsFiddle example

    CSS

    @media only screen and (min-width: 480px) {
    .element {
        width:100%;
        display:table;
        background:rgb(134, 226, 255);
        margin:10px 0px;
        padding:10px;
        box-sizing:border-box;
        -moz-box-sizing:border-box;
        -webkit-box-sizing:border-box;
    }
    p {
        display:table-cell;
        height:100%;
        vertical-align:middle;
        background:white;
    }
    img {
        display:table-cell;
        width:100%;
        height:auto;
    }
    }
    @media only screen and (max-width: 479px) {
    .element {
        width:100%;
        background:rgb(134, 226, 255);
        margin:10px 0px;
        padding:10px;
        box-sizing:border-box;
        -moz-box-sizing:border-box;
        -webkit-box-sizing:border-box;
    }
    p {
        background:white;
    }
    img {
        width:50%;
        margin:0px auto;
        display:block;
        height:auto;
    }
    }
    

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