当前位置: 首页 > news >正文

CSS:水平垂直居中

一、使用 Flexbox 布局

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="flex-center"><div class="content">我是居中内容</div></div>
</template><style scoped>
.flex-center {display: flex;justify-content: center; /* 水平居中 */align-items: center;     /* 垂直居中 */height: 100%;          /* 视口高度 */width: 100%;            /* 宽度 */
}.content {/* 可选样式 */padding: 20px;background-color: #f0f0f0;border-radius: 8px;
}
</style>

二、使用 Grid 布局

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="grid-center"><div class="content">我是居中内容</div></div>
</template><style scoped>
.grid-center {display: grid;place-items: center; /* 同时设置水平和垂直居中 */height: 100%;width: 100%;
}.content {/* 可选样式 */padding: 20px;background-color: #f0f0f0;
}
</style>

三、使用绝对定位 + transform

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="absolute-center"><div class="content">我是居中内容</div></div>
</template><style scoped>
.absolute-center {position: relative;height: 100%;width: 100%;
}.content {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%); /* 反向偏移自身宽高的50% *//* 可选样式 */padding: 20px;background-color: #f0f0f0;
}
</style>

四、使用 margin: auto (需要固定尺寸)

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="margin-auto-center"><div class="content">我是居中内容</div></div>
</template><style scoped>
.margin-auto-center {display: flex;height: 100%;width: 100%;
}.content {margin: auto; /* 需要在flex或grid容器中,且元素需要有尺寸 */width: 200px;  /* 需要固定宽度 */height: 100px; /* 需要固定高度 *//* 可选样式 */padding: 20px;background-color: #f0f0f0;
}
</style>

五、使用 CSS 表格布局

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="table-center"><div class="content">我是居中内容</div></div>
</template><style scoped>
.table-center {display: table;width: 100%;height: 100%;
}.table-center > .content {display: table-cell;text-align: center; /* 水平居中 */vertical-align: middle; /* 垂直居中 */
}
</style>

六、使用 JavaScript 动态计算 (响应式)

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'const position = ref({ x: 0, y: 0 })
const contentRef = ref<HTMLElement>()const updatePosition = () => {if (contentRef.value) {const parent = contentRef.value.parentElementif (parent) {position.value = {x: (parent.clientWidth - contentRef.value.offsetWidth) / 2,y: (parent.clientHeight - contentRef.value.offsetHeight) / 2}}}
}onMounted(() => {updatePosition()window.addEventListener('resize', updatePosition)
})onBeforeUnmount(() => {window.removeEventListener('resize', updatePosition)
})
</script><template><div class="js-center"><div ref="contentRef" class="content" :style="{ transform: `translate(${position.x}px, ${position.y}px)` }">我是JS计算的居中内容</div></div>
</template><style scoped>
.js-center {position: relative;height: 100%;width: 100%;
}.content {position: absolute;/* 初始位置,会被JS覆盖 */top: 0;left: 0;/* 可选样式 */padding: 20px;background-color: #f0f0f0;transition: transform 0.3s ease; /* 平滑过渡 */
}
</style>

七、使用 CSS 变量和 calc()

<script setup lang="ts">
// 不需要额外逻辑,纯CSS实现
</script><template><div class="calc-center"><div class="content">我是使用calc居中的内容</div></div>
</template><style scoped>
.calc-center {position: relative;height: 100%;width: 100%;
}.content {position: absolute;width: 200px;height: 100px;left: calc(50% - 100px); /* 50% - 宽度的一半 */top: calc(50% - 50px);  /* 50% - 高度的一半 *//* 可选样式 */padding: 20px;background-color: #f0f0f0;
}
</style>

Flexbox - 最简单、最现代的方式,推荐使用

Grid - 代码最简洁,但兼容性略低于Flexbox

绝对定位 + transform - 兼容性好,适合复杂场景

margin: auto - 需要固定尺寸,适合简单场景

CSS表格 - 较老的方法,不推荐在新项目中使用

JavaScript计算 - 适合需要动态调整的复杂场景

CSS calc() - 需要知道元素尺寸,灵活性较低

在大多数现代项目中,Flexbox 或 Grid 方法是首选,因为它们简洁、高效且易于维护。

以上方法均经过测试

八、欢迎交流指正

http://www.dtcms.com/a/337496.html

相关文章:

  • 蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
  • 吴恩达 Machine Learning(Class 1)
  • Fluss:颠覆Kafka的面向分析的实时流存储
  • 深入解析Kafka消费者重平衡机制与性能优化实践指南
  • 【Java基础】反射,注解,异常,Java8新特性,object类-详细介绍
  • 民俗博物馆如何选择数字技术?交互体验如何创新文化传播方式?
  • Effective C++ 条款48:认识模板元编程
  • 高并发网络编程实战:深入理解epoll客户端的事件驱动模型
  • Python字典dict的初始化方法
  • 亚马逊合规风暴升级:三类账号风险预警与防御体系构建
  • 图论Day5学习心得
  • 晨控CK-GW08S与欧姆龙PLC配置Ethernet/IP通讯连接手册
  • CAN总线的安全性
  • fit函数
  • 我们为什么需要时序数据库?
  • Image and Video Tokenization with Binary Spherical Quantization 论文阅读
  • Windows桌面自动化的革命性突破:深度解析Windows-MCP.Net Desktop模块的技术奥秘
  • 【音视频】芯片、方案、市场信息收集
  • (52) QT 里使用枚举类,可以把其作为类对象,构造出来的。enum A{ m, n, p }; qDebug() << A(1);
  • 用户态网络缓冲区设计
  • C++常见考点与易错点详细教程
  • SAP 数据脱敏工具:SNP TDO如何满足新颁敏感信息政策要求
  • 视觉语言导航(8)——任务驱动的架构增强 3.3
  • Redis——基础篇
  • 当使用STL容器去存放数据时,是存放对象合适,还是存放对象指针(对象地址)合适?
  • 将std容器的正向迭代器转换成反向迭代器
  • SCAU学习笔记 - 校科联自科二面通关指南
  • 淘宝扭蛋机小程序开发:引领电商娱乐化新潮流
  • Python循环语句 从入门到精通
  • Qt——对话框 QDialog