如何在CSS中设置文本的背景颜色? [英] How do I set background color of text only in CSS?

查看:151
本文介绍了如何在CSS中设置文本的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>

想要的是绿色背景不要100%的页面宽度。
我想把它放在文本的后面。
Html:

What I want is for the green background not to be 100% of the page width. I want it just behind the text. Html:

<!DOCTYPE html>

<head> 
<link rel="stylesheet" type="text/css" href="style.css">
</head> 

<body> 

<h1>  
The Last Will and Testament of
Eric Jones</h1> 

</body> 

style.css:

style.css:

h1
{ 
    text-align: center; 
    background-color: green; 
}


推荐答案

a href =http://www.htmlhelp.com/reference/html40/inline.html>内嵌元素,例如< span>

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天全站免登陆