如何在 HTML/CSS 中设置某些文本子集的样式?
目前,我正在做这样的事情:
<h2><div class='q'>Q:</div> Does alfredo sauce contain soy?</h2>
然后在我的 CSS 文件中对其进行样式设置,如下所示:
.q {
padding-bottom: 15px;
display: inline;
font-size: 35px;
font-weight: 700;
color: #65A6D1;
}
虽然这在我的浏览器中显示良好,但通过 http://validator.w3.org,它抱怨:“在此上下文中元素 div 不允许作为元素 h2 的子元素。(抑制此子树中的进一步错误。)”
我该怎么办将这段文本设置为有效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:
.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.)"
How would I style this piece of text in valid HTML/CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用跨度
还可以从类中删除 display: inline
You can use a span
also remove display: inline from the class
在 h2 内使用 span 而不是 div。
Use a span instead of a div inside the h2.
使用
标记而不是
。
是内联元素,而
是块元素。
Use the
<span>
tag instead of<div>
.<span>
is an inline element, while<div>
is a block element.div 创建一个新的块元素。这些在 h2 和许多其他元素中是被禁止的。您可以使用 span 创建内联元素。
当然,您需要相应地更改样式表。
A div creates a new block element. These are forbidden in h2 and many other elements. You can create an inline element with span.
Of course, you need to change the stylesheet accordingly.
你可以这样做:
You can do this:
div
和h2
都是块元素。使用span
而不是div
。例如:
附加说明:【非规范描述】有些元素不喜欢包含块元素。标头(h1、h2、...)元素不喜欢包含块元素。 “不喜欢”:我相信规范说“不应该”。
div
andh2
are both block elements. Usespan
instead ofdiv
.For example:
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. "Don't like": the spec says "should not" I believe.