内容简介:吃饭也吃完了,开始来写一写选择器接下来的篇章吧。前面一章说了很多基础方面的知识,怎么说呢?还是比较基础的,现在这篇文章主要来讲一讲伪类和伪元素。关于链接伪类呢?主要介绍
吃饭也吃完了,开始来写一写选择器接下来的篇章吧。
前面一章说了很多基础方面的知识,怎么说呢?还是比较基础的,现在这篇文章主要来讲一讲伪类和伪元素。
一、 链接伪类
关于链接伪类呢?主要介绍 a
元素独有的三种伪元素:
- link:定义a元素未访问时的样式
- visited:定义a元素访问后的样式
- target:突出显示活动的 HTML 锚 首先来介绍关于
link
和visited
的内容,来看下面一段代码:
// html <div> <a href="#">链接一</a> <a href="#1">链接二</a> <a href="#2">链接三</a> <a href="#3">链接四</a> <span>链接四</span> </div> // css * { padding: 0; margin: 0; } a{ text-decoration: none; // 去掉 } a:link { color: red; } a:visited { color:green; } span:link { color: red } span:visited { color:green; } 复制代码
效果如下:
由上面的现象可以观察到: span
元素并没有 link
和 visited
的效果。其实 link
和 visited
是 a
元素特有的伪类,当然也包括下面要讲的 target
伪类,其他的元素使用起来是不会起作用的。
说到这里我突然想插句嘴,这是之前在开发的时候发现的,请诸位注意一下,来看修改一下css代码:
// css a:link { color: red; font-size: 18px; } a:visited { color:green; font-size: 28px; } 复制代码
效果如下:
visited支持的属性仅有:color/background-color/border-color三种
有兴趣的朋友可以自己验证看一下。
最后我们来提一下 target
,我就直接用 target
来写一个tab例子了,来看下面一段代码:
// html <div> <a href="#id1">链接一</a> <a href="#id2">链接二</a> </div> <div id="id1"></div> <div id="id2"></div> // css * { padding: 0; margin: 0; } a{ text-decoration: none; // 去掉 } #id1{ width: 200px; height: 200px; border: 1px solid black; } #id2{ width: 200px; height: 200px; border: 1px solid black; } :target { background-color: red; } 复制代码
效果如下:
关于这个伪类的话,说实话我用到的并不多,在复习的时候刚好看到了这个类,简单的做了一下知识的梳理。
二、 动态伪类
关于动态伪类呢,我的定义是: 需要用户主动操作才能触发的 。其中包含以下两种:
- hover:定义鼠标经过显示的样式
- active:定义鼠标单击激活时的样式 首先来一段代码:
// html <div> <a href="#id1">链接一</a> <a href="#id2">链接二</a> <span>我是三</span> </div> // css * { padding: 0; margin: 0; } a{ text-decoration: none; // 去掉 } a:hover{ color: green; } a:active { color: yellow } span:hover { color: green; } span:active { color: yellow } 复制代码
其效果如下:
hover
和
active
是所有的元素通用的,也就是
hover
和
active
伪类是支持所有元素的,而
a
元素却是支持所有的链接伪类和动态伪类的。 值得注意的一点是我在开发中遇到了这么一个问题,我们修改一下css代码:
a:hover{ color: green; } a:active { color: yellow } a:link { color: black; } a:visited { color: red; } 复制代码
效果如下:
爱恨原则
(love-hate)。意思呢是叫我们要把link和visited放在前面,hover和active放在后面,这样就能避免样式被覆盖了。
三、 表单伪类
接下来我们来谈一谈表单伪类,表单伪类听这个名称就知道是作用在表单里面的,我这里呢?主要来介绍4个常用的(据说现在还新增加了几个):
- enable:当表单元素未禁止时被触发
- disable:当表单元素禁止时被触发
- focus: 当表单元素被聚焦时触发
- checked:当表单元素被选择时触发 来看下面一段代码:
// html <input type="text" disabled> <input type="text"> // css input:enabled { background: red; } input:disabled { background: blue; } input:focus { background:yellow; } 复制代码
效果如下:
从上面的效果大概可以看出来 enable
、 disable
、 focus
的作用,我这里就不多加赘述了。
最后关于 checked
,我就用一个简单的demo来表现他们的作用吧。请看下面一段代码:
//html <label> <input type="radio" name="select"> <span></span> </label> <label> <input type="radio" name="select"> <span></span> </label> // css label { width: 100px; height: 100px; float: left; border: 1px solid yellow; overflow: hidden; } label span { display: flex; width: 100px; height: 100px } input { display: none; } input:checked + span { background:red; } 复制代码
效果如下:
四、 结构伪类
接下来我们介绍以下结构伪类,这个东西基本上在实际的开发中使用量会非常大,这里就分为两个模块来进行讲解:
- x-child
- x-of-type
4.1、 x-child属性
在x-child中呢?主要的属性有下列这几项:
- first-child
- last-child
- nth-child
- nth-last-child
- only-child 首先关于
first-child
和last-child
这两个属性,可以来看下面一段代码:
// html <div> <p>1</p> <p>2</p> <p>3</p> </div> //css div p:first-child{ background-color: red; } div p:last-child { background-color: green; } 复制代码
效果如下:
first-child
和
last-child
分别是作用于父元素的第一个子元素和最后一个子元素。
接下来我们介绍一下: nth-child
和 nth-last-child
,我就在上面的基础上面修改一下css代码:
div p:nth-child(1){ background-color: red; } div p:nth-last-child(1) { background-color: green; } 复制代码
效果如下:
nth-child
和
nth-last-child
分别是作用于父元素的第n个元素和作用于父元素的倒数第n个元素。值得我们注意的是,这两个属性的下标都要从1开始而不是从0。
最后来说一说 only-child
:关于这个伪类我是这样理解的:它相当于是一个组合选择器,他组合了 first-child
和 last-child
,来请看下面一段代码.
// html <div> <p>1</p> </div> // css div p:only-child{ background-color: red; } 复制代码
效果如下:
从上面的效果我么可以知道当一个父元素有且仅有一个子元素的时候,该伪类会被触发。
当然前面写的都是正常情况下,我们这里来整点异常情况:
- 当父元素不止一个子元素的时候,
only-child
伪类有没有作用呢?来看下面一段代码:
//css div p:only-child{ background-color: red; } //html <div> <p>1</p> <p>2</p> </div> 复制代码
效果如下:
only-child
将会失去效果。
- 当父元素的制定子元素不是预期元素时,会不会有效果呢?来看下面一段代码:
//html <div> <div></div> <p>1</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> </div> // css div p:first-child{ background-color: red; } 复制代码
效果如下:
div
的第一个子元素不是
p
的时候,
first-child
就会失效。同样
last-child
、
nth-child
、
nth-last-child
等
x-child
也是一样,这就不再赘述了。所以为了解决这个问题,
x-of-type
横空出世。
4.2、x-of-type属性
前文说了,为了弥补 x-child
的缺漏, x-of-type
横空处世,与 x-child
相同, x-of-type
也有五个伪类:
- first-of-type
- last-of-type
- nth-of-type
- nth-last-of-type
- only-of-type
首先来看 first-of-type
和 last-of-type
,先看下面一段代码:
// html <div> <div></div> <p>1</p> <p>2</p> <p>2</p> <p>2</p> <p>2</p> <div></div> </div> // css div p:first-of-type{ background-color: red; } div p:last-of-type { background-color: green; } 复制代码
效果如下:
first-of-type
和
last-of-type
会将父元素第指定元素给附加效果,这一定程度上弥补了
x-child
的弊端
然后再来简单的看一下 nth-of-type
和 nth-last-of-type
,修改一下前面的css:
div p:nth-of-type(1){ background-color: red; } div p:nth-last-of-type(1){ background-color: red; } 复制代码
效果如下:
nth-of-type
和
nth-last-of-type
也产生了效果,给父元素的指定n个子元素的附加属性。
最后来看点: only-of-type
:
//html <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <p></p> </ul> // css ul>p:only-of-type{ background: green; width:100px; height: 40px; } 复制代码
效果如下:
这里我们 当父元素的指定元素有且仅有一个的时候 就能生效。
####4.3、 我在开发的时候遇到的一个坑 前面说到了一个问题:如果 x-of-type
这么好用是不是我们就能无脑的用这个 而放弃 x-child
呢?关于这个问题,我就分享一个我在开发的时候遇到的一个坑吧来看下面一段代码:
//html <div> <div class="item">div</div> <p class="item">p</p> <h1 class="item">h1</span> <h2 class="item">h2</p> <h3 class="item">h3</p> <h4 class="item">h4</p> </div> // css div .item:first-of-type{ background-color: red; border: 1px solid black; } 复制代码
效果如下:
x-child
却能起作用。从这个坑能充分验证我前面的观点:
x-child
和 x-of-type
在使用的时候要分场景来选择的
。
五、 伪元素
接下来关于伪元素,这一块的内容不多,没什么比较想聊的,简单的概述一下吧:
- before
- after
- frist-letter
- frist-line
- selection
首先关于 before
和 after
我之前写过一篇文章封装了几个图标 有兴趣的可以去瞧瞧: 如何灵活的运用before和after
然后 first-letter
和 first-line
,请看下面一段代码:
//html <p>i am klivitam <br/>i love my country</p> // css p::first-letter{ color: red; font-size: 48px; } p::first-line{ font-weight: bold; } p::selection{ background: green; color: white; } 复制代码
效果如下:
这个意思的内容比较简单,从字面上就能看出来,我也不多加赘述了。
六、最后检查一下
最后我再仔细的翻阅了一下文档,我发现了还有:
- not
- empty
- root
- lang 这四个还没有讲到 首先关于
empty
和not
,来看下面一段代码:
// html <p>i am klivitam <br/>i love my country</p> <p></p> // css p { width: 200px; height: 60px; border: 1px solid; } p:not(:empty){ background: red; color:white; } 复制代码
效果如下:
由上面的现象我们可以出来: empty
伪类是当父元素没有子元素的时候触发的,而 not
则是不满足括号内的条件会触发的。
最后来看看遗漏的 root
和 lang
, root
是匹配元素e所在的文档的根元素,在HTML文档中,根元素是html;而 lang
是允许您为不同的语言定义特殊的规则。这两个属性我用的太少了,只晓得其意思,实在举不出例子来,并且感觉用网上的例子给大家演示这种操作很蠢,就不做这种蠢事儿了。。
#说在最后
好了,好了。本来想今天就把这三篇文章都写出来的。但是现在都已经12点了 第二篇文章都才刚出来。不多说了,我先睡波觉,明早起来继续写。明天还有一个选择器的优先级,敬请期待哦!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning in Action
Peter Harrington / Manning Publications / 2012-4-19 / GBP 29.99
It's been said that data is the new "dirt"—the raw material from which and on which you build the structures of the modern world. And like dirt, data can seem like a limitless, undifferentiated mass. ......一起来看看 《Machine Learning in Action》 这本书的介绍吧!