如何仅使用CSS设置目标文本的背景颜色? [英] How do I set the background color of targeted text only, using only CSS?

查看:100
本文介绍了如何仅使用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.

这篇关于如何仅使用CSS设置目标文本的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆