第十五讲_css水平垂直居中的技巧

css水平垂直居中的技巧

  • [1. 水平垂直居中(场景一)](#1. 水平垂直居中(场景一))
  • [2. 水平垂直居中(场景二)](#2. 水平垂直居中(场景二))
  • [3. 水平垂直居中(场景三)](#3. 水平垂直居中(场景三))
  • [4. 水平垂直居中(场景四)](#4. 水平垂直居中(场景四))

1. 水平垂直居中(场景一)

条件 :一个固定大小的div,包含一个固定大小的div

效果 :让内部的div位于外部div水平垂直居中的位置,且内部的div文本水平垂直居中。

注意事项 :父元素的第一个子元素margin塌陷的问题。

html 复制代码
<html>
  <style>
    .outer {
      height: 500px;
      width: 500px;
      background-color: brown;
      margin: 0 auto;
      /* 解决margin塌塌陷问题 */
      overflow: hidden;
    }

    .inner {
      height: 100px;
      width: 100px;
      background-color: aqua;
      /* 设置该元素在父元素中水平居中 */
      margin: 0 auto;
      margin-top: 200px;
      /* 设置该元素中的文本水平居中 */
      text-align: center;
      /* 设置该元素中的文本垂直居中 */
      line-height: 100px;
    }
  </style>

  <div class="outer">
    <div class="inner">hello</div>
  </div>
</html>

2. 水平垂直居中(场景二)

条件 :一个固定大小的div,包含一个行内元素、行内块元素。

效果 :让内部的行内元素、行内块元素都位于外部div水平垂直居中的位置。

注意事项:基线的影响。

html 复制代码
<html>
  <style>
    .outer {
      height: 500px;
      width: 500px;
      background-color: brown;
      /* 设置内部文本或行内元素水平居中 */
      text-align: center;
      /* 设置内部文本或行内元素垂直居中 */
      line-height: 500px;
      /* 为了让其子元素正好垂直居中,没有偏差 */
      font-size: 0;
    }

    .inner {
      background-color: aqua;
      /* 行内元素的基线相对于该元素所在行的基线的垂直对齐 */
      vertical-align: middle;
      font-size: 20px;
    }

    img {
      height: 100px;
      width: 100px;
      vertical-align: middle;
    }
  </style>

  <div class="outer">
    <span class="inner">hello</span>
    <img
      src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
    />
  </div>
</html>

3. 水平垂直居中(场景三)

条件:一个伸缩容器。

效果:让伸缩项目在伸缩容器的水平垂直居中位置。

实现方式一:

html 复制代码
<html>
  <style>
    .div1 {
      width: 500px;
      height: 500px;
      background-color: blueviolet;
      /* 设置为伸缩容器 */
      display: flex;
      /* 主轴上居中对齐 */
      justify-content: center;
      /* 侧轴上居中对齐 */
      align-items: center;
    }
    .div2 {
      width: 200px;
      height: 200px;
      background-color: blue;
    }
  </style>

  <div class="div1">
    <div class="div2"></div>
  </div>
</html>

实现方式二:

html 复制代码
<html>
  <style>
    .outer {
      width: 500px;
      height: 500px;
      background-color: blueviolet;
      /* 设置为伸缩容器 */
      display: flex;
    }
    .inner {
      width: 200px;
      height: 200px;
      background-color: blue;
      /* 设置margin为auto,会让伸缩项目水平垂直居中 */
      margin: auto;
    }
  </style>

  <div class="outer">
    <div class="inner"></div>
  </div>
</html>

4. 水平垂直居中(场景四)

条件 :一个固定大小的div

效果 :让div在视口的水平垂直居中位置。

注意事项:视口的大小不固定。

html 复制代码
<html>
  <style>
    body {
      /* 设置body的高度为视口的高度 */
      height: 100vh;
      /* 设置为伸缩容器 */
      display: flex;
    }

    .div1 {
      width: 500px;
      height: 500px;
      background-color: brown;
      /* 垂直水平居中父元素 */
      margin: auto;
    }
  </style>

  <div class="div1"></div>
</html>

代码中vh单位,是相对于视口高度的百分比,更多长度单位介绍可以参考另一篇博文:长度单位介绍

ps:运行代码,无论怎么调整视口的大小,红色块始终位于视口的水平垂直居中的位置。

相关推荐
牛马1112 分钟前
Flutter CustomPaint
开发语言·前端·javascript
炽烈小老头13 分钟前
函数式编程范式(三)
前端·typescript
ruoyusixian25 分钟前
chrome二维码识别查插件
前端·chrome
fengfuyao98536 分钟前
一个改进的MATLAB CVA(Change Vector Analysis)变化检测程序
前端·算法·matlab
yuhaiqiang1 小时前
为什么这道初中数学题击溃了所有 AI
前端·后端·面试
djk88881 小时前
支持手机屏幕的layui后台html模板
前端·html·layui
紫_龙1 小时前
最新版vue3+TypeScript开发入门到实战教程之watch详解
前端·javascript·typescript
默默学前端2 小时前
ES6模板语法与字符串处理详解
前端·ecmascript·es6
lxh01132 小时前
记忆函数 II 题解
前端·javascript
我不吃饼干2 小时前
TypeScript 类型体操练习笔记(三)
前端·typescript