首页 / CSS / css 基础---选择器
css 基础---选择器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了css 基础---选择器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2716字,纯文字阅读大概需要4分钟。
内容图文
1.css基础
selector {property: value}
eg:
h1 {color:red; font-size:14px;}
p { text-align: center; color: black; font-family: arial; }
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>h1,h2,h3,h4,h5,h6 { color: green; }
希望列表中的 strong 元素变为斜体字,而不是通常的粗体字,可以这样定义一个派生选择器:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>li strong { font-style: italic; font-weight: normal; }
id 选择器以 "#" 来定义。
下面的两个 id 选择器,第一个可以定义元素的颜色为红色,第二个定义元素的颜色为绿色:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>#red {color:red;} <CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>#green {color:green;}
id 选择器和派生选择器
在现代布局中,id 选择器常常用于建立派生选择器。
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>#sidebar p { font-style: italic; text-align: right; margin-top: 0.5em; }
类选择器以一个点号显示:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>.center {text-align: center}
和 id 一样,class 也可被用作派生选择器:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>.fancy td { color: #f60; background: #666; }
对带有指定属性的 HTML 元素设置样式。
可以为拥有指定属性的 HTML 元素设置样式,而不仅限于 class 和 id 属性。
注释:只有在规定了 !DOCTYPE 时,IE7 和 IE8 才支持属性选择器。在 IE6 及更低的版本中,不支持属性选择。
属性选择器
下面的例子为带有 title 属性的所有元素设置样式:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>[title] { color:red; }
下面的例子为 title="W3School" 的所有元素设置样式:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>[title=W3School] { border:5px solid blue; }
下面的例子为包含指定值的 title 属性的所有元素设置样式。适用于由空格分隔的属性值:
<CODE style=‘margin: 0px; padding: 0px; border: 0px currentColor; color: rgb(0, 0, 221); font-family: "Courier New", Courier, monospace;‘>[title~=hello] { color:red; }
CSS 选择器参考手册
选择器 | 描述 |
---|---|
[attribute] | 用于选取带有指定属性的元素。 |
[attribute=value] | 用于选取带有指定属性和值的元素。 |
[attribute~=value] | 用于选取属性值中包含指定词汇的元素。 |
[attribute|=value] | 用于选取带有以指定值开头的属性值的元素,该值必须是整个单词。 |
[attribute^=value] | 匹配属性值以指定值开头的每个元素。 |
[attribute$=value] | 匹配属性值以指定值结尾的每个元素。 |
[attribute*=value] |
匹配属性值中包含指定值的每个元素。 |
原文:http://www.cnblogs.com/luyujie/p/3558850.html
内容总结
以上是互联网集市为您收集整理的css 基础---选择器全部内容,希望文章能够帮你解决css 基础---选择器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。