zoukankan      html  css  js  c++  java
  • CSS 水平居中与垂直居中

    前言

    在CSS布局中,水平居中与垂直居中一直是用到比较多的,在本篇中将介绍水平居中、垂直居中的几种方式。

    示例

    HTML

    <div class="parent">
        <div class="child"></div>
    </div>
    

      

    CSS

    .parent {
         200px;
        height: 100px;
        position: relative;
        background-color: #374858;
    }
    
    .parent .child {
         100px;
        height: 50px;
        background-color: #9dc3e6;
    }
    

      

    效果

    1. 水平居中

    这里将分别介绍当子元素的样式为内联块级以及绝对定位时的水平居中布局方案。

    1.1 子元素为内联样式

    说明:当子元素为内联样式时(display: inline-block | inline-flex | inline-grid | inline-table 也含有内联样式特性),只需要设置父元素的text-align: center。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        text-align: center;
    }
    .parent .child {
        display: inline-block;
    }
    

    1.2 子元素为块级样式

    说明:父元素和子元素都是块级元素时,设置子元素的margin: 0 auto即可。

    注意:此时的子元素需要设置width。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {}
    
    .parent .child {
        display: block;
        margin: 0 auto;
    }
    

    1.3 子元素 position: absolute

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {}
    
    .parent .child {
        position: absolute;
        left: 50%;
        transform: translate(-50%, 0);
    }
    

    1.4 父元素 display: flex

    说明:此时子元素可为内联、块级元素。

    浏览器支持:IE 11。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        display: flex;
        justify-content: center;
    }
    
    .child {
        display: inline;
        margin: 0 auto;
    }
    

      

    2. 垂直居中

    同水平居中一样,这里也将分别介绍当子元素的样式为内联块级以及绝对定位时的垂直居中布局方案。

    2.1 子元素为块级元素

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        display: table-cell;
        vertical-align: middle;
    }
    
    .parent .child {
        display: block;
    }
    

    2.2 子元素 position: absolute

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {}
    
    .parent .child {
        position: absolute;
        top: 50%;
        transform: translate(0, -50%);
    }
    

    2.3 父元素 display: flex

    说明:此时子元素可为内联、块级元素

    浏览器支持:IE 11。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        display: flex;
        align-items: center;
    }
    
    .parent .child {
        display: inline;
    }
    

    3. 水平居中+垂直居中

    3.1 子元素 display: inline-block

    说明:设置子元素的display: inline-block。

    注意:此时的子元素需要设置width和height。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        text-align: center;
        display: table-cell;
        vertical-align: middle;
    }
    
    .parent .child {
        display: inline-block;
    }
    

    3.2 子元素 position: absolute

    说明:此时的子元素为绝对定位。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        position: relative;
    }
    
    .parent .child {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }
    

    3.3 父元素 display: flex

    浏览器支持IE 11。

    CSS

    .parent { 200px;height: 100px;position: relative;background-color: #374858;}
    .parent .child { 100px;height: 50px;background-color: #9dc3e6;}
    
    .parent {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .parent .child {}
    
  • 相关阅读:
    php内存管理机制、垃圾回收机制
    Redis 3.2.1集群搭建
    centos开启IPV6配置方法
    /etc/hosts.allow和/etc/hosts.deny详解
    3元购买微信小程序解决方案一个月
    linux下使用ntfs-3g挂载NTFS出错
    腾讯云微信小程序域名变更指南
    nginx开启gzip压缩
    centos 7使用yum安装docker容器
    linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
  • 原文地址:https://www.cnblogs.com/polk6/p/css-center.html
Copyright © 2011-2022 走看看