要實現行內元素(<span>、<a>等)的水平居中,只需把行內元素包裹在塊級父層元素(<div>、<li>、<p>等)中,并且在父層元素CSS設置如下:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { text-align: center; background-color: #666;}#center { color: #fff; font-size: 20px;}</style></head><body><div id='container'> <span id = 'center'>#center</span> </div></body></html>并且適用于文字,鏈接,及其inline或者inline-block、inline-table和inline-flex。塊狀元素的水平居中
要實現塊狀元素(display:block)的水平居中,我們只需要將它的左右外邊距margin-left和margin-right設置為auto,即可實現塊狀元素的居中,要水平居中的塊狀元素CSS設置如下:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { height: 100px; background: #d6d6d6;}#center { margin: auto; width: 100px; height: 100px; background-color: #666; color: #fff; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <div id = 'center'>#center</div></div></body></html>多個塊狀元素的水平居中
要實現多個水平排列的塊狀元素的水平居中,傳統的方法是將要水平排列的塊狀元素設為display:inline-block,然后在父級元素上設置text-align:center,達到與上面的行內元素的水平居中一樣的效果。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { height: 100px; background: #d6d6d6;}#center { margin: auto; width: 100px; height: 100px; background-color: #666; color: #fff; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <div id = 'center'>#center</div> <div id = 'center'>#center</div> <div id = 'center'>#center</div></div></body></html>
![]()
已知高度寬度元素的水平垂直居中
法一.
絕對定位與負邊距實現。
利用絕對定位,將元素的top和left屬性都設為50%,再利用margin邊距,將元素回拉它本身高寬的一半,實現垂直居中。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { background: #d6d6d6; height: 300px; position: relative;}#center { position: absolute; top: 50%; left: 50%; margin: -50px 0 0 -50px; width: 100px; height: 100px; background-color: #666; color: #fff; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <div id = 'center'>#center</div></div></body></html>
![]()
法二.
絕對定位與margin。
這種方法也是利用絕對定位與margin,但是無需知道被垂直居中元素的高和寬。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { position: relative; height: 300px; background: #d6d6d6;}#center { position: absolute; margin: auto; top: 0; bottom: 0; left: 0; right: 0; width: 100px; height: 100px; background-color: #666; color: #fff; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <div id = 'center'>#center #center #center #center</div></div></body></html>未知高度和寬度元素的水平垂直居中
法一. 當要被居中的元素是inline或者inline-block元素
當要被居中的元素是inline或者inline-block的時候,可以巧妙的將父級容器設置為display:table-cell,配合text-align:center和vertical-align:middle即可以實現水平垂直居中。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { width: 600px; height: 300px; background: #d6d6d6; display: table-cell; text-align: center; vertical-align: middle;}#center { background-color: #666; color: #fff; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <span id='center'>#center</span> </div></body></html>
![]()
法二. Css3顯威力
利用Css3的transform,可以輕松的在未知元素的高寬的情況下實現元素的垂直居中。
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { height: 300px; background: #d6d6d6; position: relative;}#center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100px; height: 100px; background-color: #666; color: #fff; font-weight: bold; font-size: 18px;}</style></head><body><div id='container'> <div id = 'center'>#center</div></div></body></html>
![]()
法三. flex布局輕松解決
使用flex布局,無需絕對定位等改變布局的操作,可以輕松實現元素的水平垂直居中。
核心代碼如下:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>垂直居中Demo</title><style type="text/css">#container { height: 300px; background: #d6d6d6; display: flex; justify-content: center; align-items: center;}#center { width: 100px; height: 100px; background-color: #666; color: #fff; font-weight: bold; font-size: 18px; display: flex;/*這個寫在這只是為了#center這幾個字的垂直居中*/ justify-content: center; align-items: center;}</style></head><body><div id='container'> <div id = 'center'>#center</div></div></body></html>
CSS3的transform和flex固然好用,但在項目的實際運用中必須考慮兼容問題,大量的hack代碼可能會導致得不償失。
某些瀏覽器仍需使用前綴寫法:
.flexboxtest{
display: flex;
display: -webkit-flex; //Safari仍舊需要使用特定的瀏覽器前綴
}
新聞熱點
疑難解答