可以通过以下方法实现:

<div style="display: flex; justify-content: center; align-items: center; height: 300px;">
  <p style="text-align: center;">文字内容</p>
</div>

上面的代码中,使用了 flex 布局,通过设置 display: flex,使父元素成为 flex 容器。然后通过设置 justify-content: center; align-items: center; 使子元素在水平和垂直方向上都居中对齐。

在子元素中,通过设置 text-align: center; 让文字水平居中。这样,文字就可以实现上下左右都居中了。

html怎么实现一个块中的文字上下左右都居中

原文地址: http://www.cveoy.top/t/topic/fqhD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录