css中不確定盒子寬高上下左右居中的八種方法小結(jié)
發(fā)布時(shí)間:2023-08-23 16:39:17 作者:Forestご
我要評(píng)論
本文主要介紹了css中不確定盒子寬高上下左右居中的八種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
第一種:利用絕對(duì)定位和margin:auto實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body{
margin: 0;
}
.box{
height: 100vh;
background-color: hotpink;
position: relative;
}
.img{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
</style>第二種:利用css3的transform屬性實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
height: 100vh;
background-color: hotpink;
position: relative;
}
.img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>第三種:利用flex布局實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
height: 100vh;
background-color: hotpink;
display: flex;
/* 上下居中 */
align-items: center;
/* 左右居中 但是圖片高度會(huì)撐滿 */
justify-content: center;
}
</style>第四種:利用grid布局實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
height: 100vh;
background-color: hotpink;
display: grid;
}
.img {
display: inline-block;
/* 上下居中 */
align-self: center;
/* 左右中 */
justify-self: center;
}
</style>第五種:利用display: -webkit-box實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
height: 100vh;
background-color: hotpink;
display: -webkit-box;
/* 上下居中 */
-webkit-box-align: center;
/* 左右居中 */
-webkit-box-pack: center;
}
</style>第六種:利用display: flex和margin: auto實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
width: 100vw;
height: 100vh;
background-color: hotpink;
display: flex;
}
.img {
margin: auto;
}
</style>第七種:利用table-cell實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
.box {
/* 要有寬高 */
width: 100vw;
height: 100vh;
background-color: hotpink;
display: table-cell;
/* 左右居中 */
text-align: center;
/* 上下居中 */
vertical-align: middle;
}
.img {
/* 不加也可以 */
display: inline-block;
}
</style>第八種:利用display: grid和place-items: center實(shí)現(xiàn)
html:
<div class="box">
<img src="./img/77.jpeg" alt="" class="img">
</div>css:
<style>
body {
margin: 0;
}
div {
height: 100vh;
background-color: hotpink;
display: grid;
/* 是同時(shí)設(shè)置 align-items 和 justify-items 的快速方法。通過將其設(shè)置為 center , align-items 和 justify-items 都將設(shè)置為 center。 */
place-items: center;
}
/* .img {
沒有固定的寬高
} */
</style>到此這篇關(guān)于css中不確定盒子寬高上下左右居中的八種方法小結(jié)的文章就介紹到這了,更多相關(guān)css盒子上下左右居中內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
本文主要介紹了CSS子盒子水平和垂直居中的五種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)2022-07-19css 利用 position + margin 實(shí)現(xiàn)固定盒子橫向縱向居中的方法
這篇文章主要介紹了css 利用 position + margin 實(shí)現(xiàn)固定盒子橫向縱向居中的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要2020-12-23
使用CSS實(shí)現(xiàn)盒子水平垂直居中的方法(8種)
這篇文章主要介紹了使用CSS實(shí)現(xiàn)盒子水平垂直居中的方法(8種),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來2020-11-11
這篇文章主要介紹了CSS盒子居中的常用的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)2020-03-31


