Vue3 Composition API生命周期钩子与<script setup> TypeScript实战指南
2026/8/29 17:48:25
给父元素设置:text-align: center
适用:文字、span、a、img、input、button 等。
方案一:margin 居中(常规首选)
元素定宽时:margin: 0 auto
方案二:Flex 居中(不定宽通用)
父 { display: flex; justify-content: center; }(1)单行文本垂直居中
height = line-height
(2)Flex 万能居中(推荐)
父 { display: flex; justify-content: center; align-items: center; }(3)绝对定位居中(不定高宽通用)
父 { position: relative; } 子 { position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; }(4)transform 居中
子 { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); }