css first-child,css :first-child选择器使用实例详解

css :first-child介绍

css :first-child设置父元素的第一个子元素的样式表属性,即匹配父元素的第一个子元素,并为其设置css样式。例如:一个div中包括多个p元素,匹配第一个p元素可使用:first-child伪类

语法:

:first-child {

style properties

}

比如:

body *:first-child {font-weight: bold;}

p:first-child {font-size: 125%;}

css :first-child实例

先看如下一段代码,HTML部分:

  • aa
  • bb
  • cc
  • dd

如果需要将第一个li的margin-left设为0px,则可以通过下面这个方法实现:

.example li:first-child{margin-left:10px;}

再看一个实例:匹配属于父元素中第一个子元素的每个

元素

p:first-child

{

background-color:yellow;

}

This paragraph is the first child of its parent (body).

/* http://www.manongjc.com/article/1304.html */

Welcome to My Homepage

This paragraph is not the first child of its parent.

This paragraph is the first child of its parent (div).

This paragraph is not the first child of its parent.

Note: For :first-child to work in IE8 and earlier, a DOCTYPE must be declared.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值