总结css定位,选择器

一、选择器

1、普通选择器
通用选择器     *     *     选择所有元素。
类选择器     .class     .message     选择 class="intro" 的所有元素。
id选择器     #id     #head     选择 id="firstname" 的所有元素。
元素选择器     el     p     选择所有 <p> 元素。
选择器分组     el,el     div,p     选择所有 <div> 元素和所有 <p> 元素。
后代选择器     el el     div p     选择 <div> 元素内部的所有 <p> 元素。
子元素选择器     el > el     div>p     选择 <div> 的第一子代的 所有<p> 元素。
相邻兄弟选择器     el + el     div+p     选择与<div>同级且紧接在其后的第一个 <p> 元素
2、属性选择器
[attribute]     [target]     选择带有 target 属性所有元素
[attribute=value]     [target=_blank]     选择 target="_blank" 的所有元素
[attribute~=value]     [title~=flower]     选择 title 属性包含单词 "flower" 的所有元素
[attribute¦=value]     [lang¦=en]     选择 lang 属性值以 "en" 开头的所有元素。
[attribute^=”value”]     [abc^=”def”]     选择 abc 属性值以 “def” 开头的所有元素
[attribute$=”value”]     [abc$=”def”]     选择 abc 属性值以 “def” 结尾的所有元素
[attribute*=”value”]     [abc*=”def”]     选择 abc 属性值中包含子串 “def” 的所有元素
3、伪类
:link     a:link     选择所有未被访问的链接
:visited     a:visited     选择所有已被访问的链接
:active     a:active     选择正在被点击的活动链接
:hover     a:hover     选择鼠标指针位于其上的链接
:focus     input:focus     选择获得焦点的 input 元素
:lang(language)     p:lang(it)     选择带有以 "it" 开头的 lang 属性值的每个 <p> 元素

注:link、visited、active、hover的顺序,为LoVe HAte
4、伪元素
类型     eg     描述
:before     p:before     在每个 <p> 元素的内容之前插入内容
:after     p:after     在每个 <p> 元素的内容之后插入内容
:first-letter     p:first-letter     选择每个 <p> 元素的首字母
:first-line     p:first-line     选择每个 <p> 元素的首行
:first-child     p:first-child     选择属于父元素的第一个子元素的每个 <p> 元素
el1~el2     p~ul     选择前面有 <p> 元素的每个 <ul> 元素。
:first-of-type     p:first-of-type     选择属于其父元素的首个 <p> 元素的每个 <p> 元素。
:last-of-type     p:last-of-type     选择属于其父元素的最后 <p> 元素的每个 <p> 元素。
:only-of-type     p:only-of-type     选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。
:only-child     p:only-child     选择属于其父元素的唯一子元素的每个 <p> 元素。
:nth-child(n)     p:nth-child(2)     选择属于其父元素的第二个子元素的每个 <p> 元素。
:nth-last-child(n)     p:nth-last-child(2)     同上,从最后一个子元素开始计数。
:nth-of-type(n)     p:nth-of-type(2)     选择属于其父元素第二个 <p> 元素的每个 <p> 元素。
:nth-last-of-type(n)     p:nth-last-of-type(2)     同上,但是从最后一个子元素开始计数。
:last-child     p:last-child     选择属于其父元素最后一个子元素每个 <p> 元素。
:root     :root     选择文档的根元素。
:empty     p:empty     选择没有子元素的每个 <p> 元素(包括文本节点)。
:target     #news:target     选择当前活动的 #news 元素。
:enabled     input:enabled     选择每个启用的 <input> 元素。
:disabled     input:disabled     选择每个禁用的 <input> 元素
:checked     input:checked     选择每个被选中的 <input> 元素。
:not(selector)     :not(p)     选择非 <p> 元素的每个元素。
::selection     ::selection     选择被用户选取的元素部分。

二、定位

1)属性定位

1.css可以通过元素的id、class、标签这三个常规属性直接定位到

2.如下是百度输入框的的html代码:

<input id="kw" class="s_ipt" type="text" autocomplete="off" maxlength="100" name="wd"/>

3.css用#号表示id属性,如:#kw

4.css用.表示class属性,如:.s_ipt

5.css直接用标签名称,无任何标示符,如:input

2)其它属性

1.css除了可以通过标签、class、id这三个常规属性定位外,也可以通过其它属性定位

2.以下是定位其它属性的格式
[name=wd] [autocomplete='off'][maxlength='255']

3)标签

css页可以通过标签与属性的组合来定位元素
input.s_ipt input#kw input[id='kw']

4)层级关系

//form的id属性
form#form>span>input
//form的class属性
form.fm>span>input

5)索引

css也可以通过索引nth-child(1)来定位子元素,直接翻译过来就是第几个小孩
总结:选择标签后,找第几个小孩即可
Select控件第三个Opel
#select>select>option:nth-child(3)
CheckBox第一个Volvo
#checkbox>input:nth-child(1)

CheckBox第二个Saab
#checkbox>input:nth-child(4)
RadioBox第二个Saab
#radio>input:nth-child(4)
通过索引nth-of-type(2)来定位子元素,按照分类指定

选择select的saab
#select>select>option:nth-of-type(2);

选择 id 为 radio 的 div 下的第 1 个子节点
div#radio>input:nth-of-type(4)+label

选择id 为radio 的div 下的第4 个input 节点之后挨着的 label
节点
div#radio>input:nth-of-type(4)~label

转载于:https://www.cnblogs.com/TS-Prime/p/10828293.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值