如何仅使用CSS设置目标文本的背景颜色?
问题描述:
如何动态地在文本后面添加绿色背景,而不是整个页面宽度,只是文本。
How can I dynamically add a green background behind text, not the entire page width, just the text.
绿色正在使用当前代码扩展到整个页面。我无法修改HTML或Javascript,只是CSS文件。
The green is expanding to the entire page with my current code. I am not able to modify the HTML or Javascript, just the CSS file.
$ b
HTML
<h1>The Last Will and Testament of Eric Jones</h1>
CSS
CSS
h1 {
text-align: center;
background-color: green;
}
答
一个href =http://www.htmlhelp.com/reference/html40/inline.html =noreferrer>内联元素,比如< span> code>。
Put the text in an inline element, such as a <span>
.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
然后在内联元素上应用背景颜色。
And then apply the background color on the inline element.
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
内联元素与内容一样大,所以应该这样做给你。
An inline element is as big as its contents is, so that should do it for you.