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 ...
随机推荐
- 题解 CF656G 【You're a Professional】
又是一道假黑题 它教会我们不要看难度标签 虽然这道题的数据范围很小,用cin能过,但我还是要讲一下快读 快读嘛,顾名思义,就是 快速读入 的意思 有同学就会问了,快速读入的原理是什么? 答:它的原理其 ...
- 基于 muse-ui 封装一个微信公众号上传插件 实现多图上传
Vue.component('my-wx-upload', { template: ` <mu-grid-list :cols="3" :cellHeight="9 ...
- nodejs 实现图片上传
1.首先在目录下的运行cmd,执行以下命令 npm install multer; 2.在router下新建upload.js let express = require('express');let ...
- git出现误修改如何撤销
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步, ...
- Zimber 8.8.12卸载后重新安装报错解决办法
1.1 zimber故障处理步骤 1.1.1 现象描述 Running Post Installation Configuration: /opt/zimbra/bin/zmlocalconfig ...
- PHP操作redis的常用例子
Redis常用的例子 1,connect 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: &l ...
- 怎么选取训练神经网络时的Batch size?
怎么选取训练神经网络时的Batch size? - 知乎 https://www.zhihu.com/question/61607442 深度学习中的batch的大小对学习效果有何影响? - 知乎 h ...
- python3.7 迭代器和生成器
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 迭代器和生成器 #迭代器协议: ''' 1.迭代器协议是指:对象 ...
- Tame Me【驯服我】
Tame Me “Good morning,” said the fox. 早上好,狐狸说 “Good morming,” the little prince responded politely,a ...
- Linux系统属性文件详解
1)inode概述 中文意思就是索引节点(index node)第一部分是inode 第二部分是block inode主要用来存放文件属性信息的(也就是ls - l 的结果)包含的属性信息包括文件的大 ...