CSS实现垂直居中的几种方法


1. table-cell

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: #ff44ff;
            display: table-cell;
            vertical-align: middle;
            text-align: center;   
        }
    style>
head>
<body>
    <div class="box">
       <div>
            <span>垂直居中span>
       div>
    div>
body>
html>

2. flex布局 (弹性布局)

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: #ff44ff;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
    style>
head>
<body>
    <div class="box">
        <div>
            <span>垂直居中span>
        div>
    div>
body>
html>

3. 使用position和transform

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: #ff44ff;
            position: relative;
        }
        .box div span{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            -webkit-transform: translate(-50%,-50%);
            -ms-transform: translate(-50%,-50%);
        }
    style>
head>
<body>
    <div class="box">
        <div>
            <span>垂直居中span>
        div>
    div>
body>
html>

4. margin: auto;

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: #ff44ff;
            margin: auto;
        }
    style>
head>
<body>
    <div class="box">
        <div>
            <span>垂直居中span>
        div>
    div>
body>
html>

5. 绝对定位

DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        .box div span {
            width: 50%;
            height: 50%;
            background: #f4f4f4;
            overflow: auto;
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
        }
    style>
head>

<body>
    <div class="box">
        <div>
            <span>垂直居中span>
        div>
    div>
body>
html>
CSS