CSS 元素垂直居中的 6种方法

2017-07-05 09:48:51

利用CSS进行元素的水平居中,比较简单,行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可。本文收集了六种利用css进行元素的垂直居中的方法,每一种适用于不同的情况,在实际的使用过程中选择某一种方法即可。

Line-Height Method

line height demo
试用:单行文本垂直居中,demo

代码:

html

1
2
3
<div id="parent"><div id="child">Text here</div></div>

css

1
2
3
#child {line-height: 200px;}

垂直居中一张图片,代码如下

html

1
2
3
<div id="parent"><img src="image.png" alt="" /></div>

css

1
2
3
4
5
6
#parent {line-height: 200px;}#parent img {vertical-align: middle;}

CSS Table Method

table cell demo

适用:通用,demo

代码:

html

1
2
3
<div id="parent"><div id="child">Content here</div></div>

css

1
2
3
4
5
#parent {display: table;}#child {display: table-cell;vertical-align: middle;}

低版本 IE fix bug:

1
2
3
#child {display: inline-block;}

Absolute Positioning and Negative Margin

absolute positioning and negative margin demo

适用:块级元素,demo

代码:

html

1
2
3
<div id="parent"><div id="child">Content here</div></div>

css

1
2
3
4
5
6
7
8
9
#parent {position: relative;}#child {position: absolute;top: 50%;left: 50%;height: 30%;width: 50%;margin: -15% 0 0 -25%;}

Absolute Positioning and Stretching

absolute positioning and vertical stretching demo

适用:通用,但在IE版本低于7时不能正常工作,demo

代码:

html

1
2
3
<div id="parent"><div id="child">Content here</div></div>

css

1
2
3
4
5
6
7
8
9
10
11
#parent {position: relative;}#child {position: absolute;top: 0;bottom: 0;left: 0;right: 0;width: 50%;height: 30%;margin: auto;}

Equal Top and Bottom Padding

vertical centering with padding demo

适用:通用,demo

代码:

html

1
2
3
<div id="parent"><div id="child">Content here</div></div>

css

1
2
3
4
5
6
#parent {padding: 5% 0;}#child {padding: 10% 0;}

Floater Div

vertical centering with floater div demo

适用:通用,demo

代码:

html

1
2
3
4
<div id="parent"><div id="floater"></div><div id="child">Content here</div></div>

css

1
2
3
4
5
6
7
8
9
10
11
#parent {height: 250px;}#floater {float: left;height: 50%;width: 100%;margin-bottom: -50px;}#child {clear: both;height: 100px;}


  • 2021-01-21 13:55:53

    Mongodb字段更新$unset操作符

    当使用$操作符匹配任何数组元素,$unset替换指定的元素为null而不是删除掉指定的元素,此行为保持数组大小和位置一直;

  • 2021-01-22 08:30:02

    Android IO简化之Okio库

    如果之前有使用过Okhttp,那么你一定知道底层的IO读取是由square公司开发的Okio库。它补充了Java.io和java.nio的不足,以便能够更加方便,快速的访问、存储和处理你的数据。而在一般的开发中,我们也可以使用Okio来做IO读写,非常方便深得我心

  • 2021-01-22 21:56:48

    emcc生成wasm,wast,bc文件的方法

    Emscripten实现把C/C++文件转成wasm,wast(wasm的可读形式),llvm字节码(bc格式),ll格式(llvm字节码的可读形式)的步骤。

  • 2021-01-22 21:59:34

    emcc编译与部分重要参数选取

    C/C++代码通过emcc编译为字节码,然后根据不同的目标编译为asm.js或wasm。emcc和gcc编译选项类似,例如-s OPTIONS=VALUE、-O等。另外为了适应Web环境,emcc增加了一些特有的选项,如–pre-js 、–post-js 等。

  • 2021-01-22 22:01:19

    Emscripten Compiler Frontend (emcc)

    The Emscripten Compiler Frontend (emcc) is used to call the Emscripten compiler from the command line. It is effectively a drop-in replacement for a standard compiler like gcc or clang.

  • 2021-01-22 22:21:41

    emcc编译命令介绍

    这个输入文件file,既可以是clang可以编译的C/C++语言,也可以是二进制形式的llvm bitcode或者人类可读形式的llvm assembly文件。

  • 2021-01-22 22:25:51

    How to protect your JS code by WebAssembly

    对于iOS或是Android来说,我们可以将相关的算法通过C/C++进行编写,然后编译为dylib或是so并进行混淆以此来增加破解的复杂度,但是对于前端来说,并没有类似的技术可以使用。当然,自从asm.js及WebAssembly的全面推进后,我们可以使用其进一步增强我们核心代码的安全性,但由于asm.js以及WebAssembly标准的开放,其安全强度也并非想象中的那么美好。