* {
background-color: yellowgreen;
}
h1 {
text-align: center;
display: block;
background-color: yellow;
border: 3px solid red;
border-radius: 25px;
}

<h1> This is Main website </h1>
&#13;
是否可以将黄色仅围绕文本而不是整行?
答案 0 :(得分:0)
您只需将属性display: block
更改为display: inline-block
,它就会按照您希望的方式运行。试试这段代码。
h1 {
text-align: center;
}
h1 span {
display: inline-block;
background-color: yellow;
border: 3px solid red;
border-radius: 25px;
}
&#13;
<h1> <span> This is Main website </span> </h1>
&#13;
答案 1 :(得分:0)
对span
的文字使用display: inline-block
标记(这也允许您将文字保持居中,并使边框跨越整个宽度)
如果需要,可以使用padding
设置稍微扩展黄色区域
* {
background-color: yellowgreen;
}
h1 {
text-align: center;
display: block;
border: 3px solid red;
border-radius: 25px;
}
h1 span {
display: inline-block;
background-color: yellow;
padding: 0 10px;
}
&#13;
<h1> <span> This is Main website </span> </h1>
&#13;