我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

    • css

      • 如何让chrome实现小于12px的字体大小
      • 如何使用css画出三角形,正方形
      • 如何使用CSS完成视差滚动效果
      • 说说单行和多行文本溢出的实现方式
      • 说说对Css预编语言的理解
      • 说说你对CSS盒子模型的理解
      • 说说设备像素、css像素、设备独立像素、dpr、ppi 之间的区别
      • 说说什么是响应式设计及实现方式
      • 说说什么是重绘和回流,如何优化
      • 说说元素水平垂直居中的方法有哪些
        • 是什么
        • 怎么用
        • 原理
      • 说说CSS3新增了哪些新特性
      • 说说css性能优化有哪些方式
      • 说说css中层叠上下文z-index的理解
      • 说说css中的选择器是优先级
      • 说说css中两栏布局和三栏布局有哪些
      • 说说css中哪些属性是继承和不继承的
      • 说说css中实现动画有哪些
      • 说说CSS中有哪些方式可以隐藏页面元素
      • 说说em-px-rem-vh-vw的区别
      • 说说flex盒子的理解及应用
      • 说说property和prototype的区别
      • 谈谈你对BFC的理解
    • ES6

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • css
fangdown
2021-08-20
目录

说说元素水平垂直居中的方法有哪些

# 是什么

让元素的内容(文字或者图片)在水平、垂直方向都居中

# 怎么用

  • 定位 + margin:auto
  • 定位 + 自身一半 (定宽)
  • 定位 + transform
  • flex
  • table
  • grid

# 原理

  • 定位 + margin:auto
<style>
    .parent{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      position: relative;
    }
    .child{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }
  </style>
  <div class="parent">
    <div class="child"></div>
  </div>
  • 定位 + 自身一半
  <style>
    .parent2{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      position: relative;
    }
    .child2{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
      position: absolute;
      /* 易漏点 */
      left: 50%;
      top:50%;
      margin-left:-50px;
      margin-top: -50px;
    }
  </style>
  <div class="parent2">
    <div class="child2"></div>
  </div>
  • 定位 + transform
  <style>
    .parent3{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      position: relative;
    }
    .child3{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
  </style>
  <div class="parent3">
    <div class="child3"></div>
  </div>
  • flex
  <style>
    .parent4{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .child4{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
    }
  </style>
  <div class="parent4">
    <div class="child4"></div>
  </div>
  • table
<style>
    .parent5{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      display: table-cell;
      vertical-align: middle;
      text-align: center;
    }
    .child5{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
      /* 必须也要设置display */
      display: inline-block; 
    }
  </style>
  <div class="parent5">
    <div class="child5"></div>
  </div>
  • grid
  <style>
    .parent6{
      width: 200px;
      height: 200px;
      border: 1px solid #ddd;
      display: grid;
      justify-content: center;
      align-items: center;
    }
    .child6{
      width: 100px;
      height: 100px;
      border: 1px solid #f43;
    }
  </style>
  <div class="parent6">
    <div class="child6"></div>
  </div>
上次更新: 2021/12/19, 18:05:42
说说什么是重绘和回流,如何优化
说说CSS3新增了哪些新特性

← 说说什么是重绘和回流,如何优化 说说CSS3新增了哪些新特性→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式