vertical-align属性探究
在前端开发中我们经常需要将元素垂直居中对齐,我们很自然的想到使用vertical-align属性,但是使用后却发现有时候能起作用,有时候却又不起作用。究其原因还是因为我们没有将vertical-align属性弄清楚,今天就来分析一下这个属性,若分析有误,还请原谅,望一起探讨!
规范介绍
Value: baseline | sub | super | top | text-top | middle | bottom | text-bottom | <percentage> | <length> | inherit
Initial: baseline
Applies to: inline-level and 'table-cell' elements
Inherited: no
Percentages: refer to the 'line-height' of the element itself
Media: visual
Computed value:for <percentage> and <length> the absolute length, otherwise as specified
适用元素
该属性仅适用于inline,inline-block,table-cell元素
属性值介绍
介绍属性之前先来了解一下各个属性值代表着什么,通过下面这张图可以知道

我们知道英语本子每行有4条线,其中红色的线即为基线
所用demo
<div class="box">
<span class="aa"></span>
x
</div>
baseline
将元素的基线与父元素的基线对齐
.aa4{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: baseline;
}
baseline的确定规则
- inline-table元素的baseline是它的table第一行的baseline。
- 父元素【line box】的baseline是最后一个inline box 的baseline。
- inline-block元素,如果内部有line box,则inline-block元素的baseline就是最后一个作为内容存在的元素[inline box]的baseline,而这个元素的baseline的确定就要根据它自身来定了。
- inline-block元素,如果其内部没有line box或它的overflow属性不是visible,那么baseline将是这个inline-block元素的底margin边界。
length
基于基线上(正值)下(负值)移动元素
input[name="sex"]{
margin:0;
padding:0;
vertical-align:-2px;
}
基于基线向下移动-2px
percentage
基于基线上(正值)下(负值)移动元素,值通过百分比乘上line-height而得
.aa2{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: -10%;
line-height: 30px;
}
这里的vertical-align:-10%所代表的实际值是:-10% * 30 = -3px,即向基线下方移动3px
注意:若该元素没有定义line-height,则会使用继承的line-height值
middle
将元素的中线与父元素的基线加上字母x的高度的一半对齐
.aa3{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: middle;
}
text-top
将元素的顶部与父元素正文区域的顶部对齐,元素的位置受父元素font-size的大小影响
.aa5{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: text-top;
}
text-bottom
将元素的底部与父元素的正文区域的底部对齐,元素的位置受父元素font-size的大小影响
.aa6{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: text-bottom;
}
top
将元素的顶部与line box顶部对齐
.aa7{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: top;
}
bottom
将元素的底部与line box底部对齐
.aa8{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: bottom;
}
super
将元素置于基线上方合适的位置
.aa10{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: super;
}
sub
将元素置于基线下方合适的位置
.aa9{
display:inline-block;
width:5px;
height:5px;
background:blue;
vertical-align: sub;
}
参考文章
vertical-align属性探究的更多相关文章
- 前端知识点回顾之重点篇——CSS中vertical align属性
来源:https://www.cnblogs.com/shuiyi/p/5597187.html 行框的概念 红色(line-height)为行框的顶部和底部,绿色(font-size)为字体的高度, ...
- HTML 标签元素的 align 属性
align 属性规定段落中文本的对齐方式. 有 left right center justify 这些参数 left right center 就是左对齐 右对齐 中间对齐 justify ...
- What is Vertical Align?
https://css-tricks.com/what-is-vertical-align/ ************************************************* CSS ...
- Android中软键盘展示、EditText焦点获取及windowSoftInputMode属性探究
2017-08-14 21:44:23 有很多中情况,分别展示. 1.Activity不做任何设置,布局使用LinearLayout 会自动滚动EditText之上的所有View,代码: <?x ...
- 格而知之2:UIView的autoresizingMask属性探究
UIView的autoresizingMask属性,是用在当一个UIView实例的父控件的尺寸发生变化时,来自动调整UIView实例在父控件中的位置与尺寸的.autoresizingMask属性是一个 ...
- 关于Vertical Align的理解
1:vertical-align 翻译就是垂直-对齐... 2:关于line-height的点 2.1:如果一个标签没有定义height属性,那么其最终表现的高度一定是由line-height起作用. ...
- 浏览器兼容CSS代码:按钮文字垂直居中(input button text vertical align)
经过测试的浏览器:IE6, IE7, IE8, IE9, Firefox, Chrome, Safiri, Maxthon 按钮的HTML代码: <input id="btn_comm ...
- img 的 align 属性
AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐. AbsMiddle 图像的中间与同一行中最大元素的中间对齐. Baseline 图像的下边缘与第一行文本的下边缘对齐. Bottom ...
- HTML自定义对象与属性探究(谷歌,火狐,IE9浏览器没问题)
1.自定义标签 <zqz>asdas</zqz> <style> zqz{ color:red; } </style> 页面变色 2.自定义标签的hov ...
随机推荐
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 《用OpenResty搭建高性能服务端》笔记
概要 <用OpenResty搭建高性能服务端>是OpenResty系列课程中的入门课程,主讲人:温铭老师.课程分为10个章节,侧重于OpenResty的基本概念和主要特点的介绍,包括它的指 ...
- 精读《dob - 框架使用》
本系列分三部曲:<框架实现> <框架使用> 与 <跳出框架看哲学>,这三篇是我对数据流阶段性的总结,正好补充之前过时的文章. 本篇是 <框架使用>. 1 ...
- [转&精]IO_STACK_LOCATION与IRP的一点笔记
IO_STACK_LOCATION和IRP算是驱动中两个很基础的东西,为了理解这两个东西,找了一点资料. 1. IRP可以看成是Win32窗口程序中的消息(Message),DEVICE_OBJECT ...
- SpringMVC跨域问题排查以及源码实现
SpringMVC跨域问题排查以及源码实现 最近一次项目中,将SpringMVC版本从4.1.1升级到4.3.10,出现跨域失败的情况.关于同源策略和跨域解决方案,网上有很多资料. 项目采用的方式是通 ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
- 谈谈 JAVA 的对象序列化
所谓的『JAVA 对象序列化』就是指,将一个 JAVA 对象所描述的所有内容以文件 IO 的方式写入二进制文件的一个过程.关于序列化,主要涉及两个流,ObjectInputStream 和 Objec ...
- Tomcat他山之石.可以攻玉(一)Server组件
Server组件 Server组件作用: 采用观察者模式,又叫源-收听者的设计模式,提供了可以动态添加.删除的监听器,作用是在Server组件的不同生命周期中完成不同的功能.逻辑: Tomcat容器的 ...
- 基于vue技术的企业移动办公系统的设计与实现
如何打包: http://www.cnblogs.com/smilehuanxiao/p/7693858.html http://www.cnblogs.com/1314y/p/6207153.htm ...
- MySQL小计
判断不等于某一个数字的时候,数据表为空的字段不参与比较: 判断字段为空用 is null