多个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元素垂直对齐的更多相关文章

  1. 【CSS学习】--- 文本水平对齐属性text-align和元素垂直对齐属性vertical-align

    一.文本水平对齐属性---text-align text-align属性是将块级标签以及单元格里面的内容进行相应的对齐,块级标签里的内联元素会被整体进行移动,而子块级元素或子单元格则会继承父元素的te ...

  2. vertical-align表单元素垂直对齐

    原文地址:http://www.blueidea.com/tech/web/2009/6910.asp 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio).但是在前 ...

  3. 文字和表单(checkbox/radio)元素垂直对齐方法,兼容Firefox和IE。

    这几天在做表单时总会碰到复选框(checkbox)和单选框(radio)与文字不对齐的问题,要不是checkbox上浮了,要不是文字上浮.在前端开发过程中,单(复)选框和它们后面的提示文字在不进行任何 ...

  4. CSS3如何去除 inline block 元素之间多出的空格

    display: inline-block 属性很好的避免了元素的浮动问题,但是会有点小问题,就是 inline-block 元素间的回车会被显示为一个空格.然而,我们写代码时,都是用回车来格式化的. ...

  5. vertical-align垂直对齐用法

    一.垂直对齐方式{vertical-align:middle/top/bottom:} <img>垂直对齐方式:1)给自身加vertical-align:再设置line-height即可: ...

  6. block元素和inline元素的特点

    一.block元素的特点 1.处于常规流中时,如果width没有设置,会自动填充满父容器 2.可以设置height/width及margin/padding 3.处于常规流中时,布局在前后元素位置之间 ...

  7. vertical-align 属性设置元素的垂直对齐方式。

     值 描述 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶 ...

  8. 多个inline元素、block元素、inline-block元素在父容器中的换行情况

    1.首先看inine元素的换行情况 <style> *{padding:0;margin:0} div.wrap{width:200px;height:200px;border:1px s ...

  9. 当inline元素包裹block元素时会发生什么

    经常有图片链接写法如下: <a href="www.baidu.com"><img src="baidu.jpg" /></a&g ...

随机推荐

  1. python读取txt写入txt

    http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html

  2. Jquery动态添加多行,返回数据至每一行中

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_channel_ed ...

  3. React后台管理系统-后台接口封装

    1新建文件夹 service ,里边建4个文件,分别是statistic-service.jsx 首页数据统计接口, user-service.jsx用户接口, product-service.jsx ...

  4. linux apache 不解析php文件显示源码

    首先检查是否安装PHP 没有的话就先安装 如果安装过 在/etc/httpd/conf/httpd.conf文件中 在<IfModule mime_module>里面 AddType ap ...

  5. Servlet 的生命周期 及 注意事项 总结

    Servlet的生命周期 图解Servlet的生命周期 生命周期的各个阶段 实例化 :Servlet 容器创建 Servlet 的实例 初始化 :该容器调用init() 方法 请求处理 :如果请求Se ...

  6. wepy一些问题和解决方案

    wepy一些问题和解决方案 小程序开发和传统的web开发有相识的地方,但是也有不同的地方,要区分. computed属性名和props属性名重复 如果那个组件的渲染值是重名的computed属性,每次 ...

  7. 【SAM manacher 倍增】bzoj3676: [Apio2014]回文串

    做法一:PAM:做法二:SAM+manacher.前缀树上倍增 Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你 ...

  8. 用JavaScript实现CheckBox的全选取消反选,及遮罩层中添加内容

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. mysql -u root -p 解释

    使用此命令首先确保你的mysql运行环境已经搭建好 这是客户端连接mysql服务器的指令,比较全的写法是下面两种 第一个是全拼,第二个是第一个的缩写 mysql --host=localhost -- ...

  10. arduino 语音音箱 :语音控制、MP3播放、报时、回复温湿度情况

    arduino 语音音箱 :语音控制.MP3播放.报时.回复温湿度情况 效果图 线路图 包装后的效果 功能 需要材料 arduino板 MP3播放模块及喇叭 时钟模块 温湿度模块 语音识别模块 面包板 ...