永发信息网

html/css中的伪类first-child的问题

答案:2  悬赏:10  手机版
解决时间 2021-01-23 13:59
  • 提问者网友:我是我
  • 2021-01-22 23:48
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
p:first-child {font-weight: bold;}
li:first-child {text-transform:uppercase;}
</style>
</head>

<body>
<div>
<ul>
<li>Push accelerator</li>
</ul>
<p>Do not push the brake at the same time as the accelerator.</p>
</div>

</body>
</html>

为什么p:first-child {font-weight: bold;}不起作用??求大神解答~
最佳答案
  • 五星知识达人网友:爱难随人意
  • 2021-01-23 00:30
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child
{
background-color:yellow;
}
</style>
</head>
<body>

<p>这个段落是其父元素(body)的首个子元素。</p>

<h1>欢迎访问我的主页</h1>
<p>这个段落不是其父元素的首个子元素。</p>

<div>
<p>这个段落是其父元素(div)的首个子元素。</p>
<p>这个段落不是其父元素的首个子元素。</p>
</div>

<p><b>注释:</b>对于 IE8 及更早版本的浏览器中的 :first-child,必须声明 <!DOCTYPE>。</p>

</body>
</html>

是这样的
全部回答
  • 1楼网友:duile
  • 2021-01-23 00:48
你的代码有两个错误,一是p:first-child {font-weight: bold;}有误,而是<p></p>没有调用样式。 修改为: <style type="text/css"> p.first-child {font-weight: bold;} li.first-child {text-transform:uppercase;} </style> <li class="first-child">Push accelerator</li> <p class="first-child">Do not push the brake at the same time as the accelerator.</p>
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯