Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
過前端開發的朋友都知道,要使文字在一個div容器垂直居中顯示是很簡單的事,但是如果是要讓一張圖片在div容器中垂直顯示那就有點傷腦筋了,因為圖片是個置換元素,有些特殊的特性,對于前端開發的大牛來說還好,解決方法有很多,比如什么利用js啊之類的,但是這些對于初學者來說就有些難度了,今天小編就把自己平時在項目中用的方法分享給大家,給大家一個參考。
其實小編用的方法也不是什么很高端的技術,算是最low的方法吧,但是很實用,能兼容各種瀏覽器,為了演示,小編直接從某寶商家店鋪里拿了一張商品圖來做演示,因為商品圖都是高清無碼的,且還很大,所以我們在CSS中把圖片的高度設置為200px,div容器的高度為300px,完整的代碼如下:
最終的運行效果如下圖:
可以看到,這個圖片在div中水平、垂直都是在正中間,是不是很完美。這個原理也很簡單,就是在div容器中加了個 <i> 標簽并把它的 display 屬性設置為 inline-block(行內塊元素),讓它去撐開div容器的高度,再把圖片的 vertical-align 屬性設置為 middle 就可以垂直居中了,當然這只是個演示,大家在實際的項目中可以利用 max-width、min-width、max-height、min-height 等屬性讓圖片根據需要自適應,以達到想要的效果。
當然解決方法還有很多種,具體用什么方法需根據自身頁面布局去權衡,總之沒有最好的,只有最適合的,如果你有更好的方法也可以告訴小編哦,一起交流方法,共同進步。
直居中-父元素高度確定的單行文本
父元素高度確定的單行文本的豎直居中的方法是通過設置父元素的height和line-height高度一致來實現的。如下代碼:
<div class="container">
hi,imooc!
</div>
css代碼:
<style>
.container{
height:100px;
line-height:100px; /* 僅能用于單行文本 */
background:#999;
}
</style>
垂直居中-圖片以及行內塊元素
<div class="container">
<img src="imgegs/icon.png" />
</div>
css代碼:
<style>
.container{
height:100px;
background:#999;
}
.container img{
vertical-align:middle;
}
</style>
垂直居中-父元素高度確定的多行文本(方法一)
父元素高度確定的多行文本、圖片、塊狀元素的豎直居中的方法有兩種:
方法一:使用插入table(包括tbody、tr、td)標簽, 同時設置vertical-align:middle。
說到豎直居中,css中有一個用于豎直居中的屬性vertical-align,但這個樣式只有在父元素為td 或th時,才會生效。所以又要插入table標簽了。
下面看一下例子:
html代碼:
<body>
<table><tbody><tr><td class="wrap">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</td></tr></tbody></table>
</body>
css代碼:
table td{height:500px;background:#ccc}
因為td標簽默認情況下就默認設置了vertical-align為middle, 所以我們不需要顯式地設置了。
垂直居中-父元素高度確定的多行文本(方法二)
在chrome、firefox及IE8以上的瀏覽器下可以設置塊級元素的display為table-cell, 激活vertical-align屬性, 但注意IE6、7并不支持這個樣式。
html代碼:
<div class="container">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</div>
css代碼:
<style>
.container{
height:300px;
background:#ccc;
display:table-cell; /*IE8以上及Chrome、Firefox*/
vertical-align:middle; /*IE8以上及Chrome、Firefox*/
}
</style>
這種方法的好處是不用添加多余的無意義的標簽,但缺點也很明顯,它的兼容性不是很好,不兼容 IE6、7。
垂直居中--方法三
<!DOCTYPE html>
<html lang="zh">
<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 type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 300px;
background-color: orange;
}
/*
* 思路一:left:50%;top:50%;margin-left: -200px;margin-top: -150px;
* 思路二:left:0;top:0;right:0;bottom:0;margin:auto;
* */
div{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%); /* 平移 */
}
</style>
</head>
<body>
<div></div>
</body>
</html>
實例1:將內層div的文本垂直居中
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>父元素高度確定的多行文本</title>
<style>
.container{
height:300px;
background:#ccc;
display:table-cell; /*IE8以上及Chrome、Firefox*/
vertical-align:middle; /*IE8以上及Chrome、Firefox*/
}
</style>
</head>
<body>
<div class="container">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</div>
<!--下面是代碼任務區-->
</body>
</html>
實例2:將內層垂直居中、外層水平居中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
<style type="text/css">
#content{
width:300px;
height:300px;
border:#000 solid 1px;
margin:auto;
display:table;
}
#wenzi{
border:#F00 solid 1px;
text-align:center;
display:table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="content">
<div id="wenzi">
鋤禾日當午,<br>
汗滴禾下土。<br>
誰知盤中餐,<br>
粒粒皆辛苦。<br>
</div>
</div>
</body>
</html>
實例3: 使用絕對定位垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 220px;
height: 280px;
background: url("img/王思聰.jpg");
position: absolute;
left: 50%;
top: 50%;
margin-left: -110px;
margin-top: -140px;
}
</style>
</head>
<body>
<!--
行內元素(文本)->水平垂直居中
text-align: center;
line-height: height;
-->
<!--
塊元素->水平垂直居中
margin: 0 auto;
-->
<div></div>
</body>
</html>
實例4: 使用絕對定位垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 600px;
height: 200px;
padding: 10px 20px;
border: 1px solid #000;
border-radius: 5px;
/* 下面這種寫法也可以讓一個盒子居中 */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div>您確定刪除:重慶萬州公交墜江事件結果公布后,司乘糾紛和公交駕駛安全問題成為人們熱議的焦點,如何預防和避免惡性結果的發生,才是問題的關鍵?!肮膭钍忻衽e報,并對勇于制止干擾公交車正常行駛違法行為的公民予以獎勵?!弊蛉障挛纾靼彩泄簿止步煌ǚ志终匍_媒體通氣會,通報西安相關安全舉措。這條消息嗎</div>
</body>
</html>
絕對定位(固定定位)之后, 所有標準流的規則, 都不適用了。所以margin:0 auto; 失效。
解決辦法:left:50%; margin-left:負的寬度的一半。(三句話)
div{
width: 600px;
height: 60px;
position: absolute; /* → 第一句 */
left: 50%; // /* → 第二句 */
top: 0;
margin-left: -300px; /*→ 第三句。寬度的一半*/
}
實例4:使用絕對定位和margin:auto垂直居中
當父div的行高等于自身高度時,內部的行內元素會上下居中顯示。行內塊沒有固定高度時也會上下居中顯示。所以需要對父div的 line-height 進行調整。利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。定位屬性top和bottom(或是left和right)值分別設置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設置 margin:auto 會使它居中顯示。
轉載自喜歡JS的無名小站
例如 一個父div(w:100%;h:400px)中有一個子div(w:100px;100px;)。讓其上下左右居中。
利用表格單元格的居中屬性。
父div外層配置一個div,同時設置為表格元素 (display: table),寬度為100%
父div配置為表格單元格元素 (display: table-cell)
父div配置居中屬性(vertical-align: middle),使子div上下居中
子div通過margin配置左右居中(margin-left:auto; margin-right:auto)
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .table {display: table; width: 100%;} .father {display: table-cell; vertical-align: middle;} .son {margin: auto;} </style> <body> <div class="table" > <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </div> </body>
注:
表格單元格比較特殊,如果只有一個單元格時,它的寬度默認會占父級(table|tr)寬度的100%;
table默認寬度不會撐開,需要手動配置width:100%;
當父div的行高等于自身高度時,內部的行內元素會上下居中顯示。行內塊沒有固定高度時也會上下居中顯示。通過文本居中屬性text-align:center
,可以使內部行內元素或行內塊元素左右居中顯示。
子div設定為行內塊元素(display:inline-block);
父div設置行高(line-height)使子div上下居中
父div設置文本居中(text-align:center)使子div左右居中。
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {line-height: 500px; text-align: center; font-size: 0;} .son { display: inline-block; /* display: inline-flex; display: inline-grid; display: inline-table; */ } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
注: 行高如果設置為當前父div的高度(400px)的話,有固定高度的子div并不會居中顯示的,問題出在瀏覽器默認將其當做文本居中的,即把它當做了一段文本(chrome默認font-size:16px;hight:21px)進行居中,沒把它當做高度100px進行居中。所以需要對父div的line-height
進行調整。以font-size:0
(對應的字體高度為0)為例子,則需要line-height增加一個子div的高度(400px + 100px;)。
利用定位屬性(top、left、right、bottom)百分比的模式。若為100%,則代表偏移的長度為父div的高度(寬度)的100%。
父div標記下定位(position:relative|absolute|fixed);子div絕對定位(position:absolute)
子div上下居中:top:50%;margin-top:-h/2;
或是 bottom:50%;margin-bottom:-h/2;
;
子div左右居中: left:50%;margin-left:-w/2
或是 right:50%;margin-right:-w/2
;
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px; } </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
定位屬性top和bottom(或是left和right)值分別設置為0,但子div有固定高度(寬度),并不能達到上下(左右)間距為0,此時給子div設置margin:auto會使它居中顯示。
父div標記下定位(position:relative|absolute|fixed|sticky);子div絕對定位(position:absolute)
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
彈性盒子,自帶的一個居中功能
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {display: flex; align-items: center} .son {margin: auto} </style> <body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </body>
flex兼容性,以及存在的已知問題
方法二和方法三兼容性要比其它好些
Can I use
css-vertical-center-solution
CSS實現垂直居中的5種方法--前端觀察
*請認真填寫需求信息,我們會在24小時內與您取得聯系。