在網(wǎng)頁中將 HTML 元素水平居中,可以使用 CSS 中的 margin: 0 auto; 屬性。以下是一種常用的實(shí)現(xiàn)方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horizontal Centering</title>
<style>
.center {
width: 300px; /* 設(shè)置元素寬度 */
margin: 0 auto; /* 水平居中 */
background-color: lightblue;
padding: 20px;
}
</style>
</head>
<body>
<div class="center">
<p>This element is horizontally centered.</p>
</div>
</body>
</html>
在上面的示例中, center 類的元素使用了 width: 300px; 來設(shè)置寬度,然后通過 margin: 0 auto; 來實(shí)現(xiàn)水平居中。這樣,無論屏幕寬度如何變化,元素都會(huì)始終水平居中顯示。
您也可以將此樣式應(yīng)用到任何 HTML 元素(例如 div 、 span 、 p 等),以實(shí)現(xiàn)水平居中效果。
.水平居中的 margin:0 auto;
關(guān)于這個(gè),大家也不陌生做網(wǎng)頁讓其居中用的比較多, 這個(gè)是用于子元素上的,前提是不受float影響
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
border: 3px solid red;
/*text-align: center;*/
}
img{
display: block;
width: 100px;
height: 100px;
margin: 0 auto;
}
</style>
<!--html-->
<body>
<div class="box">

</div>
</body>
我自己是一名從事了多年開發(fā)的web前端老程序員,目前辭職在做自己的web前端私人定制課程,去年我花了一個(gè)月整理了一份最適合2019年學(xué)習(xí)的web前端學(xué)習(xí)干貨,各種框架都有整理,送給每一位前端小伙伴,想要獲取的可以關(guān)注我的頭條號(hào)并在后臺(tái)私信我:前端,即可免費(fèi)獲取。
2.水平居中 text-align:center;
img的display:inline-block;類似一樣在不受float影響下進(jìn)行 實(shí)在父元素上添加效果讓它進(jìn)行水平居中
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
border: 3px solid red;
text-align: center;
}
img{
display: inline-block;
width: 100px;
height: 100px;
/*margin: 0 auto;*/
}
</style>
<!--html-->
<body>
<div class="box">

</div>
</body>
3.水平垂直居中(一)定位和需要定位的元素的margin減去寬高的一半
這種方法的局限性在于需要知道需要垂直居中的寬高才能實(shí)現(xiàn),經(jīng)常使用這種方法
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
background:#e9dfc7;
border:1px solid red;
position: relative;
}
img{
width: 100px;
height: 150px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -50px;
}
</style>
<!--html -->
<body>
<div class="box" >

</div>
</body>
4.水平垂直居中(二)定位和margin:auto;
這個(gè)方法也很實(shí)用,不用受到寬高的限制,也很好用
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
background:#e9dfc7;
border:1px solid red;
position: relative;
}
img{
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
<!--html -->
<body>
<div class="box" >

</div>
</body>
5.水平垂直居中(三)絕對(duì)定位和transfrom
這個(gè)方法比較高級(jí)了,用到了形變,據(jù)我所知很多大神喜歡使用這個(gè)方法進(jìn)行定位,逼格很高的,學(xué)會(huì)后面試一定要用!這個(gè)是不需要知道居中元素的寬高就可以使用的,代碼里的圖片稍微有點(diǎn)大,改改寬高,僅此而已,在面試中大部分人會(huì)問如果不知道寬高該如何居中,答這個(gè),加分!對(duì)transform不了解的同學(xué)可以查閱一下資料了解一下!
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 300px;
height: 300px;
background:#e9dfc7;
border:1px solid red;
position: relative;
}
img{
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
<!--html -->
<body>
<div class="box" >

</div>
</body>
6.水平垂直居中(四)diplay:table-cell
其實(shí)這個(gè)就是把其變成表格樣式,再利用表格的樣式來進(jìn)行居中,很方便
<style>
.box{
width: 300px;
height: 300px;
background:#e9dfc7;
border:1px solid red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
img{
width: 100px;
height: 150px;
/*margin: 0 auto;*/ 這個(gè)也行
}
</style>
<!--html -->
<body>
<div class="box" >

</div>
</body>
7.水平垂直居中(五)flexBox居中
這個(gè)用了C3新特性flex,非常方便快捷,在移動(dòng)端使用完美,pc端有兼容性問題,以后會(huì)成為主流的
<style>
.box{
width: 300px;
height: 300px;
background:#e9dfc7;
border:1px solid red;
display: flex;
justify-content: center;
align-items:center;
}
img{
width: 150px;
height: 100px;
}
</style>
<!--html -->
<body>
<div class="box" >

</div>
</body>
8.水平垂直居中(六)利用vertical-align:middle;
這方法不常見,但是一位朋友補(bǔ)充后我覺得也不失為一種好方法可以讓別人刮目相看,這個(gè)方法關(guān)鍵要有一個(gè)和容器一樣高的元素作為居中的一個(gè)參照就像b元素一樣
<style>
.wrap{
width:300px;
height:300px;
background:rgba(0,0,0,0.5);
text-align:center;
font-size:0;
}
.vamb{
display:inline-block;
width:0px;
height:100%;
vertical-align:middle;
}
.test{
display:inline-block;
vertical-align:middle;
font-size:16px;
text-align:left;
background:red;
}
</style>
<body>
<div class="wrap">
<b class="vamb"></b>
<div class="test">
寬高不定<br>
垂直水平居中
</div>
</div>
</body>
常見又實(shí)用的例子就先寫到這,歡迎提意見,謝謝大家!喜歡請(qǐng)關(guān)注點(diǎn)個(gè)贊,也是對(duì)我的支持和鼓勵(lì)!
作者:coderLfy鏈接:https://www.jianshu.com/p/a7552ce07c88
直居中-父元素高度確定的單行文本
父元素高度確定的單行文本的豎直居中的方法是通過設(shè)置父元素的height和line-height高度一致來實(shí)現(xiàn)的。如下代碼:
<div class="container">
hi,imooc!
</div>
css代碼:
<style>
.container{
height:100px;
line-height:100px; /* 僅能用于單行文本 */
background:#999;
}
</style>
垂直居中-圖片以及行內(nèi)塊元素
<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)標(biāo)簽, 同時(shí)設(shè)置vertical-align:middle。
說到豎直居中,css中有一個(gè)用于豎直居中的屬性vertical-align,但這個(gè)樣式只有在父元素為td 或th時(shí),才會(huì)生效。所以又要插入table標(biāo)簽了。
下面看一下例子:
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}
因?yàn)閠d標(biāo)簽?zāi)J(rèn)情況下就默認(rèn)設(shè)置了vertical-align為middle, 所以我們不需要顯式地設(shè)置了。
垂直居中-父元素高度確定的多行文本(方法二)
在chrome、firefox及IE8以上的瀏覽器下可以設(shè)置塊級(jí)元素的display為table-cell, 激活vertical-align屬性, 但注意IE6、7并不支持這個(gè)樣式。
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>
這種方法的好處是不用添加多余的無意義的標(biāo)簽,但缺點(diǎn)也很明顯,它的兼容性不是很好,不兼容 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>
實(shí)例1:將內(nèi)層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>
<!--下面是代碼任務(wù)區(qū)-->
</body>
</html>
實(shí)例2:將內(nèi)層垂直居中、外層水平居中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標(biāo)題文檔</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">
鋤禾日當(dāng)午,<br>
汗滴禾下土。<br>
誰知盤中餐,<br>
粒粒皆辛苦。<br>
</div>
</div>
</body>
</html>
實(shí)例3: 使用絕對(duì)定位垂直居中
<!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>
<!--
行內(nèi)元素(文本)->水平垂直居中
text-align: center;
line-height: height;
-->
<!--
塊元素->水平垂直居中
margin: 0 auto;
-->
<div></div>
</body>
</html>
實(shí)例4: 使用絕對(duì)定位垂直居中
<!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;
/* 下面這種寫法也可以讓一個(gè)盒子居中 */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div>您確定刪除:重慶萬州公交墜江事件結(jié)果公布后,司乘糾紛和公交駕駛安全問題成為人們熱議的焦點(diǎn),如何預(yù)防和避免惡性結(jié)果的發(fā)生,才是問題的關(guān)鍵。“鼓勵(lì)市民舉報(bào),并對(duì)勇于制止干擾公交車正常行駛違法行為的公民予以獎(jiǎng)勵(lì)。”昨日下午,西安市公安局公共交通分局召開媒體通氣會(huì),通報(bào)西安相關(guān)安全舉措。這條消息嗎</div>
</body>
</html>
絕對(duì)定位(固定定位)之后, 所有標(biāo)準(zhǔn)流的規(guī)則, 都不適用了。所以margin:0 auto; 失效。
解決辦法:left:50%; margin-left:負(fù)的寬度的一半。(三句話)
div{
width: 600px;
height: 60px;
position: absolute; /* → 第一句 */
left: 50%; // /* → 第二句 */
top: 0;
margin-left: -300px; /*→ 第三句。寬度的一半*/
}
實(shí)例4:使用絕對(duì)定位和margin:auto垂直居中
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。