Answers:
将文本放入内联元素中,例如<span>
。
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
然后将背景颜色应用于内联元素。
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
内联元素与其内容一样大,因此应该为您完成。
h1 { display:inline; }
display: table;
h1 {
display: table; /* keep the background color wrapped tight */
margin: 0px auto 0px auto; /* keep the table centered */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
小提琴
http://jsfiddle.net/J7VBV/293/
更多
display: table
告诉元素表现得像普通的HTML表一样。
在w3schools,CSS Tricks和此处了解更多
display: inline-flex;
text-align: center;
父母.container {
text-align: center; /* center the child */
}
h1 {
display: inline-flex; /* keep the background color wrapped tight */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
display: flex;
.container {
display: flex;
justify-content: center; /* center the child */
}
h1 {
display: flex;
/* margin: 0 auto; or use auto left/right margin instead of justify-content center */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
关于
也许最受欢迎的Flexbox指南是我经常引用的CSS Tricks。
display: block;
.container {
display: flex;
justify-content: center; /* centers child */
}
h1 {
display: block;
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<div class="container">
<h1>The Last Will and Testament of Eric Jones</h1>
</div>
::before
h1 {
display: flex; /* set a flex box */
justify-content: center; /* so you can center the content like this */
}
h1::before {
content:'The Last Will and Testament of Eric Jones'; /* the content */
padding: 5px;font-size: 20px;background-color: green;color: #ffffff;
}
<h1></h1>
小提琴
http://jsfiddle.net/J7VBV/457/
关于
有关CSS伪元素:: before和:: after的更多信息,请访问 CSS技巧的 w3schools一般的伪元素
display: inline-block;
以position: absolute
和为中心translateX
需要position: relative
父母
.container {
position: relative; /* required for absolute positioned child */
}
h1 {
display: inline-block; /* keeps container wrapped tight to content */
position: absolute; /* to absolutely position element */
top: 0;
left: 50%; /* part1 of centering with translateX/Y */
transform: translateX(-50%); /* part2 of centering with translateX/Y */
white-space: nowrap; /* text lines will collapse without this */
padding:5px;font-size:20px;background-color:green;color:#ffffff;
}
<h1>The Last Will and Testament of Eric Jones</h1>
关于
此CSS技巧文章中的更多内容transform: translate();
(和一般居中)
text-shadow:
和 box-shadow:
h1, h2, h3, h4, h5 {display: table;margin: 10px auto;padding: 5px;font-size: 20px;color: #ffffff;overflow:hidden;}
h1 {
text-shadow: 0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green,
0 0 5px green,0 0 5px green;
}
h2 {
text-shadow: -5px -5px 5px green,-5px 5px 5px green,
5px -5px 5px green,5px 5px 5px green;
}
h3 {
color: hsla(0, 0%, 100%, 0.8);
text-shadow: 0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 10px hsla(120, 100%, 25%, 0.5),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1),
0 0 5px hsla(120, 100%, 25%, 1);
}
h4 { /* overflow:hidden is the key to this one */
text-shadow: 0px 0px 35px green,0px 0px 35px green,
0px 0px 35px green,0px 0px 35px green;
}
h5 { /* set the spread value to something larger than you'll need to use as I don't believe percentage values are accepted */
box-shadow: inset 0px 0px 0px 1000px green;
}
<h1>The First Will and Testament of Eric Jones</h1>
<h2>The 2nd Will and Testament of Eric Jones</h2>
<h3>The 3rd Will and Testament of Eric Jones</h3>
<h4>The Last Will and Testament of Eric Jones</h4>
<h5>The Last Box and Shadow of Eric Jones</h5>
小提琴
https://jsfiddle.net/Hastig/t8L9Ly8o/
通过结合上面的不同显示选项和居中方法,还有其他几种方法可以解决此问题。
display: table
可能如此强大,但我感谢您提供了可行的替代方案。
游戏有点晚了,但我想我会加2美分...
为了避免添加内部范围的额外标记,您可以将<h1>
display属性从更改block
为inline
(catch是,您要确保<h1>
are块元素之后的元素。
的HTML
<h1>
The Last Will and Testament of
Eric Jones</h1>
<p>Some other text</p>
的CSS
h1{
display:inline;
background-color:green;
color:#fff;
}
结果
JSFIDDLE
http://jsfiddle.net/J7VBV/
其他人忽略的主要考虑因素是OP声明他们不能修改HTML。
您可以在DOM中定位所需的内容,然后使用javascript动态添加类。然后根据需要进行样式设置。
在我制作的示例中,我<p>
使用jQuery 定位了所有元素,然后将其包装为带有“彩色”类的div
$( "p" ).wrap( "<div class='colored'></div>" );
然后在我的CSS中,我定位了<p>
,并为其指定了背景色,并更改为display: inline
.colored p {
display: inline;
background: green;
}
通过将显示设置为嵌入式,您将失去通常会继承的某些样式。因此,请确保您针对最具体的元素并为容器设置样式以适合您的其余设计。这仅是一个工作起点。小心使用。CodePen上的工作演示
其他答案请注意,您可以background-color
在<span>
在文本周围使它起作用。
如果有的话line-height
,您会看到差距。要解决此问题,您可以在范围上添加一box-shadow
点点增长。您还需要box-decoration-break: clone;
FireFox正确渲染它。
编辑:如果您在IE11中遇到盒子阴影问题,请尝试outline: 1px solid [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>
可以在段落和标题标记内使用html5 标记。
<p>lorem ipsum <mark>Highlighted Text</mark> dolor sit.</p>
试试这个:
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与所有浏览器都不兼容:)只想与原始文章中的对齐方式保持一致。
的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.
}
}
box-decoration-break: clone;
,developer.mozilla.org
的HTML
<h1 class="green-background"> Whatever text you want. </h1>
的CSS
.green-background {
text-align: center;
padding: 5px; /*Optional (Padding is just for a better style.)*/
background-color: green;
}
<h1 style="display:inline-block;text-align: center;background : red;">The Last Will and Testament of Eric Jones</h1>