/*html*/
<div class="wrap">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div> /*css*/
.wrap{
display:table;
width: 200px;
}
.wrap span{
display:table-cell;
text-align:center;
}

nth-child(n)

//选择父元素下的第二个子元素,且为span
.wrap span:nth-child(2){
color:red;
}

但是如果子元素是混乱的,比如:

<div class="wrap">
<span>1</span>
<p>p</p>
<span>2</span>
<span>3</span>
<span>4</span>
</div>

nth-child(2)会选择第2个子元素,而且子元素必须是span,但是没找到(第二个子元素为p)

.wrap span:nth-child(3){
color:red;
}

nth-child(3)会选择第3个子元素,而且子元素必须是span

相关

  • nth-last-child(n) 从后往前找子元素

nth-of-type(n)

//选择父元素下的第二个span
.wrap span:nth-of-type(2){
color:red;
}

同样的html,nth-of-type(2) 会找到子元素的第2个span,不管有没有其他元素阻碍

相关

  • nth-last-of-type(n) 从后往前找子元素

only-child

<div class="wrap">
<span>1</span>
</div> /*css*/
.wrap span:only-child{
color:red;
}

只有父元素下有一个子元素时,才会生效

当有其他任意标签时,不生效

only-child应用比较少

相关

  • last-child 最后一个子元素

only-of-type

对比于only-child,only-of-type允许父元素下有其他类的子元素

// 这时会选中唯一的span元素
<div class="wrap">
<span>1</span>
<i>2</i>
</div>
.wrap span:only-of-type{
color:red;
}

相关

  • first-of-type 选中第一个指定子元素
  • last-of-type 选中最后一个指定子元素

css 选择器之子元素的更多相关文章

  1. 深入学习jQuery选择器系列第二篇——过滤选择器之子元素选择器

    × 目录 [1]通用形式 [2]反向形式 [3]首尾元素 [4]唯一元素 前面的话 在上一篇中已经介绍过基础选择器和层级选择器,本文开始介绍过滤选择器.过滤选择器是jQuery选择器中最为庞大也是最为 ...

  2. jQuery选择器之子元素过滤选择器Demo

    测试代码: 07-子元素过滤选择器.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  3. jQuery选择器之子元素选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  4. jquery选择器之子元素

    HTML代码: :first-child 匹配第一个子元素,每个父元素的第一个子元素 :last-child 匹配最后一个子元素,每个父元素的最后一个子元素 <!DOCTYPE html> ...

  5. css3类选择器之结合元素选择器和多类选择器

    css3类选择器之结合元素选择器和多类选择器用法: <!DOCTYPE html> <html lang="en"> <head> <me ...

  6. weex中css不能使用子元素选择器

    weex中css不能使用子元素选择器 刚入手可能会有错误,有误导地方请谅解,不过都是亲身踩坑

  7. 用css完成根据子元素不同书写样式

    我们需要达到的效果: 需要什么 1张图片的, 2张图片的, 3张图片的样式各不相同.可以使用js完成子元素的判断,但是这里我使用css来完成 核心知识点 使用css选择器完成子元素的判断 例子: 用c ...

  8. CSS选择器之伪类选择器(伪元素)

    selection [CSS4]应用于文档中被用户高亮的部分(比如使用鼠标或其他选择设备选中的部分).(IE8及以下不支持)(火狐-moz-selection) first-line 选择每个 < ...

  9. CSS选择器之伪类选择器(元素)

    :first-child 选择某个元素的第一个子元素(IE6不支持) :last-child 选择某个元素的最后一个子元素 :first-of-type [CSS3]选择一个上级元素下的第一个同类子元 ...

随机推荐

  1. WPF程序中的弱事件模式

    在C#中,得益于强大的GC机制,使得我们开发程序变得非常简单,很多时候我们只需要管使用,而并不需要关心什么时候释放资源.但是,GC有的时并不是按照我们所期望的方式工作. 例如,我想实现一个在窗口的标题 ...

  2. sql server 老外博客

    Aaron Bertrand Grant Fritchey Brent Ozar Thomas LaRock Pinal Dave Phil Factor SQL Skills w/ Paul Ran ...

  3. cocurrent包semaphore信号量

    semaphore英[ˈseməfɔ:(r)]美[ˈsɛməˌfɔr, -ˌfor]n. 臂板信号系统,(铁道)臂板信号装置; Semaphore 用法 信号量主要有两种用途: 保护一个重要(代码)部 ...

  4. tmux用法

    列出所有的tmux  session,一个session是多个窗口的集合 tmux list-session 创建tmux窗口, tmux new -s server server为tmux的sess ...

  5. ckeditor的详细配置(转)

    CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...

  6. vertex buffer 数据结构 如何读vb的memory pool

    vertex attribute (declaration)    vertex stream (memory pool) 这两部分 通过attribute 里面对memory的描述把两部分 vbo ...

  7. spring 配置多数据源 (案例)

    *.properties配置: <!--数据连接配置一--> jdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:m ...

  8. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  9. notepad++ 在每一行最后加上逗号

    1.全选缩进对齐 2.替换功能,入下全部替换 3.在入下替换 4.结果 完成!

  10. node - 写返回mime类型

    何为mime类型?  mime.json { ".323":"text/h323" , ".3gp":"video/3gpp&qu ...