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 ... 
随机推荐
- pytho线程信号量
			pytho线程信号量 import threading,time def going(num,sleep_time): semaphore.acquire()#启动允许执行 print("g ... 
- 在maven项目中 配置代理对象远程调用crm
			1 在maven项目中配置代理对象远程调用crm 1.1 在项目的pom.xml中引入CXF的依赖 <dependency> <groupId>org.apache.cxf&l ... 
- LGTB 学分块
			总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 LGTB 最近在学分块,但是他太菜了,分的块数量太多他就混乱了,所以只能分成 3 块 今天他得 ... 
- DevOps - 配置管理 - Chef
			#!/bin/sh # WARNING: REQUIRES /bin/sh # # - must run on /bin/sh on solaris 9 # - must run on /bin/sh ... 
- vue.js 服务端渲染nuxt.js反向代理nginx部署
			vue.js的官方介绍里可能提到过nuxt.js,我也不太清楚我怎么找到这个的 最近项目vue.js是主流了,当有些优化需求过来后,vue还是有点力不从心, 比如SEO的优化,由于vue在初始化完成之 ... 
- Python读取内容UnicodeDecodeError错误
			1.错误现象 环境:Python3.7 描述: 用open方法获取文件句柄: 用read/readlines方法一次读取文件所有内容: 尝试了编码GB2312/GBK/GB18030/UTF-8,发现 ... 
- 37.VUE学习之-表单的综合运用
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ... 
- 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季
			目录 要实现的功能 思路: 视图层 控制器里: 扩展函数里 要实现的功能 用表单里的提交过来的sort数据,批量修改表里的排序值 界面效果: 思路: 视图层表单提交数据主键=>sort值 控制器 ... 
- thinkphp 分页的 实现 和样式 分享
			aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgoAAABlCAIAAACjnlykAAAI8UlEQVR4nO3bP2/bSBrH8eSQ5rq0eh ... 
- Spark架构与作业执行流程简介(scala版)
			在讲spark之前,不得不详细介绍一下RDD(Resilient Distributed Dataset),打开RDD的源码,一开始的介绍如此: 字面意思就是弹性分布式数据集,是spark中最基本的数 ... 
