inline-block元素垂直对齐
多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素。
另一种方法是添加line-height:normal,也能实现多个inline-block元素的对齐或inline-block容器内文本的垂直居中。
例如:
<h3>Vertical-align of multiple elements by default not possible</h3>
<div class="block">
<div class="inner inner1">Inline-Block</div>
<div class="inner inner2">Inline-Block</div>
</div> <h3>With an added pseudo element it's possible</h3>
<div class="block2">
<div class="inner">Inline-Block</div>
<div class="inner">Inline-Block</div>
</div> <h3>It also works with just an added line-height and nothing else</h3>
<div class="block3">
<div class="inner inner3">Inline-Block with some lengthy text to show that it also works with multiple lines.</div>
<div class="inner inner3">Inline-Block.</div>
</div> <h3>Button with vertically centered mult-line text</h3>
<div class="block4">
<div class="inner inner4">Inline-Block with centered text.</div>
</div>
/* By default vertical-align ist not possible, only different elements can be vertically aligned among eachother */
.block {
background: red;
height: 100px;
} .inner {
display: inline-block;
vertical-align: middle;
background: yellow;
padding: 3px 5px;
} .inner1 {
height: 30px;
} .inner2 {
height: 20px;
} /* With an added fake (pseudo) element it works. IMPORTANT: There must be no spaces between the elements in the source, else it doesn't work! */
.block2 {
background: orange;
height: 80px;
} /* Fake (pseudo) element, that enables vertical-align */
.block2:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
} /* Also works if you set line-height instead of the height (or together with the same value as the height). No pseudo-element needed. */
.block3 {
background: green;
/*height: 120px;*/
line-height: 120px;
} .inner3 {
width: 30%;
line-height: normal; /* Reset line-height for the child. IMPORTANT: Must be "normal", no integer value allowed! */
} .block4 {
background: magenta;
line-height: 60px;
} .inner4 {
line-height: normal; /* Reset line-height for the child. */
background: none;
} /* Miscellaneous styling */
@import url(https://fonts.googleapis.com/css?family=PT+Sans:400,700); h3, div {
font-family: 'PT Sans', sans-serif;
} .block, .block2, .block3 {
border: 5px dotted rgba(0,0,0,.4);
background-clip: padding-box;
width: 50%;
} .block4 {
text-align: center;
width: 20%;
box-shadow: 3px 3px 0 black;
} h3 {
margin: 40px 0 7px;
}
出处: https://codepen.io/edge0703/pen/iHJuA
inline-block元素垂直对齐的更多相关文章
- 【CSS学习】--- 文本水平对齐属性text-align和元素垂直对齐属性vertical-align
一.文本水平对齐属性---text-align text-align属性是将块级标签以及单元格里面的内容进行相应的对齐,块级标签里的内联元素会被整体进行移动,而子块级元素或子单元格则会继承父元素的te ...
- vertical-align表单元素垂直对齐
原文地址:http://www.blueidea.com/tech/web/2009/6910.asp 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio).但是在前 ...
- 文字和表单(checkbox/radio)元素垂直对齐方法,兼容Firefox和IE。
这几天在做表单时总会碰到复选框(checkbox)和单选框(radio)与文字不对齐的问题,要不是checkbox上浮了,要不是文字上浮.在前端开发过程中,单(复)选框和它们后面的提示文字在不进行任何 ...
- CSS3如何去除 inline block 元素之间多出的空格
display: inline-block 属性很好的避免了元素的浮动问题,但是会有点小问题,就是 inline-block 元素间的回车会被显示为一个空格.然而,我们写代码时,都是用回车来格式化的. ...
- vertical-align垂直对齐用法
一.垂直对齐方式{vertical-align:middle/top/bottom:} <img>垂直对齐方式:1)给自身加vertical-align:再设置line-height即可: ...
- block元素和inline元素的特点
一.block元素的特点 1.处于常规流中时,如果width没有设置,会自动填充满父容器 2.可以设置height/width及margin/padding 3.处于常规流中时,布局在前后元素位置之间 ...
- vertical-align 属性设置元素的垂直对齐方式。
值 描述 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶 ...
- 多个inline元素、block元素、inline-block元素在父容器中的换行情况
1.首先看inine元素的换行情况 <style> *{padding:0;margin:0} div.wrap{width:200px;height:200px;border:1px s ...
- 当inline元素包裹block元素时会发生什么
经常有图片链接写法如下: <a href="www.baidu.com"><img src="baidu.jpg" /></a&g ...
随机推荐
- python_35_进度条
import sys for i in range(50): sys.stdout.write("+")#此命令不会像print语句执行一次,换行一次\ sys.stdout.fl ...
- [solr 管理界面] - 索引数据删除
删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...
- C# 创建和初始化集合对象
一. 引言 C# 3.0中新的对象初始化器是一种简单的语法特征-借助于这种特征,对象的构建和初始化变得非常简单.假定你有一个类Student,它看起来有如下样子: public class Stude ...
- GPT分区表的备份与恢复
GPT分区表的备份与恢复 keenshoes 2016-01-13 21:02:25 关键词: GPT, Partition, MBR,APPLE, GUID, Protective MBR 对于现 ...
- Python导入模块方法
import module_name 导入整个模块 from module_name import function_name 导入特定函数 from module_name import funct ...
- JZOJ 3493. 【NOIP2013模拟联考13】三角形
3493. [NOIP2013模拟联考13]三角形(triangle) (File IO): input:triangle.in output:triangle.out Time Limits: 10 ...
- javaWeb开发中常见的问题
1.修改表单提交的时候不好使可能是因为没写对应隐藏域的ID 2.el表达式在js代码中要加“”,例如 "${}" 3.JavaScript中的函数也有重载的特性.如果两个input ...
- 刷表法动态规划:HOJ11391_Word Clouds Revisited
题目大意,给若干方块,让把方块拍成若干行,使得最终高度最小.其中,每行有宽度限制,高度为每行中最高的箱子的高度. 于是,很直观的认为,这个题可能也许大概应该是个动态规划的题. 于是,设DP[K]为K及 ...
- WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...
- POJ 2311 Cutting Game(SG函数)
Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4806 Accepted: 1760 Desc ...