zoukankan      html  css  js  c++  java
  • CSS实现图片与文本的居中对齐的常见方式

    1.为图片和文本都设置vertical-align:middle

    <style>
        .d1>* {
            vertical-align: middle;
        }
    </style>
    <div class='d1'>
        <img src='./1.jpg' width='100'/>
        <span>这是一段文本</span>
    </div>

    2.通过弹性布局实现,外层容器设置弹性布局,通过align-items: middle设置交叉轴居中对齐

    <style>
        .d1 {
            display: flex;
            align-items: center;
        }
    </style>
    <div class='d1'>
        <img src='./1.jpg' width='100'/>
        <span>这是一段文本</span>
    </div>

    3.借助于背景图片实现,这时只需要文本居中即可

    <style>
        .d1 {
            height: 133px;
            background: url(./1.jpg) 0 0 no-repeat;
            background-size: 100px 133px;
            padding-left: 100px;
            line-height: 133px;
        }
    </style>
    <div class='d1'>
        <!--<img src='./1.jpg' width='100'/>-->
        <span>这是一段文本</span>
    </div>

    4.图片与文本左浮动,同时设置容器元素的line-height

    <style>
        .d1 *{
            float: left;
        }
        .d1 span {
            line-height: 133px;
        }
    </style>
    <div class='d1'>
        <img src='./1.jpg' width='100' height='133'/>
        <span>这是一段文本</span>
    </div>

    5.文本设置为行内块,图片垂直居中对齐

    <style>
        .d1 img {
            vertical-align: middle;
        }
        .d1 span {
            display: inline-block;
        }
    </style>
    <div class='d1'>
        <img src='./1.jpg' width='100' height='133'/>
        <span>这是一段文本</span>
    </div>
    原创文章请随便转载。愿和大家分享,并且一起进步。-- 江 coder
  • 相关阅读:
    HDU 5444 Elven Postman 二叉排序树
    HDU 5438 Ponds dfs模拟
    Gym
    markdown test
    Gym
    集训回顾
    UVALive
    UVALive
    UVALive
    codeforcres 589 J
  • 原文地址:https://www.cnblogs.com/jiangxiaobo/p/13962647.html
Copyright © 2011-2022 走看看