css常见居中方式

文章目录

  • 前言
  • 一、水平居中
    • [1、使用`text-align + inline-block`水平居中](#1、使用text-align + inline-block水平居中)
    • [2、使用定位`postion` + 变形`transform`水平居中](#2、使用定位postion + 变形transform水平居中)
    • [3、使用 `margin: 0 auto + display: block` 水平居中](#3、使用 margin: 0 auto + display: block 水平居中)
    • 4、使用`css3`新特性`flex`弹性盒子布局水平居中
  • 二、垂直居中
    • [1、固定高度(height) = 行高(line-height)](#1、固定高度(height) = 行高(line-height))
    • [2、使用定位(postion) + 变形(transform)垂直居中](#2、使用定位(postion) + 变形(transform)垂直居中)
    • [3、使用display: table-cell + vertical-align 垂直居中](#3、使用display: table-cell + vertical-align 垂直居中)
    • 4、使用`css3`新特性`flex`弹性盒子布局垂直居中

前言

页面布局基本都会有居中样式,包括水平居中、垂直居中等


一、水平居中

1、使用text-align + inline-block水平居中

html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用 text-align + inline-block 水平居中</title>
<style>
    .wrap{
        height: 200px;
        text-align: center;
        border: 1px solid red;
    }
    .wrap .text{
        height: 100px;
        width: 300px;
        display: inline-block;
        border: 1px solid blue; 
    }
</style>
</head>
<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
</body>
</html>

2、使用定位postion + 变形transform水平居中

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>用定位(postion) + 变形(transform)水平居中</title>
    <style>
        .wrap {
            height: 200px;
            border: 1px solid red;
            position: relative;
        }

        .wrap .text {
            height: 100px;
            width: 300px;
            border: 1px solid blue;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
</body>

</html>

3、使用 margin: 0 auto + display: block 水平居中

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>用 margin: 0 auto + display: block 水平居中</title>
    <style>
        .wrap {
            height: 200px;
            border: 1px solid red;
        }

        .wrap .text {
            height: 100px;
            width: 300px;
            display: block;
            border: 1px solid blue;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <span class="text">我是元素1</span>
    </div>
</body>

</html>

4、使用css3新特性flex弹性盒子布局水平居中

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>flex弹性盒子布局水平居中</title>
    <style>
       .wrap {
            height: 200px;
            border: 1px solid red;
            display: flex;
            justify-content: center;
        }
        .wrap .text {  
            border: 1px solid blue;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <span class="text">我是元素1</span>
    </div>
</body>

</html>

二、垂直居中

1、固定高度(height) = 行高(line-height)

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>固定高度(height) = 行高(line-height)</title>
    <style>
        .wrap {
            height: 300px;
            line-height: 300px;
        }

        .wrap .text {
            border: 1px solid blue;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
</body>

</html>

2、使用定位(postion) + 变形(transform)垂直居中

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用定位(postion) + 变形(transform)垂直居中</title>
    <style>
        .wrap {
            height: 600px;
            width: 300px;
            border: 1px solid red;
            position: relative;
        }

        .wrap .text {
            width: 300px;
            height: 100px;
            border: 1px solid blue;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }

        #div1 {
            width: 300px;
            height: 200px;
            background-color: #ccc;
            position: relative;
        }

        #div2 {
            width: 100px;
            height: 100px;
            background-color: #FB7299;
            margin: auto;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-top: -50px;
            margin-left: -50px;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
    <div id="div1">
        <div id="div2">
            子容器的垂直居中
        </div>
    </div>
</body>

</html>

3、使用display: table-cell + vertical-align 垂直居中

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>display: table-cell + vertical-align 垂直居中</title>
    <style>
        .wrap {
            height: 600px;
            width: 300px;
            border: 1px solid red;

            display: table-cell;
            vertical-align: middle;
        }

        .wrap .text {
            width: 300px;
            height: 100px;
            border: 1px solid blue;
        }

        #div1 {
            width: 300px;
            height: 200px;
            margin: 100px auto;
            /* 使容器水平边距自动相等 */
            border: 1px solid red;
            /* 给文本加上边框可以更清楚地看到效果 */
            display: table;
        }

        #div2 {
            display: table-cell;
            vertical-align: middle;
            text-align: center;
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
    <div id="div1">
        <div id="div2">
            多行文本的垂直居中
            多行文本的垂直居中
            多行文本的垂直居中
            多行文本的垂直居中
            多行文本的垂直居中
        </div>
    </div>
</body>

</html>

4、使用css3新特性flex弹性盒子布局垂直居中

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>flex弹性盒子布局垂直居中</title>
    <style>
        .wrap {
      height: 600px;
      width: 300px;
      border: 1px solid red;
      display: flex;
      flex-direction: column;
      justify-content: center;
  }
  .wrap .text {  
      width: 300px;
      height: 100px;
      border: 1px solid blue;
  }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="text">我是元素1</div>
    </div>
</body>

</html>
相关推荐
崔庆才丨静觅11 分钟前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅37 分钟前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment40 分钟前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅1 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊1 小时前
jwt介绍
前端
爱敲代码的小鱼1 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
Cobyte1 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
NEXT062 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
剪刀石头布啊2 小时前
生成随机数,Math.random的使用
前端
剪刀石头布啊2 小时前
css外边距重叠问题
前端