如何将一个 div 居中放置在另一个 div 内?
简介
div 居中对齐是前端开发中最重要的方面之一。在本文中,我们将学习使用 HTML 和 CSS 将一个 div 居中放置在另一个 div 内的技巧。
在本教程中,我们将使用一个父 div,其中包含一个子 div。我们的任务是将子 div 放置在父 div 的中间。
使用 Transform、Translate 和 Position 语法
将一个 div 居中放置在另一个 div 内并不是一种非常流行的方法
语法
left:50%; top:50%; Transform:translate(-50%, -50%);
上述语法的作用如下:
CSS 规则"left:50%;"将元素的水平位置设置为距离其容器左侧 50%。
规则"top:50%;"将元素的垂直位置设置为距离其容器顶部 50%。
规则"transform:translate(-50%, -50%);"将元素水平移动 -50%,垂直移动 -50%,从而有效地将元素的中心定位在其容器左侧和顶部 50% 的位置。
然而,这并不是将一个 div 置于另一个 div 中心的常用方法。这是因为以下原因:
这需要额外的五行代码,比其他方法要多。
必须定义父 div 和子 div 的位置,这可能会给以后设计其他关联 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>
说明
在上面的例子中,我们首先将子元素的位置声明为绝对位置,将父元素的位置声明为相对位置。接下来,我们将子元素从父元素 div 的顶部和左侧移动了 50%。接下来,我们使用 CSS 的 transform 属性将子元素移到中心。
translate(x, y) 函数接受两个值作为参数,其中 x 是元素水平移动的像素数,y 是元素垂直移动的像素数。在本例中,元素宽度和高度分别移动了 50%,使其垂直和水平居中。
使用 Grid 属性
一种更流行的将 div 居中对齐的方法是使用 CSS 的 grid 属性;但是,要实现这一点,首先需要将 div 声明为网格。
语法
place-items: center;
place-items 属性用于将项目与网格容器水平和垂直对齐。我们只能将该属性用于网格容器。
示例
<!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>
使用 Flex Box 属性
我们可以使用另一种方法是 CSS 的 flexbox 属性。首先,我们需要将父元素声明为 flexbox。Flex Box 是 CSS 中广泛使用的元素。由于它们是响应式元素,并且程序员通常可以很好地控制 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>
将多个嵌套 div 置于中心
将多个嵌套 div 放置在父 div 内也很容易。假设有三个 div,分别是容器(父 div)、first-child(容器的子 div)以及 second-child(第一个子 div 的子 div)。那么,我们可以先使用之前讨论的方法将 first-child 居中对齐到容器 div 中。接下来,我们可以将 first-child 作为第二个子 div 的父 div,并应用相同的技术。
为了便于说明,我们仅使用其中一种方法来演示该技术。读者可以尝试使用其他两种方法来完成类似的任务。
示例
<!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>
总结
在本文中,我们了解了如何使用 HTML 和 CSS 将 div 居中放置在其他 div 中。我们还了解了三种不同的 div 居中技巧。它们分别使用了 Position 属性、Grid 属性和 Flexbox 属性。其中,Flexbox 属性是最广泛使用且最便捷的。
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

