你可以使用下面的代码。
通过这种方式,您不必关心HTML标签到您的li中的顺序,而且您还可以将文本置于y轴上。
li:first-child {
position: relative; /* First you create a reference for your h4 absolute item. */
text-align: right; /* And you put the span to right. */
}
li:first-child span,
li:first-child h4 {
text-align: left; /* You align content as you want. */
}
li:first-child h4 {
/* And you X/Y center the h4 content with the `li` relavive reference. */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* The CSS below is not a part of the answer but help the result to be more nice. */
ul {
width:100%;
list-style-type:none;
margin: 0;
padding: 0;
}
li {
border: 1px solid #ccc;
}
li span {
display: inline-block;
padding: 20px;
border-left: 1px solid #ccc;
}
li h4 {
margin: 0;
}
<ul>
<li>
<h4>A AAA AA AAA AA A AAAAA A </h4>
<span>B</span>
</li>
<li>
<h4>A AAA AA AAAAAA<br>AA A AAAAA<br>A AAA AA AAAAAA</h4>
</li>
<li>
<h4>A AAA AAAAA</h4>
</li>
<li>
<h4>A AAA AA<br>AA A AAAAA</h4>
</li>
</ul>
如果你只是想集中在x轴,只使用left: 50%
和transform: translateX(-50%)
。
你应该用一些标记和样式发表小提琴。 – inorganik