首页 > 其他分享 >8 种实现垂直和水平居中元素的方法汇总

8 种实现垂直和水平居中元素的方法汇总

时间:2022-09-30 14:00:09浏览次数:51  
标签:居中 50% center parent align 元素 汇总 child display

HTML:

<div class="parent" style="background: black; width: 200px; height: 200px">
<div class="child" style="background: red; width: 100px; height: 100px"></div>
</div>

 

8中居中方法:

1.

首先是基于父子容器大小的粗略 CSS 值:

<style>
  .parent {
    position: relative;
  }
.child {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -50px 0 0 -50px;
  }
</style>
一旦原始元素大小发生变化,CSS 就需要随之改变。这并不理想。所以下面介绍的方法不需要考虑父子元素的宽高,比较推荐。

2.

<style>
  .parent {
    position: relative;
  }
.child {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
</style>
这种方法与第一种方法的不同之处在于我们使用了transform而不是margin,这使得我们的CSS代码不再依赖于元素的尺寸。

3.

<style>
  .parent {
    position: relative;
  }
.child {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }
</style>
请记住,所有四个方向的值都必须为 0。

4.

<style>
  .parent {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
  }
.child {
    display: inline-block;
  }
</style>
5.

<style>
  .parent {
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
这种方法可能是目前使用最广泛的。

6.

<style>
  .parent {
    display: flex;
  }
.child {
    margin: auto;
  }
</style>
7.

<style>
  .parent {
    display: grid;
    /* The following 3 ways of writing are OK */
    /* 1 */
    /* justify-content: center;
    align-content: center; */
/* 2 */
    /* align-items: center;
    justify-items: center; */
/* 3 */
    place-content: center;
  }
</style>
8.

<style>
  .parent {
    display: grid;
  }
.child {
    align-self: center;
    justify-self: center;
  }
</style>

 

标签:居中,50%,center,parent,align,元素,汇总,child,display
From: https://www.cnblogs.com/bomdeyada/p/16744692.html

相关文章

  • <map>元素
    <map>元素<area>元素<map>元素的子元素设置图片的指定区域的锚链接区位<areashape=""coords=""href=""alt="">shape属性shape=rect:矩形shape=circle:圆形sh......
  • 【code基础】判断数组中每个元素前递增序列子序列的最大长度
    这是力扣刷题中很经典的一个套路,类似有:以下标i为标准,其之前最长的非递增序列的个数以下标i为标准,其之后的最长非递减序列的个数//以下标i为标准,其之前最长的非递增......
  • AM5728 Opencl 案例汇总:实现sobel算法,计算向量和,矩阵转置
    案例一:实现sobel算法OpenCV(Open Source Computer Vision Library)是一个基于BSD许可开源发行的跨平台计算机视觉库。实现图像处理和计算机视觉方面的很多通用计算。......
  • python如何依次打印出列表中的元素
    list1=[]forjinrange(1,100):list1.append(j)list2=[]sum1=int(input("输入一个数字:"))#定义一个int类型i=0while(i<=len(list1)):#对输入......
  • AC609实战问题汇总
    1.烧写文件格式:ALTERA的开发板在程序开发过程中,要把程序烧写到FPGA开发板上运行,上版调试,有三种文件格式sof,jic,pof。区别见:【FPGA学习笔记】sof文件和jic文件的区别,程序固......
  • 文字图片flex布局一行居中后要微调文字高度
    文字图片flex布局一行居中后要微调文字高度采用设置padding或者margin的方式不可行,会往外扩可以设置文字的line-height来调整文字和图片的相对高度......
  • JS Element.scrollIntoView() 滚动元素的父容器
    Element接口的scrollIntoView()方法会滚动元素的父容器,使被调用scrollIntoView()的元素对用户可见。文档语法element.scrollIntoView();//等同于element.scrollIntoView(......
  • 15. HTML-- 块级元素和内联元素
    1.前言HTML标签(元素)可以分为两个类别,分别是块级元素和内联元素(也叫行内元素)。2.块级元素块级元素最主要的特点是它们自己独占一行,块级元素中最具代表性的就是<div>,......
  • 18.getElementById(id)是javascript中访问某个元素的方法
    document.getElementById(id)是javascript中访问某个元素的方法。返回指定ID的元素getElementById()方法可返回对拥有指定ID的第一个对象的引用。HTMLDOM定义了多......
  • 21.如果元素的某个属性是变化的怎么办?
    第一种方法:将这个元素变化的部分用“*”代替第二种方法:使用UI分析器选择其他的方法,比如选用CSS方式选择,技巧:如果这种方式定位到了同级的其他元素(比如两个span的第一个,我们......