css选择器
来源:互联网 发布:淘宝买苹果7手机可靠吗 编辑:程序博客网 时间:2024/05/29 18:22
css选择器是css的重中之重,所以总结如下:
一共可分为六大类:
1>元素选择器:文档的元素就是最基本的选择器
eg:html{ color:red;} h1{color:red;}p{color:red}
2>id选择器::允许以一种独立于文档元素的方式来指定样式
eg:#intro{color:red}
3>class选择器:选择属于同一类的元素
eg:.special{color:red;}
4>后代选择器:选择作为某元素的后代元素
eg:<ul>
<li>List item 1
<ol><li>List item 1-1</li><li>List item 1-2</li>
<li>List item 1-3<ol>
<li>List item 1-3-1</li> <li>List item <em>1-3-2</em></li>
<li>List item 1-3-3</li></ol>
</li> <li>List item 1-4</li></ol>/li><li>List item 2</li><li>List item 3</li></ul>
css样式: ul em{color:red; font-weight:bold;}
5>子元素选择器:与后代元素相比,子元素选择器只能作为某元素子元素的元素
eg:<h1>This is <strong>very</strong><strong>very</strong> important.</h1>
<h1>This is <em>really <strong>very</strong></em>important.</h1>
css: h1 > strong{color:red;}
这个规则会把第一个h1下面的两个strong变为红色,但第二个h1中的strong不受影响
This is very veryimportant.
This is really veryimportant.
6>组合选择器:如果需要几个元素或class等等都添加一个样式,就需要组合原则器
eg: body, h2, p, table, th, td, pre, strong, em{color:gray;}
7>相邻兄弟选择器:用于定位同一个父元素下某个元素之后的元素
请记住:用一个结合符只能选择两个相邻兄弟中的第二个元素,请看下面例子
eg: <div>
<ul><li>List item 1</li><li>List item 1</li><li>List item 1</li></ul>
<ol><li>List item 1</li><li>List item 1</li><li>List item 1</li></pl>
</div>
li + li { font-weight: bold;}
- List item 1
- List item 2
- List item 3
- List item 1
- List item 2
- List item 3
8>属性类选择器:可以根据某个属性是否存在或属性的值来寻找元素,因此能够实现某些非常有意思和强大的效果
[attr] 用于选取带有指定属性的元素
[attr = value] 用于选取带有指定属性和值的元素
[attr ~= value] 用于选取属性中包含指定词汇的元素
[attr |= value] 用于选取以指定值开头的属性值的元素,该值必须是整个单词
[attr ^= value] 匹配属性值以指定开头的每个元素
[attr $= value] 匹配属性值以指定结尾的每个元素
[atte *= value] 匹配属性值中包含指定值的每个元素
eg:<style type="text/css">
h2[title]/*h2元素有title属性的元素才可以应用改样式*/
{
color:red;
}
</style>
<body>
<h1>可以应用样式:</h1>
<h2 title="Hello world">Hello world</h2>
<a title="W3School" href="http://w3school.com.cn">W3School</a><hr />
<h1>无法应用样式:</h1>
<h2>Hello world</h2>
<a href="http://w3school.com.cn">W3School</a>
可以应用样式:
Hello world
W3School
无法应用样式:
Hello world
W3School
p:first-child
{
background-color:yellow;
}
</style>
<p>这个段落是其父元素(body)的首个子元素。</p>
<h1>欢迎访问我的主页</h1>
<p>这个段落不是其父元素的首个子元素。</p
<div>
<p>这个段落是其父元素(div)的首个子元素。</p>
<p>这个段落不是其父元素的首个子元素。</p>
</div>
</body>
background: orange;
}
- CSS选择器,jQuery选择器
- jQuery选择器 CSS选择器
- css选择器-基本选择器
- css选择器-属性选择器
- CSS选择器选择器
- CSS选择器-属性选择器
- css选择器
- css选择器
- css选择器
- Css选择器!
- CSS选择器
- css选择器
- CSS选择器
- CSS选择器
- CSS选择器
- CSS选择器
- css选择器
- css选择器
- Android初试--Android中的文件操作之ROM操作(1)
- activiti 配置(一)
- 极光推送接入-客户端
- 非常好用的Xcode插件(持续更新)
- as导入github项目
- css选择器
- Java多线程面试问题集锦
- Windows上使用ionic开发IOS程序
- eval —— javascript 永久的忌讳
- Spark API(1) 构建hadoop 集群
- jQuery 序列化表单数据 serialize() serializeArray()
- iOS大牛博客集
- Java程序的路径问题
- 简单制作漂亮的照片拼贴文字效果