css selector简介

一.Tag selector
标签selecor ,也可以称为类型selector,它将对HTML页面中所有的该类型有效。

h1 {
   font-family:Arial, sans-serif;
   color:#CCCCFF;
}
表示所有h1标签都适用该CSS的rule,当然为了缩短代码,适用相同的rule的多个HTML Tag可以用逗号分隔开进行统一的修饰。如
DIV,H1 {
}

二.Class Selector
类选择器和HTML中的class相结合,应用到所有声明了该class的标签中。类选择器必须要以 “.”来开头。
对于

.title{
   color:#FF000;
   font-size:24px;
}
class selector可以分成独立和相关两种方式,独立的class selector是直接以.开头的,表示所有具有属性class的节点都是该CSS规则修饰的目标;相关的class selector常常跟Tag selector一起来修饰,表示指定的Tag 节点里面且具有对应的class的节点才是该CSS规则修饰的目标,如DIV.testClass {}

三.ID selector
ID选择器会将其syle应用在HTML中该ID的tag上。
对于

 #sidebar{
  float:left;
  margin-left:30px;
}

一个有意思的问题是如果在一个HTML中,有两个以上的tag用了同一个ID会怎么样呢?虽然并不是合法的HTML,但是大部分的游览器会忽略这个问题。这个时候ID选择器就像类选择器一样,只要哪个tag的使用了该ID,就会应用该style.
四.Descendent selector

这种选择器的的意思就是根据DOM树的祖先、父子关系来选择具体的元素。

如:

h1 strong {
     color:red;
}

将对<h1> This is homepage of <strong>liwenbing</strong></h1>中的strong的内容进行标红。

同样这种方式是可以组合前面的任何三种选择器:

h1 .intro a{
      color:yellow;
}

将对<h1> This is homepage of <strong >liwenbing </strong> who is <span class=”intro”><a href=”#”>lovely boy</a></span></h1>中的lovely boy标黄。

注意tag和类之间是否有空格是非常重要的。

有空格表示是应用该类的元素在tag之中,就像上面的例子;没有空格,则表示tag和class都标明的是同一个元素。

h1.intro a{   //注意h1和.intro之间没有空格
     color:blue;
}

对<h1 class=”intro”>This is homepage of <a href=”#” >liwenbing </a></h1>中的liwenbing是有效的。

再举一个和ID selector结合的例子:

#sidebar h2{
      font-size:1.2em;
}

上述style将对sidebar元素下面的所有h2进行应用。

五.Group selector

组选择器顾名思义就是对于一组selector应用同一中style,以逗号”,”隔开;组中的元素都可以是前面四中的任何一种。

h1,
p .intro,
#sidebar .title{
 color:#6F6F6F;
}

六.伪选择
伪选择器可以进一步地定义用户和页面的行为。最常用的就是对link的定义了;

a:hover{
  text-decoration:  none;
}
a:visited{
 color:blue;
}

当然关于link的除了上述两种,还有a:link,a:active;
其他还有很多的伪选择,例如现在在用的focus.下面这个CSS可以让input 有safari中的高亮效果(不过IE并不支持)。

 input:focus{
  border-color: #feca70;
}

其他伪选择器还有:before,:after,:firs-child.

 

 

Overview of CSS 2.1 selector syntax
Selector typePatternDescription
Universal*Matches any element.
TypeEMatches any E element.
Class.infoMatches any element whose class attribute contains the value info.
ID#footerMatches any element with an id equal to footer.
DescendantE FMatches any F element that is a descendant of an E element.
ChildE > FMatches any F element that is a child of an E element.
AdjacentE + F

Matches any F element immediately preceded by a sibling element E.

(E元素和F元素同属于一个parent并且E元素在F元素之前)

AttributeE[att]Matches any E element that has an att attribute, regardless of its value.
AttributeE[att=val]Matches any E element whose att attribute value is exactly equal to val.
AttributeE[att~=val]Matches any E element whose att attribute value is a list of space-separated values, one of which is exactly equal to val.
AttributeE[att|=val]Matches any E element whose att attribute has a hyphen-separated list of values beginning with val.
The :first-child pseudo-classE:first-childMatches element E when E is the first child of its parent.
The link pseudo-classesE:link
E:visited
Matches not yet visited (:link) or already visited (:visited) links.
The dynamic pseudo-classesE:active
E:hover
E:focus
Matches E during certain user actions.
The :language pseudo-classE:lang(c)Matches elements of type E that are in language c.
The :first-line pseudo-elementE:first-lineMatches the contents of the first formatted line of element E.
The :first-letter pseudo-elementE:first-letterMatches the first letter of element E.
The :before and :after pseudo-elementsE:before
E:after
Used to insert generated content before or after an element’s content.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS选择器(CSS selector)是用于选择HTML元素的一种模式匹配语法。它允许开发者通过指定元素的属性、关系和层级关系来选择需要样式化的元素。 以下是常见的CSS选择器类型: 1. 元素选择器:通过元素名称选择元素。例如,使用`p`选择所有段落元素。 2. 类选择器:通过类名选择元素。使用类选择器时,需要在类名前加上一个点(.)。例如,使用`.myclass`选择所有具有`myclass`类的元素。 3. ID选择器:通过元素的ID属性选择元素。使用ID选择器时,需要在ID名称前加上一个井号(#)。例如,使用`#myid`选择具有ID为`myid`的元素。 4. 属性选择器:通过元素的属性选择元素。例如,使用`[type="text"]`选择所有具有`type`属性且属性值为`text`的元素。 5. 伪类选择器:通过元素的状态或位置选择元素。例如,使用`:hover`选择鼠标悬停在上方的元素。 6. 后代选择器:选择特定元素的后代元素。例如,使用`.parent .child`选择类名为`child`且位于类名为`parent`的元素内部的子孙元素。 7. 直接子元素选择器:选择特定元素的直接子元素。例如,使用`.parent > .child`选择类名为`child`且直接位于类名为`parent`的元素内部的子元素。 8. 通用选择器:选择所有元素。使用通用选择器时,可以使用`*`。例如,使用`*`选择所有元素。 这只是CSS选择器的一小部分,还有更多选择器类型可用于根据不同的需求选择HTML元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值