在CSS中设置文本居中可以通过以下几种方法:
行内元素文本居中
对于单行文本,可以使用 `text-align` 属性设置为 `center`:
span {text-align: center;}
块级元素文本居中
对于多行文本或块级元素,可以通过设置左右外边距为 `auto` 并设置宽度不为 `100%` 来完成居中:
div {width: 50%;margin-left: auto;margin-right: auto;}
表格文本居中
对于表格中的文本,可以使用 `text-align` 属性设置为 `center`:
td {text-align: center;}

水平居中
利用 `margin` 属性,设置左右外边距为 `auto` 即可完成水平居中:
div {width: 50%;margin-left: auto;margin-right: auto;}
垂直居中
垂直居中方法比较复杂,有多种实现方式:
1. 使用 Flexbox
.parent {display: flex;align-items: center;}
2. 使用 `table-cell`
.parent {display: table;height: 100px;}.child {display: table-cell;vertical-align: middle;}
以上是几种常见的文本居中方法。您可以根据具体需求选择合适的方法
