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.
按需引入<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ant Design Vue 纯HTML项目示例</title> <!-- 引入Ant Design Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-vue@3.2.21/dist/antd.css"> <!-- 引入Ant Design X Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-x-vue@1.0.0/dist/antdx.css"> <!-- vue3引入 --> <script src="https://unpkg.com/dayjs/dayjs.min.js"></script> <script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekday.js"></script> <script src="https://unpkg.com/dayjs/plugin/localeData.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script> <!-- vue3 --> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <!-- antdv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.6/dist/antd.min.js"></script> <!-- antdxv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-x-vue@1.2.7/dist/index.umd.min.js"></script> </head> <body> <div id="app"></div> <script> const { createApp, ref, computed } = Vue; const { Button } = antd; const { Bubble, XProvider } = antdx; createApp({ template: ` <AXProvider :theme="{ algorithm: myThemeAlgorithm, }"> <div :style="{ padding: &#39;24px&#39;, backgroundColor: bgColor, }"> UMD <AXBubble content="hello bubble"></AXBubble> <AButton type="primary" @click="setLightTheme">Light</AButton> <AButton type="primary" @click="setDarkTheme">Dark</AButton> </div> </AXProvider> `, setup() { const { theme } = antd; const bgColor = ref("white"); const myThemeAlgorithm = ref(theme.defaultAlgorithm); const setLightTheme = () => { myThemeAlgorithm.value = theme.defaultAlgorithm; bgColor.value = "white"; }; const setDarkTheme = () => { myThemeAlgorithm.value = theme.darkAlgorithm; bgColor.value = "#141414"; }; return { myThemeAlgorithm, bgColor, setLightTheme, setDarkTheme }; } }) .use(XProvider) .use(Button) .use(Bubble) .mount("#app"); </script> <style> .container { max-width: 1200px; margin: 24px auto; padding: 0 16px; } .search-form { margin-bottom: 24px; padding: 16px; background-color: #f5f5f5; border-radius: 4px; } .user-table { margin-top: 16px; } .mb-6 { margin-bottom: 24px; } .mt-2 { margin-top: 8px; } </style> </body> </html>
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值