我想要的是绿色背景刚好在文本的后面,而不是100%的页面宽度。这是我当前的代码:
h1 { text-align:中心; 背景颜色:绿色; } <h1>埃里克·琼斯最后的遗嘱和遗嘱<h1>
我想要的是绿色背景刚好在文本的后面,而不是100%的页面宽度。这是我当前的代码:
h1 { text-align:中心; 背景颜色:绿色; } <h1>埃里克·琼斯最后的遗嘱和遗嘱<h1>
当前回答
你必须提到h1标签的宽度..
你的CSS是这样的
h1 {
text-align: center;
background-color: green;
width: 600px;
}
其他回答
将文本放入内联元素中,例如<span>。
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
然后在内联元素上应用背景色。
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
内联元素和它的内容一样大,所以这应该可以帮你。
试试这个:
h1 {
text-align: center;
background-color: green;
visibility: hidden;
}
h1:after {
content:'The Last Will and Testament of Eric Jones';
visibility: visible;
display: block;
position: absolute;
background-color: inherit;
padding: 5px;
top: 10px;
left: calc(30% - 5px);
}
请注意,calc并不兼容所有浏览器:)只是想与原始帖子中的对齐方式保持一致。
https://jsfiddle.net/richardferaro/ssyc04o4/
<mark>标签与背景颜色帮助我。
输入:
<p>
Effective <mark style="background-color: light-gray">Nov 1, 2022</mark> We will be lowering our price.
</p>
输出: 自2022年11月1日起,我们将降低价格。
Ref
HTML
<h1>
<span>
inline text<br>
background padding<br>
with box-shadow
</span>
</h1>
Css
h1{
font-size: 50px;
padding: 13px; //Padding on the sides so as not to stick.
span {
background: #111; // background color
color: #fff;
line-height: 1.3; //The height of indents between lines.
box-shadow: 13px 0 0 #111, -13px 0 0 #111; // Indents for each line on the sides.
}
}
codeen上的演示
正如其他答案所指出的那样,您可以在文本周围的<span>中添加背景颜色以使其工作。
在有行高的情况下,你会看到空白。为了解决这个问题,你可以在你的跨度上添加一个盒子阴影。你还需要box-decoration-break: clone;FireFox才能正确渲染它。
编辑:如果你在IE11中遇到盒子阴影的问题,试着添加一个轮廓:1px纯色[color];也只适用于IE。
下面是它实际运行的样子:
.container { margin: 0 auto; width: 400px; padding: 10px; border: 1px solid black; } h2 { margin: 0; padding: 0; font-family: Verdana, sans-serif; text-transform: uppercase; line-height: 1.5; text-align: center; font-size: 40px; } h2 > span { background-color: #D32; color: #FFF; box-shadow: -10px 0px 0 7px #D32, 10px 0px 0 7px #D32, 0 0 0 7px #D32; box-decoration-break: clone; } <div class="container"> <h2><span>A HEADLINE WITH BACKGROUND-COLOR PLUS BOX-SHADOW :3</span></h2> </div>