[CSS]中子元素在父元素中居中

元素居中

对于当行文字居中,比较简单,设置text-align:center和text-height为盒子高度即可

对于父元素中子元素居中,要实现的话有以下几个方法

方法1:利用定位+margin:auto

<style>
  .father {
    width: 500px;
    height: 300px;
    border: 1px solid #0a3b98;
    position: relative;
  }
​
  .son {
    width: 100px;
    height: 40px;
    background: #f0a238;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin:auto;
  }
</style>
<div class="father">
  <div class="son"></div>
</div>

父元素中relative相对定位,子元素absolute绝对定位,并设置top,left,right,bottom为0,并设置margin:auto

方法2:利用定位+margin:负值

<style>
 .father {
 position: relative;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 position: absolute;
 top: 50%;
 left: 50%;
 margin-left:-50px;
 margin-top:-50px;
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>

父元素中relative相对定位,子元素absolute绝对定位,并设置top,left为50%,并设置margin-left和margin-top为盒子大小的一半-50px(这种情况需要知道盒子大小)

方法3:利用定位+transform

<style>
 .father {
 position: relative;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%,-50%);
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>

父元素中relative相对定位,子元素absolute绝对定位,并设置top,left为50%,并使用transform移动-50%,transform: translate(-50%,-50%); 此方法不需要知道盒子大小

方法4:利用flex

<style>
 .father {
 display: flex;
 justify-content: center;
 align-items: center;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>

使用flex布局,设置justify-content: center; 水平居中 align-items: center;设置垂直居中

相关推荐

  1. [CSS]中子元素元素居中

    2024-03-30 14:08:04       45 阅读
  2. css设置子元素元素水平垂直居中

    2024-03-30 14:08:04       35 阅读
  3. 【前端】查找元素元素的索引

    2024-03-30 14:08:04       36 阅读
  4. CSS的水平垂直居中元素的多种方式

    2024-03-30 14:08:04       67 阅读
  5. css垂直水平居中(元素宽高有无情况)+ flex用法

    2024-03-30 14:08:04       45 阅读
  6. CSSCSS元素的垂直居中案例

    2024-03-30 14:08:04       34 阅读
  7. CSS如何寻找第一个元素

    2024-03-30 14:08:04       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 14:08:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 14:08:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 14:08:04       82 阅读
  4. Python语言-面向对象

    2024-03-30 14:08:04       91 阅读

热门阅读

  1. Flink 反压问题处理

    2024-03-30 14:08:04       38 阅读
  2. 扫地机器人

    2024-03-30 14:08:04       45 阅读
  3. Elasticsearch rollover API

    2024-03-30 14:08:04       38 阅读
  4. docker centos7离线安装ElasticSearch单机版

    2024-03-30 14:08:04       40 阅读
  5. R语言数据分析基础(一)

    2024-03-30 14:08:04       42 阅读
  6. 【React】React表单组件

    2024-03-30 14:08:04       39 阅读
  7. 【C语言】程序翻译过程

    2024-03-30 14:08:04       43 阅读
  8. sqlmap基础知识

    2024-03-30 14:08:04       36 阅读
  9. Go的数据结构与实现【LinkedList】

    2024-03-30 14:08:04       42 阅读
  10. 手写DNS服务器测速程序(工具分享)

    2024-03-30 14:08:04       57 阅读
  11. test6

    test6

    2024-03-30 14:08:04      46 阅读
  12. 成都某公司笔试题sql

    2024-03-30 14:08:04       46 阅读