[CSS]CSS/CSS3选择器
来源:互联网 发布:网络斗牛赌博作弊器 编辑:程序博客网 时间:2024/06/14 00:30
- CSS选择器
- 标签选择器
- id选择器
- 类选择器
- 后代选择器
- 交集选择器
- 并集选择器
- 通配符
- 伪类选择器
- after和before选择器
- CSS3选择器
- 儿子选择器
- 序选择器
- 下一个兄弟选择器
CSS选择器
标签选择器
<!DOCTYPE html><html><head> <title>我是页面</title> <meta charset="utf-8"> <style type="text/css"> p{ color: red; } </style></head><body> <p>哈哈</p> <p>呵呵</p> <p>嘿嘿</p></body></html>
所有的p标签的文字颜色,都变为红色
id选择器
<!DOCTYPE html><html><head> <title>我是页面</title> <meta charset="utf-8"> <style type="text/css"> #haha{ color: red; } </style></head><body> <p id="haha">哈哈</p> <p id="hehe">呵呵</p> <p id="heihei">嘿嘿</p></body></html>
指定id的字体颜色变为红色,选择器使用#来选择指定id
类选择器
<!DOCTYPE html><html><head> <title>我是页面</title> <meta charset="utf-8"> <style type="text/css"> .important{ color: red; } .error{ text-decoration: underline; } .special{ font-size: 20px; } </style></head><body> <p class="error important">哈哈</p> <p class="error special">呵呵</p> <p class="special important">嘿嘿</p></body></html>
最重要的选择器,不建议使用id选择器,id选择器主要供给给js使用。
后代选择器
<!DOCTYPE html><html><head> <title>我是页面</title> <meta charset="utf-8"> <style type="text/css"> .oneClass p .twoClass{ color: red; } </style></head><body> <ul class="oneClass"> <li>我是大帅锅 <p>我真的真的好帅</p></li> <li>我是大帅锅 <p>我真的真的 <span class="twoClass">好帅</span> </p></li> <li>我是大帅锅 <p>我真的真的好帅</p></li> </ul> <ul> <li>我是大帅锅 <p>我真的真的好帅</p></li> <li>我是大帅锅 <p class="twoClass">我真的真的好帅</p></li> <li>我是大帅锅 <p>我真的真的好帅</p></li> </ul></body></html>
空格 代表后代(在标签内的标签,不一定是子标签),其中.oneClass p .twoClass代表将类oneClass中的p标签中的.twoClass类的颜色设置为红色
交集选择器
<!DOCTYPE html><html><head> <title>test</title> <meta charset="utf-8"> <style type="text/css"> p.special{ color: red; } </style></head><body> <h3>我最帅</h3> <h3>我最帅</h3> <h3 class="special">我最帅</h3> <p class="special">我最帅</p> <p class="special">我最帅</p> <p>我最帅</p></body></html>
交集选择器用在这种情况下,当两个不同的标签处于不同的类时,要想为其中一个设置style,就需要交集选择器了
并集选择器
<!DOCTYPE html><html><head> <title>test</title> <meta charset="utf-8"> <style type="text/css"> h3,p{ color: red; } </style></head><body> <h3>我最帅</h3> <h3>我最帅</h3> <ul> <li>我最帅</li> </ul> <p>我最帅</p> <p>我最帅</p></body></html>
通配符*
用于初始化,效率极低。
*{}
伪类选择器
伪类选择器是一种设置标签状态效果的选择器,以:表示
常见的伪类选择器有:
a:link{ /*用户没有点击链接时的样式*/ color: blue; } a:visited{ /*用户访问后的样式*/ color: orange; } a:hover{ /*用户鼠标悬停的样式*/ color: green; } a:active{ /*用户鼠标按住链接时的样式*/ color: black; }
爱恨准则,必须按上面的顺序写,不然将会失效:love hate。
after和before选择器
p:after{ /*再每个p标签后面插入新的内容*/ content:"台词:"; /*为插入的内容设置样式*/ background-color:yellow; color:red; font-weight:bold;}p:before{ /*为每个p标签前插入新的内容*/}
CSS3选择器
儿子选择器
IE7开始兼容
<!DOCTYPE html><html><head> <title>test</title> <meta charset="utf-8"> <style type="text/css"> div>ul>li>p{ color: red; } </style></head><body> <h3>我最帅</h3> <div> <ul> <li>哈 哈 哈 哈</li> <li><p>红了!!!</p></li> <li>哈 哈 哈 哈</li> </ul> </div></body></html>
序选择器
IE8开始兼容
<!DOCTYPE html><html><head> <title>test</title> <meta charset="utf-8"> <style type="text/css"> ul li:first-child{ color: red; } </style></head><body> <h3>我最帅</h3> <div> <ul> <li>哈 哈 哈 哈</li> <li><p>红了!!!</p></li> <li>哈 哈 哈 哈</li> </ul> </div></body></html>
使用冒号后面加位置属性,比如first-child last-child
兼容替代:
<style type="text/css"> ul li.first{ color: red; } </style><ul> <li class="first">哈 哈 哈 哈</li> <li><p>红了!!!</p></li> <li class="last">哈 哈 哈 哈</li></ul>
下一个兄弟选择器
IE7开始兼容
<!DOCTYPE html><html><head> <title>test</title> <meta charset="utf-8"> <style type="text/css"> h3+p{ color: red; } </style></head><body> <h3>我最帅</h3> <p>我知道</p> <p>我知道</p> <p>我知道</p> <h3>我最帅</h3> <p>我知道</p> <p>我知道</p> <p>我知道</p></body></html>
h3 后面的紧挨着的第一个p兄弟变红
1 0
- [CSS]CSS/CSS3选择器
- CSS,CSS3选择器总结
- CSS/CSS3 选择器
- CSS和CSS3选择器
- CSS和CSS3选择器
- css以及css3中的选择器
- CSS系列:CSS3新增选择器
- CSS选择器--CSS3知识图谱
- Css3(01) CSS选择器
- css3学习 之 css选择器(css3 属性选择器)
- css 属性选择器学习+css3 混合模式+css background
- 强大的css选择器--css1-css3选择器总结
- css3学习 之 css选择器(结构性伪类选择器)
- 前端知识汇总-----CSS选择器和CSS3新增选择器
- css/css3
- [css] css3
- css+css3
- css选择器
- Android 实现视屏播放器、边播边缓存功能、外加铲屎(IJKPlayer)
- springMVC子容器使用spring父容器配置文件资源
- linux环境下mysql5.6的安装、配置、使用及常见问题解决办法
- 【linux系统】apt-get命令解析
- iOS——图片、界面的抖动
- [CSS]CSS/CSS3选择器
- 反射总结
- bootstrap常用的时间插件罗列及对比
- dyld: Library not loaded: /System/Library/Frameworks/UserNotifications.framework/UserNotifications
- 将 ICDAR 2015 的 Ground Truth 标注在图像数据上
- csrf攻击原理及防备
- DLL弹出窗口
- JavaScript中的引用类型Function类型学习心得
- Hadoop入门之Hadoop伪分布式环境搭建