Html 将文本居中放置在div中

Html 将文本居中放置在div中,html,css,Html,Css,我想知道如何将文本集中在div中。在我的代码中,这不适用于每个p标记,因为文本长度不相同 HTML:- <div class="textBlock"> <p>text text text</p> </div> 如果只对齐文本,不确定所有其他代码的用途是什么 .textBlock{ 文本对齐:居中; 最小高度:160px; } .textBlock p{ 字号:28px; 字体系列:Helvetica,Arial; 线高:34px; 字母间距

我想知道如何将文本集中在div中。在我的代码中,这不适用于每个p标记,因为文本长度不相同

HTML:-

<div class="textBlock">
  <p>text text text</p>
</div>

如果只对齐文本,不确定所有其他代码的用途是什么

.textBlock{
文本对齐:居中;
最小高度:160px;
}
.textBlock p{
字号:28px;
字体系列:Helvetica,Arial;
线高:34px;
字母间距:0.1米;
}

文本文本文本

请尝试以下方法:

Html:


没错,是绝对定位造成了问题。但不确定这是否是答案;如果OP确实需要绝对定位怎么办?
#content .textBlock {
 position: relative;
 text-align: center;
 width: 100%;
 float: left;
 display: block;
 min-height: 160px;
}

#content .textBlock p {
 position: absolute;
 top: 50%;
 -webkit-transform: translateY(-50%);
 -ms-transform: translateY(-50%);
 transform: translateY(-50%);
 font-size: 28px;
 font-family: Helvetica, Arial;
 font-weight: 100;
 padding: 0 5%;
 line-height: 34px;
 letter-spacing: 0.1em; 
}
<div class="textBlock">
  <p>text text text</p>
</div>
.textBlock {
 text-align: center;

    background-color:gray;
}

.textBlock p {
 font-size: 28px;
 font-family: Helvetica, Arial;
 line-height: 34px;
 letter-spacing: 0.1em; 
    color:white;
}