簡介
div 的居中對齊是前端開發(fā)最重要的方面之一。在本文中,我們將了解使用 HTML 和 CSS 將一個 div 置于另一個 div 中的技術(shù)。
在本教程中,我們將有一個父 div,它應(yīng)具有子 div。我們的任務(wù)是將子 div 放置在父 div 的中心。
使用 Transform 翻譯和位置語法
這不是一種非常流行的將一個 div 居中對齊到另一個 div 中的方法
語法
left:50%; top:50%; Transform: translate(-50%, -50%);
登錄后復(fù)制
上面的語法執(zhí)行以下操作 –
CSS 規(guī)則“l(fā)eft:50%;”將元素的水平位置設(shè)置為其容器左側(cè)的 50%。
規(guī)則“top:50%;”將元素的垂直位置設(shè)置為距其容器頂部的 50%。
規(guī)則“transform:translate(-50%, -50%);”將元素水平移動 -50%,垂直移動 -50%,有效地將元素的中心定位在距離其容器左側(cè)和頂部 50% 的位置。
然而,這并不是將一個 div 置于另一個 div 中心的流行方法。這是因為以下原因 –
這需要額外的五行代碼,比其他方法多。
必須定義父級和子級 div 的位置,這可能會給以后設(shè)計其他關(guān)聯(lián) div 帶來不便。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container{
background-color: red;
width:50vw;
height:50vh;
position: relative;
}
.child{
background-color: yellow;
Width: 25vw;
Height: 25vh;
position: absolute;
left:50%;
top:50%;
transform:translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="container">
<div class="child">
</div>
</div>
</body>
</html>
登錄后復(fù)制
說明
在上面的示例中,我們首先聲明子級的位置是絕對的,父級的位置是相對的。接下來,我們將子級從父級 div 的頂部和左側(cè)移動了 50%。接下來,我們使用CSS的transform屬性使子div居中。
translate(x, y) 函數(shù)采用兩個值作為參數(shù),其中 x 是水平移動元素的像素數(shù),y 是垂直移動元素的像素數(shù)。在本例中,元素將移動其寬度的 -50% 和高度的 -50%,使其垂直和水平居中。
使用網(wǎng)格屬性
將 div 居中對齊的更流行的方法是使用 CSS 的 grid 屬性;然而,對于這個,首先需要將 div 聲明為網(wǎng)格。
語法
place-items: center;
登錄后復(fù)制
place-items 屬性將項目與網(wǎng)格容器水平和垂直對齊。我們只能將該屬性與網(wǎng)格容器一起使用。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container{
background-color: blue;
width:50vw;
height:50vh;
display: grid;
place-items: center;
}
.child{
background-color: yellow;
width:25vw;
height:25vh;
}
</style>
</head>
<body>
<div class="container">
<div class="child">
</div>
</div>
</body>
</html>
登錄后復(fù)制
使用 Flex Box 屬性
我們可以使用的另一種方法是CSS的flexbox屬性。我們首先需要將父級聲明為彈性盒。 Flex box 是 CSS 中廣泛使用的元素。它們使用起來非常方便,因為它們是響應(yīng)式元素,并且程序員通常可以很好地控制 Flexbox 屬性。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container{
background-color: purple;
width:50vw;
height:30vh;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.child{
background-color: green;
width:25vw;
height:10vh;
}
</style>
</head>
<body>
<div class="container">
<div class="child">
</div>
</div>
</body>
</html>
登錄后復(fù)制
將多重嵌套 div 放在中心
將多個嵌套 div 放入父 div 中也是一項簡單的任務(wù)。假設(shè)有三個div,分別是container,是父div,first-child,是容器的子div;第二個孩子是第一個孩子的孩子。然后我們可以首先使用我們討論的方法將第一個子元素居中對齊到容器 div 中。接下來,我們可以將第一個孩子作為第二個孩子的父母并應(yīng)用相同的技術(shù)。
作為說明,我們將使用其中一種方法來展示該技術(shù)。讀者應(yīng)該嘗試使用其他兩種方法來執(zhí)行類似的任務(wù)。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
background-color: red;
width: 50vw;
height: 30vh;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.first-child {
background-color: green;
width: 25vw;
height: 20vh;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.second-child {
background-color: yellow;
height: 10vh;
width: 20vw;
}
</style>
</head>
<body>
<div class="container">
<div class="first-child">
<div class="second-child"></div>
</div>
</div>
</body>
</html>
登錄后復(fù)制
結(jié)論
在本文中,我們了解了如何使用 HTML 和 CSS 將其他 div 內(nèi)的 div 居中對齊。我們了解了三種不同的 div 居中對齊技術(shù)。它們使用position屬性、grid屬性和flexbox屬性。其中,flexbox屬性使用最廣泛,也最方便。
以上就是如何將一個div居中在另一個div中?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!






