[英]How to style a subset of some text in HTML/CSS?
Currently, I'm doing something like this: 目前,我正在做这样的事情:
<h2><div class='q'>Q:</div> Does alfredo sauce contain soy?</h2>
and then styling it in my CSS file, like so: 然后在我的CSS文件中设置样式,如下所示:
.q {
padding-bottom: 15px;
display: inline;
font-size: 35px;
font-weight: 700;
color: #65A6D1;
}
While this displays fine in my browser, when running the page through http://validator.w3.org , it complains: "Element div not allowed as child of element h2 in this context. (Suppressing further errors from this subtree.)" 虽然这在我的浏览器中显示正常,但在通过http://validator.w3.org运行页面时,它会抱怨:“在此上下文中,元素div不允许作为元素h2的子元素。(从此子树中抑制更多错误。)”
How would I style this piece of text in valid HTML/CSS? 如何在有效的HTML / CSS中设置这段文字的样式?
You can use a span 你可以使用跨度
<h2><span class='q'>Q:</span> Does alfredo sauce contain soy?</h2>
also remove display: inline from the class 还删除显示:从类内联
.q {
padding-bottom: 15px;
/*display: inline;*/
font-size: 35px;
font-weight: 700;
color: #65A6D1;
}
在h2中使用span而不是div。
Use the <span>
tag instead of <div>
. 使用
<span>
标记而不是<div>
。 <span>
is an inline element, while <div>
is a block element. <span>
是内联元素,而<div>
是块元素。
A div creates a new block element. div创建一个新的块元素。 These are forbidden in h2 and many other elements.
这些在h2和许多其他元素中是被禁止的。 You can create an inline element with span.
您可以使用span创建内联元素。
<h2><span class='q'>Q:</span> Does alfredo sauce contain soy?</h2>
Of course, you need to change the stylesheet accordingly. 当然,您需要相应地更改样式表。
div
and h2
are both block elements. div
和h2
都是块元素。 Use span
instead of div
. 使用
span
而不是div
。
For example: 例如:
<h2><span class="q">Q:</span> Blammy blammo soy?</h2>
additional note: [Non-normative description] Some elements don't like to contain block elements. 附加说明:[非规范性描述]某些元素不喜欢包含块元素。 The header (h1, h2, ...) elements don't like to contain block elements.
标题(h1,h2,...)元素不喜欢包含块元素。 "Don't like": the spec says "should not" I believe.
“不喜欢”:规范说“不应该”我相信。
You can do this: 你可以这样做:
<h2 id="q"><span>Q</span>Does alfredo sauce contain soy?</h2>
h2#q span {
padding-bottom: 15px;
display: inline;
font-size: 35px;
font-weight: 700;
color: #65A6D1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.