溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

css實(shí)現(xiàn)div水平和垂直居中

發(fā)布時(shí)間:2021-08-10 17:18:03 來源:億速云 閱讀:101 作者:chen 欄目:web開發(fā)

這篇文章主要介紹“css實(shí)現(xiàn)div水平和垂直居中”,在日常操作中,相信很多人在css實(shí)現(xiàn)div水平和垂直居中問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”css實(shí)現(xiàn)div水平和垂直居中”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

示例1 chrome33、ie8測(cè)試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* 固定寬高div,在瀏覽器中保持水平、垂直居中 */
#div1 {
width:400px; height:300px;
position:absolute;
left:50%; top:50%;
margin-left:-200px; margin-top:-150px;
background:#f90;
}
</style>
</head>
<body>
<div id="div1"></div>
</body>
</html>


示例2 chrome33、ie8測(cè)試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* 固定寬高div在固定寬高div中保持水平、垂直居中 */
#div1 {
width: 400px;height: 200px;
background-color: #f00;
position: relative;
/* 為div賦予單元格屬性,使其可以應(yīng)用align屬性 */
display: table-cell;
vertical-align: middle;
}
#div2 {
width: 200px;height: 100px;
background-color: #0f0;
/* 使當(dāng)前標(biāo)簽在父標(biāo)簽中水平居中,即0 auto 0 auto */
margin: 0 auto;
display: block;
}
</style>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


示例3 chrome33、ie8測(cè)試通過:

代碼如下:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#div1 {
width: 400px;height: 200px;
background-color: #f00;
/* 使當(dāng)前標(biāo)簽在父標(biāo)簽位置固定 */
position: absolute;
top: 50%;left: 50%;
margin: -100px 0 0 -200px;
}
#div2 {
width: 200px;height: 100px;
background-color: #0f0;
display: block;
/* 使當(dāng)前標(biāo)簽在父標(biāo)簽中保持水平、垂直居中 */
top: 50%;left: 50%;
margin: 50px 0 0 100px;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


示例4 chrome33測(cè)試通過,ie8測(cè)試不通過,參考http://www.w3school.com.cn/tiy/t.asp?f=css3_box-pack:

代碼如下:


<!DOCTYPE html>
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* 使用box-pack樣式實(shí)現(xiàn)div中子元素居中 */
#div1 {
width:350px;
height:200px;
border:1px solid black;
/* Firefox */
display:-moz-box;
-moz-box-pack:center;
-moz-box-align:center;
/* Safari, Chrome, and Opera */
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;
/* W3C */
display:box;
box-pack:center;
box-align:center;
}
#div2 {
width:100px;
height:50px;
background-color: #ff0;
border:1px solid black;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>


注:對(duì)頁面中所有元素應(yīng)用box-sizing:border-box樣式是為了將padding、margin值都計(jì)入width、height中,即為元素指定的任何內(nèi)邊距和邊框都將在已設(shè)定的寬度和高度內(nèi)進(jìn)行繪制(http://www.w3school.com.cn/cssref/pr_box-sizing.asp)。在上述代碼中應(yīng)用該樣式無意義,但是在企業(yè)級(jí)應(yīng)用中應(yīng)用該樣式可以減少很多界面缺陷,如標(biāo)簽未對(duì)齊。

到此,關(guān)于“css實(shí)現(xiàn)div水平和垂直居中”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI