花式垂直居中
方法一:
原理:在元素屬性為table使用里使用vertical-align屬性
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#wrapper {display: table;height: 100px;width: 60px;}#cell {display: table-cell;vertical-align: middle;}</style></head><body><div id="wrapper"><div id="cell"><div class="content">Content goes here</div></div></div></body></html> 復制代碼方法二:
原理:對于有固定寬高的元素,利用絕對定位設置為:top: 0; bottom: 0; left: 0; right: 0; margin: auto因為這個元素有固定的高度,并不能距離上下都為0,margin: 0會使它居中顯示。
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#content {position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;height: 140px;width: 70%;}</style></head><body><div id="content">Content goes here</div></body></html> 復制代碼方法三:
原理:利用行高,設置line-height和元素等高就行。
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#content {height: 100px;line-height: 100px;}</style></head><body><div id="content">Content goes here</div></body></html> 復制代碼方法四:
原理:使用css3的display: -webkit-box屬性,再設置-webkit-box-pack: cente; -webkit-box-align:c enter
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#content {width:90px;height:90px;display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;}</style></head><body><div id="content">Content Here</div></body></html> 復制代碼方法五:
原理:使用css3的新屬性transform: translate(x,y)屬性
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#content {position: absolute;width: 80px;height: 80px;top: 50%;left: 50%;transform: translate(-50%, -50%);-webkit-transform: translate(-50%, -50%);-moz-transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%);background: green;}body {position: relative;height: 400px;width: 400px;}</style></head><body><div id="content">Content Here</div></body></html>復制代碼方法六:
原理:使用:before元素
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style media="screen">#warp {position: fixed;display: block;top: 0;right: 0;bottom: 0;left: 0;text-align: center;background: rgba(0, 0, 0, .5);}#warp:before {content: '';display: inline-block;vertical-align: middle;height: 100%;}#warp #content {display: inline-block;width: 100px;height: 100px;line-height: 100px;background: yellow;}</style></head><body><div id="warp"><div id="content">Content Here</div></div></body></html>復制代碼總結
- 上一篇: CSS学习之多类别选择器
- 下一篇: HTTP面试题都在这里