放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 前端

    • 前端 概览
    • 前端汇总

    • front 博文

    • front 项目总结

    • front 高级

    • front tools

  • vue

    • vue 概览
    • vue 汇总

    • vue 博文

    • vue 项目总结

    • vue 高级

  • html

    • html 概览
    • html 汇总

    • html 博文

  • css

    • css 概览
    • css 汇总

    • css 博文

      • css 隔离
      • css 选择器
      • css 布局
      • css 盒子
      • css 文档流
      • css BFC
      • css 回流重绘
      • css 居中
        • 水平居中
          • 行内元素水平居中
          • 块级元素水平定宽居中
          • 块级元素水平不定宽居中
        • 垂直居中
          • 行内元素垂直居中
          • 块级元素定宽-垂直居中
          • 块级元素不定宽-垂直居中
        • 水平垂直居中
          • 块级元素定宽-水平垂直居中
          • 块级元素不定宽-水平垂直居中
      • css margin合并和塌陷
      • css hack
      • css 行内和块级元素
      • css 单位
      • css 动画
      • css 硬件加速
      • 伪类/伪元素
      • flex布局
      • grid布局
      • table布局
      • 雪碧图/精灵图
    • sass

    • less

  • js

    • javascript 概览
    • JS 汇总

    • ES6

    • JS 博文

    • JS 工具

  • node

    • node 概览
    • node 汇总

    • node 框架

    • node 博文

  • react

    • react 概览
    • react 汇总

    • react 博文

    • react 高级

  • 微信小程序

    • 微信小程序 概览
    • 微信小程序总结
    • 微信小程序文章
    • 微信小程序 博文

    • 微信小程序 高级

  • 微信公众号

    • 微信公众号 概览
    • 微信公众号总结
    • 微信公众号文章
  • 多端开发

    • 多端开发
    • dsbridge 概览
    • jsbridge 概览
    • webview
    • uniapp

      • uniapp 概览
    • taro

      • taro 概览
    • flutter

      • flutter 概览
      • flutter 环境搭建
    • electron

      • electron 概览
  • front
放肆青春
2021-03-12

css 居中

图片垂直居中:text-align:center;

文字垂直居中:外层 div 设置行高 line-height: 36px;

# 水平居中

# 行内元素水平居中

text-align: center 可以实现在块级元素内部的行内元素水平居中。

此方法对行内元素(inline),行内块(inline-block),行内表(inline-table),inline-flex 元素水平居中都有效。

# 块级元素水平定宽居中

    1. margin: 0 auto;
    1. absolute+ 负 maigin
position: absolute;
width: 100px;
left: 50%;
margin-left: -50px;
1
2
3
4

# 块级元素水平不定宽居中

    1. flex 布局

justify-content: center

    1. absolute+transform
position: absolute;
left: 50%;
transform: translateX(-50%);
1
2
3
    1. table + margin

display: table; margin: 0 auto;

# 垂直居中

# 行内元素垂直居中

  • 单行行内元素

line-height 属性 设置的高度和父元素的高度相同即可

  • 多行行内元素

flex 布局

display: flex;
flex-direction: column;
justify-content: center;
1
2
3

# 块级元素定宽-垂直居中

    1. absolute + 负 margin
position: absolute;
height: 100px;
top: 50%;
margin-top: -50px;
1
2
3
4
    1. absolute + margin auto

# 块级元素不定宽-垂直居中

    1. flex
display: flex;
justify-content: center;
align-items: center;
1
2
3
    1. absolute + transform
position: absolute;
top: 50%;
transform: translateY(-50%);
1
2
3

# 水平垂直居中

# 块级元素定宽-水平垂直居中

    1. absolute+负 maigin
position: absolute;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
1
2
3
4
5
6
7
    1. absolute + margin: auto法
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
1
2
3
4
5
6

# 块级元素不定宽-水平垂直居中

    1. flex
display: flex;
justify-content: center;
align-items: center;
1
2
3
    1. absolute+transform
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
1
2
3
4
    1. table-cell
display: table-cell;
text-align: center;
vertical-align: middle;
1
2
3
    1. grid 网格
display: grid;
justify-self: center;
align-self: center;
1
2
3
更新时间: 12/24/2021, 4:08:09 PM
css 回流重绘
css margin合并和塌陷

← css 回流重绘 css margin合并和塌陷→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式