• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

26-Icon图标和文字垂直居的N种解决方法

武飞扬头像
卖女孩的小火柴..
帮助1

我们经常会遇到图标和文字不能对齐的情况。

<div class="box">
    <img class="icon" src="restart.png"/>
    <span class="text">垂直居中</span>
</div>
<style>
	.icon{
		width:18px;
		height:20px;
		}
	.text{
		font-size:12px;
	     }
</style>

出来的结果:
学新通
下面有3中方法可以解决这个问题:

1、浮动法,将文字和图片所在的块全部浮动。给文字加line-height

<body>
        <div class="box">
            <img class="icon" src="./images/icon_aboutus.png" />
            <span class="text">垂直居中</span>
        </div>
    </body>
    <style>
        .box {
            /* border: 1px solid #000; */
            overflow: hidden;
        }
        .icon {
            float: left;
            width: 20px;
            height: 20px;
        }
        .text {
            float: left;
            padding: 0 5px 0 5px;
            font-size: 14px;
            line-height: 20px;
        }
    </style>
学新通

学新通

2、绝对定位法

<body>
        <div class="box">
            <img class="icon" src="./images/icon_aboutus.png" />
            <span class="text">垂直居中</span>
        </div>
    </body>
    <style>
		.box {
            position: relative;
        }
        .icon {
            width: 20px;
            height: 20px;
        }
        .text {
            position: absolute;
            font-size: 14px;
            line-height: 20px;
        }
    </style>
学新通

3、vertical-align法(不建议使用有bug,文字的大小必须小于icon的高)

    <body>
        <div class="box">
            <img class="icon" src="./images/icon_aboutus.png" />
            <span class="text">垂直居中</span>
        </div>
    </body>
    <style>
        .box {
            border: 1px solid #000;
        }
        .icon {
            width: 20px;
            height: 20px;
            vertical-align: middle;
        }
        .text {
            font-size: 13px;
        }
    </style>
学新通

4、将图片设置成文字的背景。
待补充…

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeihef
系列文章
更多 icon
同类精品
更多 icon
继续加载