css vertical-align全解
CSS 的属性 vertical-align 指定了内联(inline)元素或表格单元格(table-cell)元素的垂直对齐方式。
要记住:vertical-align不影响块级元素中内容的对齐。
(
vertical-align要点
- It only applies to inline or inline-block elements 或table-cell元素
- It affects the alignment of the element itself, not its contents (except when applied to table cells)
- When it’s applied to a table cell, the alignment affects the cell contents, not the cell itsel
从第二点可以看出,但是inline or inline-block<它影响的是元素自己的对齐,而不是他的内容。
从第3点,应用到table-cell,影响的是cell内容,而不是元素自己.
In other words, the following code would have no effect:
- div {
- vertical-align: middle; /* this won't do anything */
- }
Why? Because a <div>
is a block-level element, not inline. Of course, if you converted the<div>
to an inline or inline-block element, then the vertical-align
property would have an effect.
On the other hand, when used correctly (on an inline or inline-block element), thevertical-align
property will cause the targeted element to align itself in relation to other inline elements.
How high up or down an element is aligned would depend on the size of the inline elements on the same line, or on the line-height set for that line.转自:http://www.impressivewebs.com/css-vertical-align/
)
- 默认值
baseline
- Applies toinline-level and table-cell elements
- 可继承no 不能继承
- Percentagesrefer to the line-height of the element itself
- Media
visual
- 计算值for percentage and length values, the absolute length, otherwise the keyword as specified
- Animatableyes, as a length
- Canonical orderthe unique non-ambiguous order defined by the formal grammar
语法
Formal syntax: baseline | sub | super | text-top | text-bottom | middle | top | bottom | <percentage> | <length>
值 (对于内联元素)
多数值是相对于父元素:
baseline
- 元素基线与父元素的基线对齐。一些 可替换元素,比如
<textarea>
, HTML标准没有说明它的基线,这意味着使用这个关键字,各浏览器表现可能不一样。 - (如果一个垂直对齐元素没有基线--也就是说,如果这是一个图像或表单插入元素,或其他替换元素,那么该元素的底端与其父元素对齐。
-
p{
border:1px solid red;
}
img{
vertical-align:baseline;
}
<p>如果<img src="data:images/mm.jpg" style="width:100px;height=100px;"/>一个<span>垂直</span>对齐元素没有基线-也就是说,如果这是一个图像或表单元素,或是其他其他替换元素,那么该元素的底端与其
父元素的基线对齐
</p> sub
- 元素基线与父元素的下标基线对齐。
super
- 元素基线与父元素的上标基线对齐。
text-top
- 元素顶端与父元素字体的顶端对齐。
text-bottom
- 元素底端与父元素字体的底端对齐。
middle
- 元素中线与父元素的小写字母的中线对齐。
- (居中对齐,它往往(但不总是)应用于图像,你可能会把它的名字想象其效果,但你的想象与其实际效果并不完全一样。
- middle会把行内元素框的中点与父元素的基线上方0.5ex的一个点对齐,这里的1ex相对于父元素的font-size定义。
<length>
- 元素基线超过父元素的基线指定高度。可以取负值。
<percentage>
- 同 <length> , 百分比相对于
line-height
。
下面两个属性不像上面的属性相对于父元素,而是相对于整行:
bottom
- 元素及其后代的底端与整行的底端对齐。
- (将元素行内框的底端与行框的底端对齐,下面的代码:
-
p{
border:1px solid red;
}
img{
vertical-align:bottom;
}
span{
vertical-align:super;
}
</style>
</head> <body>
</body>
<p>如果<img src="data:images/mm.jpg" style="width:100px;height=100px;"/>一个<span>垂直</span>对齐元素没有基线-也就是说,如果这是一个图像或表单元素,或是其他其他替换元素,那么该元素的底端与其
父元素的基线对齐
</p>加 bottom 不加bottom
可以看到加bottom的p的border与图片没有空隙。
top
元素及其后代的顶端与整行的顶端对齐。top显示如下:
还有一个规则,如果元素没有基线baseline,则以它的外边距的下边缘为基线。哪些元素没有baseline?就是替换元素没有
baseline。为了验证这个。我们设置:
img{
vertical-align:bottom;
margin-bottom:100px;
}
确实设置外边距影响了基线。
值 (对于表格单元格)
baseline
(andsub
,super
,text-top
,text-bottom
,<length>
, and<percentage>
)- 与同行单元格的基线对齐。
top
- 单元格的内边距的上边缘与行的顶端对齐。
middle
- 单元格垂直居中。
bottom
- 单元格的内边距的下边缘与行的底端对齐。
可以取负值。
以上内容大部分来自:https://developer.mozilla.org/zh-CN/docs/CSS/vertical-align。
更多看一篇非常好的文章:
http://phrogz.net/css/vertical-align/index.html
现摘取部分内容:
#div1{
width:300px;
height:300px;
border:1px solid red;
}
img{
vertical-align:middle;
width:100px;
height:100px; }
</style>
</head> <body> <div id="div1">
如果<img src="data:images/mm.jpg" />一个>垂直span>对齐元素没有基线-也就是说,如果这是一个图像或表单元素,或是其他其他替换元素,那么该元
</div>
显示如下:
为什么会这样?middle会把行内元素的中点与父元素基线对齐。
对于图片来说,元素中点就是图片中点,与父元素基线对齐,即父元素基线和
图片中点一样,所以出现了上面的情况
。
怎么让内容垂直居中?
#div1加上
display:table-cell;
vertical-align:middle;
就可以了。
下面的代码怎么显示;(只在img
增加了一句:
vertical-align:top;)
#div1{
width:300px;
height:300px;
border:1px solid red;
display:table-cell;
vertical-align:middle;
}
img{ vertical-align:top;
width:100px;
height:100px; }
</style>
</head> <body>
<!--
<p>如果<img src="data:images/mm.jpg" style="width:100px;height=100px;"/>一个<span>垂直</span>对齐元素没有基线-也就是说,如果这是一个图像或表单元素,或是其他其他替换元素,那么该元素的底端与其
父元素的基线对齐
</p> -->
<div id="div1">
如果<img src="data:images/mm.jpg" />一个>垂直对齐元素没有基线-也就是说,如果这是一个图像或表单元素,或是其他其他替换元素,那么该元
</div>
</body>
可以看到,加上后图片没有影响,只是与图片同行的文字上移动了。为什么?
在img加上vertical-align:top;表示元素及其后代的顶端与整行的顶端对齐。文字与行框顶端对齐了。
(上面的文字和图片外部不能加《p》或其他快标签,否则就不居中了)
vertical-align
in table cells
When used in table cells, vertical-align
does what most people expect it to, which is mimic the (old, deprecated) valign
attribute. In a modern, standards-compliant browser, the following three code snippets do the same thing:
<td valign="middle"> <!-- but you shouldn't ever use valign --> </td>
<td style="vertical-align:middle"> ... </td>
<div style="display:table-cell; vertical-align:middle"> ... </div>
Shown in your browser, the above (with appropriate wrappers) display as:
<td> using valign="middle" |
<td> using valign="bottom" |
<td> using vertical-align:middle |
<td> using vertical-align:bottom |
<div>
using display:table-cell; vertical-align:middle
<div>
using display:table-cell; vertical-align:bottom
vertical-align
on inline elements
When vertical-align
is applied to inline elements, however, it's a whole new ballgame. In this situation, it behaves like the (old, deprecated) align
attribute did on <img>
elements. In a modern, standards-compliant browser, the following three code snippets do the same thing:
<img align="middle" ...>
<img style="vertical-align:middle" ...>
<span style="display:inline-block; vertical-align:middle"> foo<br>bar </span>
vertical-align
on other elements
Technically, this CSS attribute doesn't go on any other kinds of elements. When the novice developer applies vertical-align
to normal block elements (like a standard <div>
) most browsers set the value to inherit to all inline children of that element.
严格来说vertical-align属性只对inline和table-cell有作用,当新手应用它到块元素时,大多数浏览器设置子内联元素继承它。
这里错误vertical-align是不能继承的。
So how do I vertically-center something?!
If you are reading this page, you're probably not as interested in why what you were doing is wrong. You probably want to know how to do it properly.
Method 1
The following example makes two (non-trivial) assumptions. If you can meet these assumptions, then this method is for you:
- You can put the content that you want to center inside a block and specify a fixed height for that inner content block.
- It's alright to absolutely-position this content. (Usually fine, since the parent element inside which the content is centered can still be in flow.
If you can accept the above necessities, the solution is:
- Specify the parent container as
position:relative
orposition:absolute
. - Specify a fixed height on the child container.
- Set
position:absolute
andtop:50%
on the child container to move the top down to the middle of the parent. - Set
margin-top:-yy
where yy is half the height of the child container to offset the item up.
An example of this in code:
<style type="text/css">
#myoutercontainer { position:relative }
#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>
...
<div id="myoutercontainer">
<div id="myinnercontainer">
<p>Hey look! I'm vertically centered!</p>
<p>How sweet is this?!</p>
</div>
</div>
这个方法很常见,设置top:50%,关键是margin-top为height的一半。
Method 2
This method requires that you be able to satisfy the following conditions:
- You have only a single line of text that you want to center.
- You can specify a fixed-height for the parent element.
If you can accept the above necessities, the solution is:
- Set the
line-height
of the parent element to the fixed height you want.
An example of this in code:
<style type="text/css">
#myoutercontainer2 { height:4em; line-height:4em }
</style>
...
<p id="myoutercontainer2">
Hey, this is vertically centered. Yay!
</p>
In your browser, the above example renders as:
Hey, this is vertically centered. Yay!
就把line-height设置成与height相同。
更多:http://www.zhangxinxu.com/wordpress/2010/05/%E6%88%91%E5%AF%B9css-vertical-align%E7%9A%84%E4%B8%80%E4%BA%9B%E7%90%86%E8%A7%A3%E4%B8%8E%E8%AE%A4%E8%AF%86%EF%BC%88%E4%B8%80%EF%BC%89/
css vertical-align全解的更多相关文章
- 你不知道的CSS背景—css背景属性全解
CSS背景在网页设计中使用频率非常高,然而对于这个开发人员很熟悉的CSS属性,却隐藏着许多不为初级开发人员熟知的细节,这篇文章尝试扒开这层不为人知的面纱. 首先列举一下CSS中关于元素背景的所有属性并 ...
- css系列教程1-选择器全解
全栈工程师开发手册 (作者:栾鹏) 一个demo学会css css系列教程1-选择器全解 css系列教程2-样式操作全解 css选择器全解: css选择器包括:基本选择器.属性选择器.伪类选择器.伪元 ...
- Echarts数据可视化全解注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...
- Echarts数据可视化visualMap,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...
- jQuery Mobile 所有data-*选项,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) jQuery Mobile事件全解 jQuery Mobile 所有class选项 jQuery Mobile 所有data-*选项 jQuery Mobile 所 ...
- Latex排版全解
Latex排版全解 LATEX(英语发音:/ˈleɪtɛk/ LAY-tek或英语发音:/ˈlɑːtɛk/ LAH-tek,音译“拉泰赫”),是一种基于TEX的排版系统,由美国电脑学家莱斯利•兰伯特在 ...
- [转]CSS hack大全&详解
转自:CSS hack大全&详解 1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的, ...
- IOS-UITextField-全解
IOS-UITextField-全解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame: ...
- Echarts数据可视化series-scatter散点图,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...
- Echarts数据可视化series-radar雷达图,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...
随机推荐
- 内存管理之二——Cocos2d-x学习历程(六)
1.工厂方法 工厂方法是程序设计中一个经典的设计模式,指的是基类中只定义创建对象的接口,将实际的实现推迟到子类中. CCObject* factoryMethod() { CCObject* ret ...
- Android BaseAdapter
ListView显示与缓存机制: 只会加载当前屏幕所要显示的数据.显示完成就会被回收到Recycler中. BaseAdapter 基本结构: public int g ...
- vb6.0 时间日期
使用year(now)可以得到4位数的年 你还可以用Format来得到, 还有FormatDateTime 下面两种都是一样的结果: FormatDateTime(now,vbLongDate ...
- WINDOWS硬件通知应用程序的常方法(五种方式:异步过程调用APC,事件方式VxD,消息方式,异步I/O方式,事件方式WDM)
摘要:在目前流行的Windows操作系统中,设备驱动程序是操纵硬件的最底层软件接口.为了共享在设备驱动程序设计过程中的经验,给出设备驱动程序通知应用程序的5种方法,详细说明每种方法的原理和实现过程,并 ...
- Autoconf/Automake工具简介
在linux下编程的时候,有时候工程项目很大,文件比较多,此时需要使用自动创建Makefile文件功能.也就是使用Autoconf/Automake工具自动生成Makefile,为编译程序带来了方便, ...
- poj2140---herd sums
#include<stdio.h> #include<stdlib.h> int main() { ,i,j; scanf("%d",&n); ;i ...
- sql server dateadd()
定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的 ...
- JavaScript之对象学习
对象是一种非常重要的数据类型,他是一种自包含的数据集合,包含在对象里面的数据可以通过属性和方法两种形式来访问; 1.属性是隶属于某个特定对象的变量; 2.方法是只有某个特定对象才能调用的函数; 而对象 ...
- C++中#include的工作原理
大多数人可能对“#include”比较熟悉,因为我们写C/C++程序的时候都会写的字符串之一,但是它是具体怎么工作的?或者它的原理是什么呢? 可能不太熟悉,也有可能没有去关心过.我们只关心程序能否正确 ...
- SET QUOTED_IDENTIFIER (Transact-SQL)
使 SQL Server 遵从关于引号分隔标识符和文字字符串的 ISO 规则. 由双引号分隔的标识符可以是 Transact-SQL 保留关键字,也可以包含 Transact-SQL 标识符语法约定通 ...