CSS选择器
来源:互联网 发布:申请淘宝商家披露信息 编辑:程序博客网 时间:2024/06/05 04:56
一、属性选择器:
E[att^="val"] 选择匹配元素E,定义了属性att,其属性值以val开头的任何字符串。E[att$="val"] 选择匹配元素E,定义了属性att,其属性值以val结尾的任何字符串。E[att*="val"] 选择匹配元素E,定义了属性att,其属性值包含val的任何字符串。
<a href="xxx.pdf">我链接的是PDF文件</a><a href="#" class="icon">我类名是icon</a><a href="#" title="我的title是more">我的title是more</a>a[class^=icon]{ background: green; color:#fff;}定义了class属性,这个属性以icon开头a[href$=pdf]{ background: orange; color: #fff;}a[title*=more]{ background: blue; color: #fff;}
二、伪类选择器——root
:root选择器,从字面上我们就可以很清楚的理解是根选择器,他的意思就是匹配元素E所在文档的根元素。
“:root”选择器等同于元素,简单点说:
:root{background:orange}html {background:orange;}得到的效果等同。
通用选择器是功能最强大的选择器,它使用一个(*)号指定,它的作用是匹配html中所有标签元素
三、伪类选择器——not
:not选择器称为否定选择器
给表单中除submit按钮之外的input元素添加红色边框input:not([type="submit"]){ border:1px solid red;}<form action="#"> <div> <label for="name">账户:</label> <input type="text" name="name" id="name" placeholder="转户" /> </div> <div> <label for="name">密码:</label> <input type="text" name="name" id="name" placeholder="密码" /> </div> <div> <input type="submit" value="提交" /> </div></form>
四、伪类选择器——empty
:empty选择器表示的就是空。用来选择没有任何内容的元素,这里没有内容指的是一点内容都没有,哪怕是一个空格。
<p>我是一个段落</p><p> </p><p></p>文档中有三个段落p元素,你想把没有任何内容的P元素隐藏起来。我们就可以使用“:empty”选择器来控制p{ background: orange; min-height: 30px;}p:empty { display: none;}
五、伪类选择器——target
:target选择器称为目标选择器,用来匹配文档(页面)的url的某个标志符的目标元素。
<h2><a href="#brand">Brand</a></h2><div class="menuSection" id="brand"> content for Brand</div><h2><a href="#jake">Brand</a></h2><div class="menuSection" id="jake"> content for jake</div><h2><a href="#aron">Brand</a></h2><div class="menuSection" id="aron"> content for aron</div>点击链接目标选择器生效#brand:target { background: orange; color: #fff;}#jake:target { background: blue; color: #fff;}#aron:target { background: red; color: #fff;}
六、伪类选择器——first-child
“:first-child”选择器表示的是选择父元素的第一个子元素的元素E。简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素。
<ol> <li><a href="##">Link1</a></li> //数字1变为红色 <li><a href="##">Link2</a></li> <li><a href="##">link3</a></li></ol>ol > li{ font-size:20px; font-weight: bold; margin-bottom: 10px;}**子选择器**,即大于符号(>),用于选择指定标签元素的**第一代**子元素。ol a { font-size: 16px; font-weight: normal;}**后代选择器**是作用于**所有子后代**元素。ol > li:first-child{ color: red;}通过“:first-child”选择器,定位列表中的第一个列表项,并将序列号颜色变为红色。
七、伪类选择器——nth-child(n)
“:nth-child(n)”选择器用来定位某个父元素的一个或多个特定的子元素。其中“n”是其参数,而且可以是整数值(1,2,3,4),也可以是表达式(2n+1、-n+5)和关键词(odd、even),但参数n的起始值始终是1,而不是0。也就是说,参数n的值为0时,选择器将选择不到任何匹配的元素。
<ol> <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> <li>item5</li> <li>item6</li> <li>item7</li> <li>item8</li> <li>item9</li> <li>item10</li></ol>ol > li:nth-child(2n){ background: orange;}通过“:nth-child(n)”选择器,并且参数使用表达式“2n”,将偶数行列表背景色设置为橙色。
“:nth-last-child(n)”选择器和前面的“:nth-child(n)”选择器非常的相似,只是这里多了一个“last”,所起的作用和“:nth-child(n)”选择器有所区别,从某父元素的最后一个子元素开始计算,来选择特定的元素
八、first-of-type选择器
“:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子元素。
<div class="wrapper"> <div>我是一个块元素,我是.wrapper的第一个子元素</div> <p>我是一个段落元素,我是不是.wrapper的第一个子元素,但是他的第一个段落元素</p> <p>我是一个段落元素</p> <div>我是一个块元素</div></div>/*我要改变第一个段落的背景为橙色*/.wrapper > p:first-of-type { background: orange;}
“:last-of-type”选择器和“:first-of-type”选择器功能是一样的,不同的是他选择是父元素下的某个类型的最后一个子元素。
“:nth-of-type(n)”选择器和“:nth-child(n)”选择器非常类似,不同的是它只计算父元素中指定的某种类型的子元素。当某个元素中的子元素不单单是同一种类型的子元素时,使用“:nth-of-type(n)”选择器来定位于父元素中某种类型的子元素是非常方便和有用的。
“:nth-last-of-type(n)”选择器和“:nth-of-type(n)”选择器是一样的,选择父元素中指定的某种子元素类型,但它的起始方向是从最后一个子元素开始,而且它的使用方法类似于上节中介绍的“:nth-last-child(n)”选择器一样。
九、only-child选择器
“:only-child”选择器选择的是父元素中只有一个子元素,而且只有唯一的一个子元素。也就是说,匹配的元素的父元素中仅有一个子元素,而且是一个唯一的子元素。
<div class="post"> <p>我是一个段落</p> <p>我是一个段落</p></div><div class="post"> <p>我是一个段落</p></div>.post p { background: green; color: #fff; padding: 10px;}.post p:only-child { background: orange;}
“:only-of-type”选择器用来选择一个元素是它的父元素的唯一一个相同类型的子元素。这样说或许不太好理解,换一种说法。
十、enabled选择器
在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等。
<form action="#"> <div> <label for="name">Text Input:</label> <input type="text" name="name" id="name" placeholder="可用输入框" /> </div> <div> <label for="name">Text Input:</label> <input type="text" name="name" id="name" placeholder="禁用输入框" disabled="disabled" /> </div></form> input[type="text"]:enabled { background: #ccc; border: 2px solid red;}
“:disabled”选择器刚好与“:enabled”选择器相反,用来选择不可用表单元素。
十一、checked选择器
在CSS3中,我们可以通过状态选择器“:checked”配合其他标签实现自定义样式。而“:checked”表示的是选中状态。
<form action="#"> <div class="wrapper"> <div class="box"> <input type="checkbox" checked="checked" id="usename" /><span>√</span> </div> <lable for="usename">我是选中状态</lable> </div> <div class="wrapper"> <div class="box"> <input type="checkbox" id="usepwd" /><span>√</span> </div> <label for="usepwd">我是未选中状态</label> </div></form> .box input { opacity: 0; position: absolute; top:0; left:0;}.box span { position: absolute; top: -10px; right: 3px; font-size: 30px; font-weight: bold; font-family: Arial; -webkit-transform: rotate(30deg); transform: rotate(30deg); color: orange;}input[type="checkbox"] + span { opacity: 0;}相邻兄弟选择器 可选择紧接在另一元素后的元素,且二者有相同父元素。input[type="checkbox"]:checked + span { opacity: 1;//透明度}
十二、::selection选择器
::selection”伪元素是用来匹配突出显示的文本(用鼠标选择文本时的文本)。浏览器默认情况下,用鼠标选择网页文本是以“深蓝的背景,白色的字体”显示的
<p>“::selection”伪元素是用来匹配突出显示的文本。浏览器默认情况下,选择网站文本是深蓝的背景,白色的字体,</p>::-moz-selection { background: red; color: green;}::selection { background: red; color: green;}
十三、read-only选择器
“:read-only”伪类选择器用来指定处于只读状态元素的样式。简单点理解就是,元素中设置了“readonly=’readonly’
<form action="#"> <div> <label for="name">姓名:</label> <input type="text" name="name" id="name" placeholder="大漠" /> </div> <div> <label for="address">地址:</label> <input type="text" name="address" id="address" placeholder="中国上海" readonly="readonly" /> </div></form> form { width: 300px; padding: 10px; border: 1px solid #ccc; margin: 50px auto;}form > div { margin-bottom: 10px;}input[type="text"]{ border: 1px solid orange; padding: 5px; background: #fff; border-radius: 5px;}input[type="text"]:-moz-read-only{ border-color: #ccc;}input[type="text"]:read-only{ border-color: #ccc;}
“:read-write”选择器刚好与“:read-only”选择器相反,主要用来指定当元素处于非只读状态时的样式。
- CSS选择器,jQuery选择器
- jQuery选择器 CSS选择器
- css选择器-基本选择器
- css选择器-属性选择器
- CSS选择器选择器
- CSS选择器-属性选择器
- css选择器
- css选择器
- css选择器
- Css选择器!
- CSS选择器
- css选择器
- CSS选择器
- CSS选择器
- CSS选择器
- CSS选择器
- css选择器
- css选择器
- php __FILE__ __DIR__魔术常量的使用
- fragment使用时 Can not perform this action after onSaveInstanceState
- 基于TCP的RPC
- Maven私服问题汇总
- Oracle创建表空间、创建用户以及授权
- CSS选择器
- 判断一个文件格式是否是日期格式
- Android-BitmapFactory.decodeResource OOM问题
- MySQL语句基础之数据库约束
- angularJS 练习
- ElasticSearch 系列(3) 安装中文分词器
- 欧拉定理
- Imageloader listview
- 安卓线程池和任务