以前一般都用:nth-child,后来知道了:nth-of-type,然后就一般用nth-of-type

它们两有什么区别呢?

首先来看个现象:

假如有这样一个HTML结构

<div class="post">
<p>我是段落1</p>
<p>我是段落2</p>
</div>

分别加上两个样式的效果

.post > p:nth-child(2){
background-color: red;
}

.post > p:nth-of-type(2){
background-color: yellow;
}

这个时候我们发现它们的效果是一样的,那么它们的区别是什么呢?别着急,看下面这个例子

这是HTML结构:

<div class="box">
<h1>我是标题</h1>
<p>我是段落1</p>
<p>我是段落2</p>
</div>

和案例1一样的样式加上去

.box > p:nth-child(2){
background-color: red;
}

.box > p:nth-of-type(2){
background-color: yellow;
}

如果我们把样式稍微改一下

.box > p:nth-child(1){
background-color: blue;
}

.box > p:nth-of-type(1){
background-color: blue;
}

这个时候:nth-child的效果就比较出乎意料了;

通过几个案例在来说两者的区别可能好理解一点:

"p:nth-chil(n)":p的父元素的第n个子元素,如果这个元素时p类型,那么就给它加上后面的样式,如果不是那就算了

"p:nth-of-type(n)":p的父元素的p类型的子元素的第n个

nth-child和nth-of-type的区别的更多相关文章

  1. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

  2. Html中,id、name、class、type的区别

    <input type="text" name="name" id="name" class="txt"> ...

  3. isinstance与type的区别

    1.isinstance()内置函数 python中的isinstance()函数是python的内置函数,用来判断一个函数是否是一个已知类型.类似type. 2.用法: isinstance(obj ...

  4. python isinstance()与type()的区别

    例如在继承上的区别: isinstance() 会认为子类是一种父类类型,考虑继承关系. type() 不会认为子类是一种父类类型,不考虑继承关系. class A: pass class B(A): ...

  5. class kind type sort区别

    class多用于 级别比如高级货就是 first class,primary class等等,以此类推kind 和sort 基本一样,就像你说的,译为 种类,what kind of疑问,回答时用so ...

  6. python 内建函数isinstance的用法以及与type的区别

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  7. isinstance 和 type 的区别

    class A: pass class B(A): pass isinstance(A(), A) # returns True type(A()) == A # returns True isins ...

  8. 【学习总结】Python-3- 类型判断之 isinstance 和 type 的区别

    菜鸟教程-Python3-基本数据类型 关于类型查询: type() 函数:可以用来查询变量所指的对象类型 用 isinstance()函数:判断是否是某个类型 两者的区别: type()不会认为子类 ...

  9. const type& 与 type& 的区别

    const type& 与 type& 是C/C++编程中容易混淆的两个知识点,现在以 cont int& 与 int& 为例讲解: 1.int& 讲解 int ...

  10. form表单重复提交,type=“button”和type=“submit”区别

    公司测试提了一个项目后台在IE浏览器下(360,firefox就没问题)出现数据重复的问题,调试了好久终于发现问题所在,也不知道是谁写的代码,醉醉的.... 错误地点: <input type= ...

随机推荐

  1. struts2必需jar包

    asm-3.3.jar                   commons-logging-1.1.3.jarasm-commons-3.3.jar           freemarker-2.3. ...

  2. jquery模拟checkbox效果,以及background-size在jquery中的使用。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  3. java合并list

    import java.util.ArrayList; import java.util.List;   import com.google.common.collect.Lists;   priva ...

  4. C#总结(2)

    有输出,当然有输入.这样才会有人机交互. using System; using System.Collections.Generic; using System.Linq; using System ...

  5. css 梯形标签页

    html 代码 略 css : nav > a{ position: relative; display: inline_block; padding: .3em 1em 0; } nav &g ...

  6. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  7. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...

  8. a^b的前n位数

    假设我们现在需要知道 ab  的后 n 位数或前 n 位数,简单直观的做法就是求出 ab  的值,然后在分别取前 n位或后 n位,不过在 a,b很大的情况下显然是无法存储的.所以,直接求是不可能的了. ...

  9. DOM2定位与高宽类属性专题学习【DOM专题学习系列(一)】

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  10. 利用cookies获取登录后的网页

    众所周知,HTTP连接是无状态的,那么问题来了,怎么记录用户的登录信息呢?通常的做法是用户第一次发送HTTP请求时,在HTTP Server端生成一个SessionID,SessionID会对应每个会 ...