我想要的是绿色背景刚好在文本后面,而不是页面宽度的 100%。这是我当前的代码:
h1 {
text-align: center;
background-color: green;
}
<h1>The Last Will and Testament of Eric Jones</h1>
最佳答案
将文本放在 inline element 中,例如 <span>
.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
然后在内联元素上应用背景色。
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
内联元素与其内容一样大,因此应该为您做到这一点。
关于html - 如何使用 CSS 设置文本宽度的背景颜色,而不是整个元素的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14310154/