/* HTML */
<div class='father'>
<div class='son'></div>
</div>
<style>
.father {
display: table-cell;
vertical-align: middle;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
width: 50px;
height: 50px;
background-color: aqua;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
background-color: aqua;
width: 50px;
height: 50px;
top: 0;
bottom: 0;
margin: auto;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
/* 负margin须是高度的一半 */
margin-top: -50px;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
/* 注意"-"两边要隔开 减去的须是高度的一半*/
top: calc(50% - 50px);
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
transform: translateY(-50%);
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
line-height: 300px;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
display: inline-block;
vertical-align: middle;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: flex;
align-items: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: grid;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
align-self: center;
}
</style>
复制代码
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: block;
}
.father::after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.son {
background-color: aqua;
width: 50px;
height: 50px;
display: inline-block;
vertical-align: middle;
}
</style>
复制代码
<!-- HTML -->
<div class="father">
<div class="hide"></div>
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
background-color: aqua;
width: 50%;
height: 50%;
}
.hide {
width: 50px;
height: 25%;
}
</style>
复制代码
<!-- HTML -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
writing-mode: vertical-lr;
text-align: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
writing-mode: horizontal-tb;
display: inline-block;
}
</style>
复制代码
作者:soloplayer
链接:https://juejin.cn/post/6904138129612439560
来源:掘金
文翻译自 How to Center a Div Using CSS Grid,作者:Fimber Elemuwa, Ralph Mason。 略有删改
在本文中,我们将介绍使用CSS Grid在水平和垂直方向上居中div的五种方法,当然这些技术可用于任何类型的元素。
我们首先创建一个容器,其中包含一个简单的div元素,我们将使用它来演示这些居中方法。下面是HTML:
<article>
<div></div>
</article>
下面是我们的初始CSS:
article {
width: 100%;
min-height: 100vh;
background: black;
display: grid;
}
div {
width: 200px;
background: yellow;
height: 100px;
}
在下面所有的例子中,我们将使用display: grid属性。这将<article>元素建立为网格容器,并为该容器生成块级网格。我们已经将网格容器设置为宽(width: 100%)和高(min-height: 100vw),这样我们的div就有足够的空间在其中移动。
接下来让我们来看看将div居中的各种方法。
place-self属性提供了一种简单的方法来水平和垂直居中网格项。它用于将网格项置于其网格单元格的中心。
将div居中就像这样简单:
article {
display: grid;
}
div {
place-self: center;
}
place-self属性是justify-self(水平)和align-self(垂直)属性的简写。
使用place-self对于在网格内居中单个项目特别有用,因为它使其他网格项目可以自由地以不同的方式定位。但这并不是使用Grid使元素居中的唯一方法,继续看看其他的一些方法。
place-items属性是justify-items(水平)和align-items(垂直)的简写。这些属性应用于网格容器而不是每个网格项,当我们希望所有网格项具有相同的位置时,这些属性非常有用。
将以下CSS代码添加到父容器:
article {
display: grid;
place-items: center;
}
我们可以基于初始代码添加更多的div元素,看看会发生什么。结果是每个div将在其网格单元格内水平和垂直居中,如下图所示(通过浏览器的网格检查器)。
place-content属性是justify-content(水平)和align-content(垂直)的简写。虽然place-self和place-items控制网格项如何放置在其指定的网格单元格中,但place-content指定网格容器的整个内容应如何对齐(即,所有网格项被视为一个组)。在我们的演示中,只有一个网格项(我们的单个黄色div),因此我们也可以使用place-content将其置于其容器的中心。
将以下CSS代码添加到父容器:
article {
display: grid;
place-content: center;
}
这里有几点需要注意。到目前为止,在所有的例子中我们都使用了center的值。但是到目前为止,我们已经探索的每个属性都有各种其他的放置物品的值。place-content有很多值,另外两个值也可以用于居中我们的div:space-around和space-evenly。
此外,在我们的简单例子中,一个div在容器中居中,我们甚至可以混合和匹配我们上面看到的属性。我们可以使用justify-content和align-items来居中div,有兴趣的可以尝试看看。
像往常一样,我们将使用display: grid来定位父容器。我们还将使用margin: auto为div指定自动边距。这使浏览器自动计算div周围的可用空间,并在其网格单元格内垂直和水平划分,将div放置在中间:
article {
display: grid;
}
div {
margin: auto;
}
最后一个方法我们将深入探讨Grid布局的强大功能,因为我们将研究两种方法来将div居中放置在具有多行和多列的网格中。
以下是我们的基本CSS:
article {
display: grid;
grid-template-columns: 1fr 200px 1fr;
grid-template-rows: 1fr 100px 1fr;
}
div {
background: yellow;
grid-column: 2;
grid-row: 2;
}
我们显式地布局了一个网格,中间有一个区域来放置我们的div。我们现在甚至不需要在div上设置尺寸,因为网格轨迹会处理这个问题。我们在网格的中间指定一个网格单元格,其宽度为200px,高度为100px,然后我们告诉div从第二条网格线和第二条行线开始。(默认情况下,它将仅跨到每个方向上的下一条轴网线。)div元素被很好地放置在其容器的中心,如下所示。
下图显示了位于其网格轨迹内的div。
网格布局提供了各种不同的方法来实现这一结果。最后我们做与上面相同的事情,但这次为我们的div使用一个命名区域:
article {
display: grid;
grid-template-columns: 1fr 200px 1fr;
grid-template-rows: 1fr 100px 1fr;
grid-template-areas: ". . ."
". box ."
". . .";
}
div {
background: yellow;
grid-area: box;
}
在这里,我们设置一个名为grid-area的box,然后描述它应该位于网格上的什么位置,用一个简单的点(.)指定哪些网格单元格是空的。
这种布局方法的优点是,它可以很容易地将许多其他元素放置在我们想要的任何地方,这就是网格布局的强大之处。
这些方法中的每一个都允许我们在容器中水平和垂直地居中一个div。place-self和margin: auto选项很好,因为它们直接应用于居中的元素,而不是其容器。但是本文中介绍的所有方法都是高效的,并且可以很好地完成这项工作。在各种场景中,我们可能希望将元素置于中心,因此拥有一系列工具来实现该目标非常重要。
在演示示例中,我们只是使用了一个空的div,但是当然我们可以向div添加内容,居中仍然有效。而且这些居中技术同样适用于div以外的元素。
有兴趣的可以看看原文,可以在线体验不同颜色格式是如何工作的。看完本文如果觉得有用,记得点个赞支持,收藏起来说不定哪天就用上啦~
专注前端开发,分享前端相关技术干货,公众号:南城大前端(ID: nanchengfe)
习css大家是不是对元素居中的知识点很是模糊?是不是苦于找不到一个总结的通俗易懂的说明?是不是自己懒得去总结?今天小编在前端的学习与实践中总结出的元素的五大居中方式,黏贴了代码并对代码做了解释,希望对迷茫的有所帮助!
下面的居中示例中,统一使用了同一个div作为父元素和p作为子元素
设置一个div,并且设置了div的宽高边框,div里面设置一个块元素p,设置了它的宽高和背景色
css居中方式1
这里利用了伪元素让子元素p在div盒子里左右水平居中只需要在它的父元素div里加text-align:center;垂直方向居中需要在父元素后面加了一个伪元素,并使得样式为inline-block;height:100%;就是和父元素一样高,vertical-align:middle;垂直居中,也就是p元素相对与伪元素居中,由于伪元素和div一样高,所以相当于p元素在div里垂直居中。
css居中方式2
这里利用了定位居中
子元素p设置position:absolute脱离文档流,默认以html作为父元素,所以我们给父元素div设置position:relative;使得p以div为父元素做位置的变动,left:0;tight:0;top:0;bottom:0;(只有设置了定位的元素才可以使用这种方式来移动),最后margin:auto;就会水平和垂直都居中。
css居中方式3
这里利用了弹性盒居中
父元素div设置成弹性盒样式,justify-content:center;主轴居中
align-items:center;垂直居中(而且这两个只能设置在父元素上,弹性盒知识)
css居中方式4
利用定位线左上角居中,然后左移子元素宽度的一半,再上移子元素高度的一半。
css居中方式5
利用动画移动属性transform
小编是一个有着5年工作经验的架构师,关于web前端,自己有做材料的整合,一个完整学习web前端的路线,学习材料和工具。需要的伙伴可以私信我,发送“前端”等3秒后就可以获取领取地址,免费送给大家。对于学习web前端有任何问题(学习方法,学习效率,如何就业)都可以问我。希望你也能凭自己的努力,成为下一个优秀的程序员!
相信看了上面的有关Html5、css的元素五大居中方式,你们就可以解决自己的小问题了,但是也要养成一个总结的好习惯。好记性不如烂笔头!以前留下来的话语总是有他的道理。Comeon!
*请认真填写需求信息,我们会在24小时内与您取得联系。