第十五讲_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:运行代码,无论怎么调整视口的大小,红色块始终位于视口的水平垂直居中的位置。

相关推荐
PineappleCoder4 小时前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder4 小时前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
m0_471199634 小时前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
GIS之路4 小时前
GDAL 读取KML数据
前端
今天不要写bug5 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
用户47949283569155 小时前
记住这张时间线图,你再也不会乱用 useEffect / useLayoutEffect
前端·react.js
咬人喵喵5 小时前
14 类圣诞核心 SVG 交互方案拆解(附案例 + 资源)
开发语言·前端·javascript
问君能有几多愁~6 小时前
C++ 日志实现
java·前端·c++
咬人喵喵6 小时前
CSS 盒子模型:万物皆是盒子
前端·css