CSS:选择器:引入方式/选择器及其组合

如果说我要给所有的p标签都定义一个颜色,怎么办?复制粘贴?

格言:程序员厌恶复制粘贴!

@想一想@:为什么?

使用CSS的方式
CSS设置可以放在三个地方:

内联式Inline(也叫行内样式):这是我们已经学过的,CSS的设置放置在HTML元素的style属性里面,这里面的设置只能作用于当前元素
嵌入式Embedding(也叫页内样式):CSS设置放置在一个标记块里面。这里面的设置能作用于整个当前页面。
可以写在页面任何地方,但我们通常把它放在head中:

外联式Linking(也叫外部样式):把CSS设置放置在一个独立的以.css为后缀的文件中,文件中直接写样式,不需要

选择器
回头想一想我们的需求:要给所有的p标签……我们怎么表示“所有的p标签”呢?在页内样式或外部样式中,使用“标签选择器”即可。

包括标签选择器,CSS一共提供了以下类型的选择器:

标签选择器:表示所有该标签均适用 {}中定义的样式:
/所有p标签都是蓝色文字/
p{
color:blue;
}
类选择器:
在元素中添加一个属性:属性名class,属性值自己设定,比如说red

源栈欢迎您!

然后,在

类和Id的名字,可查看:命名规则 - 汇总。

层叠和冲突
一个元素可以同时设置id、class和style三种属性,_

    <p id="primary" class="red" style="text-align:right"> 源栈欢迎您!</p>

一个class中还可以使用多个:

源栈

如果这三种选择器里设置的样式并不冲突,该元素可以同时(层叠)使用这些样式:

当出现冲突时(比如都设置了color,但color值不同),优先级选择从高到低(简化版):

行内样式
id选择器
类选择器
标签选择器
*选择器
演示:使用F12调试工具

也可能在一个/种选择器中,同一个项设置了多次

    p {
        color: red;
        color: green;  /* 最终生效 */
    }

写在后面的样式会覆盖前面的样式。

最后,你可能希望某个地方的设置永远不要被覆盖!这时候你可以使用 !important:

    p {
        /*永远无法被其他位置的设置更改,
          哪怕是 style="color: blue"*/
        color: red !important;
    }

组合

分组
逗号(,)" 分隔:p, .mark p和.mark都一样 ()

后代
空格" " 分隔:p .mark 在p里面的.mark受影响 ()

父子
大于符号">"分隔: p>.mark 在p的直接下一级里的.mark受影响()

兄弟
“+”:相邻
“~”:所有
直接写在一起:

标签和类:p.mark 标注了该类的元素
类和类:.mark.important (至少)同时使用了mark和important两个类
PS:学习这种复杂语法的意义:

自己组织整理代码
读懂别人这样组织整理的代码
所以,不要觉得“这个太复杂,算了,我不这样用就是了……”。你不这样用,别人会这样用!你除了自己写代码,还得去读别人的代码

如何选择/组织CSS内容?【常见面试题】
一般来说,CSS会是前端最混乱的部分。大致因为:

开发人员对其掉以轻心,不予重视
层叠冲突
牵一发而动全身

最简单粗暴的方式:只允许使用外部样式。以100%实现“HTML内容与CSS表现分离”,即HTML文件中完全没有CSS样式的设置,所有的CSS设置都在.css文件中完成。

但这种方式会带来几个副作用:

让CSS的命名变得更加困难。随着项目的扩大,命名将变得越来越复杂(因为你不能重名!)。但假如我可以直接使用行内样式,就根本不需要命名;使用页内样式,只需要当前页面不重名即可。
让阅读和维护变得更加困难。比如我看到一个元素被标记为class=“success”,知道它使用于.success的定义,然而.success定义在哪里的呢?css设置多了以后,找起来还是比较麻烦的;更麻烦的是,多个设置之间还会互相冲突……
所以,飞哥个人建议,如果一个样式,

仅仅适用于某一个特定元素,没有任何重用价值,就直接使用行内样式
仅仅在某一个特定页面中重用,就使用页内样式
需要在多个页面中重用,就使用外部样式
理解:从追求“纯粹”,到寻求“妥协”。
https://github.com/users/fghdfghdfg5/projects/952
https://github.com/users/fghdfghdfg5/projects/953
https://github.com/users/fghdfghdfg5/projects/954
https://github.com/users/fghdfghdfg5/projects/955
https://github.com/users/fghdfghdfg5/projects/956
https://github.com/users/fghdfghdfg5/projects/957
https://github.com/users/fghdfghdfg5/projects/958
https://github.com/users/fghdfghdfg5/projects/959
https://github.com/users/fghdfghdfg5/projects/960
https://github.com/users/fghdfghdfg5/projects/961
https://github.com/users/fghdfghdfg5/projects/962
https://github.com/users/fghdfghdfg5/projects/963
https://github.com/users/fghdfghdfg5/projects/964
https://github.com/users/fghdfghdfg5/projects/965
https://github.com/users/fghdfghdfg5/projects/966
https://github.com/users/fghdfghdfg5/projects/967
https://github.com/users/fghdfghdfg5/projects/968
https://github.com/users/fghdfghdfg5/projects/969
https://github.com/users/fghdfghdfg5/projects/970
https://github.com/users/fghdfghdfg5/projects/971
https://github.com/users/fghdfghdfg5/projects/972
https://github.com/users/fghdfghdfg5/projects/973
https://github.com/users/fghdfghdfg5/projects/974
https://github.com/users/fghdfghdfg5/projects/975
https://github.com/users/fghdfghdfg5/projects/976
https://github.com/users/fghdfghdfg5/projects/977
https://github.com/users/fghdfghdfg5/projects/978
https://github.com/users/fghdfghdfg5/projects/979
https://github.com/users/fghdfghdfg5/projects/980
https://github.com/users/fghdfghdfg5/projects/981
https://github.com/users/fghdfghdfg5/projects/982
https://github.com/users/fghdfghdfg5/projects/983
https://github.com/users/fghdfghdfg5/projects/984
https://github.com/users/fghdfghdfg5/projects/985
https://github.com/users/fghdfghdfg5/projects/986
https://github.com/users/fghdfghdfg5/projects/987
https://github.com/users/fghdfghdfg5/projects/988
https://github.com/users/fghdfghdfg5/projects/989
https://github.com/users/fghdfghdfg5/projects/990
https://github.com/users/fghdfghdfg5/projects/991
https://github.com/users/fghdfghdfg5/projects/992
https://github.com/users/fghdfghdfg5/projects/993
https://github.com/users/fghdfghdfg5/projects/994
https://github.com/users/fghdfghdfg5/projects/995
https://github.com/users/fghdfghdfg5/projects/996
https://github.com/users/fghdfghdfg5/projects/997
https://github.com/users/fghdfghdfg5/projects/998
https://github.com/users/fghdfghdfg5/projects/999
https://github.com/users/fghdfghdfg5/projects/1000
https://github.com/users/fghdfghdfg5/projects/1001
https://github.com/users/fghdfghdfg5/projects/1002
https://github.com/users/fghdfghdfg5/projects/1003
https://github.com/users/fghdfghdfg5/projects/1004
https://github.com/users/fghdfghdfg5/projects/1005
https://github.com/users/fghdfghdfg5/projects/1006
https://github.com/users/fghdfghdfg5/projects/1007
https://github.com/users/fghdfghdfg5/projects/1008
https://github.com/users/fghdfghdfg5/projects/1009
https://github.com/users/fghdfghdfg5/projects/1010
https://github.com/users/fghdfghdfg5/projects/1011
https://github.com/users/fghdfghdfg5/projects/1012
https://github.com/users/fghdfghdfg5/projects/1013
https://github.com/users/fghdfghdfg5/projects/1014
https://github.com/users/fghdfghdfg5/projects/1015
https://github.com/users/fghdfghdfg5/projects/1016
https://github.com/users/fghdfghdfg5/projects/1017
https://github.com/users/fghdfghdfg5/projects/1018
https://github.com/users/fghdfghdfg5/projects/1019
https://github.com/users/fghdfghdfg5/projects/1020
https://github.com/users/fghdfghdfg5/projects/1021
https://github.com/users/fghdfghdfg5/projects/1022
https://github.com/users/fghdfghdfg5/projects/1023
https://github.com/users/fghdfghdfg5/projects/1024
https://github.com/users/fghdfghdfg5/projects/1025
https://github.com/users/fghdfghdfg5/projects/1026
https://github.com/users/fghdfghdfg5/projects/1027
https://github.com/users/fghdfghdfg5/projects/1028
https://github.com/users/fghdfghdfg5/projects/1029
https://github.com/users/fghdfghdfg5/projects/1030
https://github.com/users/fghdfghdfg5/projects/1031
https://github.com/users/fghdfghdfg5/projects/1032
https://github.com/users/fghdfghdfg5/projects/1033
https://github.com/users/fghdfghdfg5/projects/1034
https://github.com/users/fghdfghdfg5/projects/1035
https://github.com/users/fghdfghdfg5/projects/1036
https://github.com/users/fghdfghdfg5/projects/1037
https://github.com/users/fghdfghdfg5/projects/1038
https://github.com/users/fghdfghdfg5/projects/1039
https://github.com/users/fghdfghdfg5/projects/1040
https://github.com/users/fghdfghdfg5/projects/1041
https://github.com/users/fghdfghdfg5/projects/1042
https://github.com/users/fghdfghdfg5/projects/1043
https://github.com/users/fghdfghdfg5/projects/1044
https://github.com/users/fghdfghdfg5/projects/1045
https://github.com/users/fghdfghdfg5/projects/1046
https://github.com/users/fghdfghdfg5/projects/1047
https://github.com/users/fghdfghdfg5/projects/1048
https://github.com/users/fghdfghdfg5/projects/1049
https://github.com/users/fghdfghdfg5/projects/1050
https://github.com/users/fghdfghdfg5/projects/1051
https://github.com/users/fghdfghdfg5/projects/1052
https://github.com/users/fghdfghdfg5/projects/1053
https://github.com/users/fghdfghdfg5/projects/1054
https://github.com/users/fghdfghdfg5/projects/1055
https://github.com/users/fghdfghdfg5/projects/1056
https://github.com/users/fghdfghdfg5/projects/1057
https://github.com/users/fghdfghdfg5/projects/1058
https://github.com/users/fghdfghdfg5/projects/1059
https://github.com/users/fghdfghdfg5/projects/1060
https://github.com/users/fghdfghdfg5/projects/1061
https://github.com/users/fghdfghdfg5/projects/1062
https://github.com/users/fghdfghdfg5/projects/1063
https://github.com/users/fghdfghdfg5/projects/1064
https://github.com/users/fghdfghdfg5/projects/1065
https://github.com/users/fghdfghdfg5/projects/1066
https://github.com/users/fghdfghdfg5/projects/1067
https://github.com/users/fghdfghdfg5/projects/1068
https://github.com/users/fghdfghdfg5/projects/1069
https://github.com/users/fghdfghdfg5/projects/1070
https://github.com/users/fghdfghdfg5/projects/1071
https://github.com/users/fghdfghdfg5/projects/1072
https://github.com/users/fghdfghdfg5/projects/1073
https://github.com/users/fghdfghdfg5/projects/1074
https://github.com/users/fghdfghdfg5/projects/1075
https://github.com/users/fghdfghdfg5/projects/1076
https://github.com/users/fghdfghdfg5/projects/1077
https://github.com/users/fghdfghdfg5/projects/1078
https://github.com/users/fghdfghdfg5/projects/1079
https://github.com/users/fghdfghdfg5/projects/1080
https://github.com/users/fghdfghdfg5/projects/1081
https://github.com/users/fghdfghdfg5/projects/1082
https://github.com/users/fghdfghdfg5/projects/1083
https://github.com/users/fghdfghdfg5/projects/1084
https://github.com/users/fghdfghdfg5/projects/1085
https://github.com/users/fghdfghdfg5/projects/1086
https://github.com/users/fghdfghdfg5/projects/1087

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值