与其将某些东西居中,不如说您希望图像在单词之间均匀分布。我同意 Blaine 的说法,这些词需要包含在一个跨度中。不过,我不同意设置固定宽度,因为这非常受限制。
相反,我会从 h2 中移动背景图像并将其放置在其中一个跨度的伪元素上:
h2 {
display: inline-block;
text-align: center;
color: #000000;
padding: 20px 0;
font-size: 0; // gets rid of whitespace between the spans
}
span {
font-size: 24px; // resets the font-size of the words
}
span:nth-child(1):after {
content: '';
display: inline-block;
width: 50px;
height: 50px;
background: url("http://s33.postimg.org/twxfn1by7/Playlist_Triangle.png") top center no-repeat;
background-size: 50px;
}
使用 inline-block 将所有内容彼此相邻,并在 h2 上放置 font-size: 0 删除任何空白。
现在单词可以是任意长度,并且图像将在它们之间保持完美间隔。
这是一个演示:https ://jsfiddle.net/rq8u5b3k/1/
如果您出于某种原因无法控制标记,这里有一个 jQuery 片段,它将把每个单词包装在一个跨度中:
var words = $("h2").text().split(" ");
$("h2").empty();
$.each(words, function(i, v) {
$("h2").append($("<span>").text(v));
});
更新演示:https ://jsfiddle.net/rq8u5b3k/3/