当前位置: 首页 > Web前端 > CSS

CSS伪类实例

时间:2023-03-31 12:05:24 CSS

说到伪类,这里的代码足以展示其神奇的用法。从简单到复杂,你可以清楚地看到伪类的许多使用方法。对于一些类似的功能,以其中一个为例::first-letter为第一个单词添加样式,这里以一个drop-firstletter示例来演示:

Chinaisthe中华文明之源

p:first-letter{display:block;向左飘浮;右边距:.2em;font-size:1.7em;}:first-line在段落首行添加样式:锦瑟
锦瑟无缘无故五十弦,一弦一柱思念。
庄生晓梦蝶,望帝春心托杜鹃。
月有泪海珠,蓝田日暖玉如烟。
这种感觉能化作记忆吗?它只是不知所措。

p:first-line{font-weight:bold;font-size:1.7em;}::selection(CSS3)设置选中文本的状态,或者上面的文本:.select::selection{background-color:yellowgreen;}:empty(CSS3)元素样式,内容为空
Ihavecontent
div{width:60px;高度:40px;背景色:浅灰色;margin:5px;}div:empty{background-color:darkgray;}如果中间没有内容,则使用href的值作为内容:a:empty:before{content:attr(href);}:focus元素获得焦点时的样式input[type="text"]:focus{border:1pxpurplesolid;box-shadow:0015pxblack;}:disabled:enabled(CSS3)禁用元素样式input:disabled{background-color:red;}input:enabled{background-color:yellow;}:checked:read-only:read-write:indeterminate:invalid:valid(CSS3)伪类描述:radio,checkboxandselectselectedincheckedinput:read-onlyinput,selectandtextareaelementswithreadonlyattribute(firefox不支持):read-writeinput,selectandtextareawritableelementswithoutthereadonlyattribute(firefoxdoesn'tsupportit):indeterminatedoesnothaveanyselectedradiogroup(firefoxdoesn'tsupport):invalidinputelementsthatfailformvalidation:validinputformvalidationpasses:

input[type=“checkbox”]:checked{outline:2pxsolidred;}input:read-only{background-color:yellow;}input:read-write{background-color:lightgreen;}input:indeterminate{outline:1pxsolidblue;}input[type="email"]:invalid{背景颜色:红色;color:#fff;}input[type="email"]:valid{background-color:#lightgreen;}:required:optional:in-range:out-of-range:default(CSS3)伪类描述:required具有required属性的input、select和textarea元素:optionaleditableinput、select和textarea元素没有required属性:legalinputwithin-range在指定范围内:out-of-rangenotLegalinput在指定范围内:defaultdefault样式(仅firefox支持)

:default{background-color:green;}input:required{background-color:yellow;}input:optional{background-color:orange;}input:in-range{background-color:lightgreen;}输入:超出范围{背景色:红色;color:white;}input:indeterminate{outline:1pxsolidblue;}:link:hover:active:visited:linkanchor点样式:鼠标悬停在元素(任意元素)上方时的悬停样式点击(任何元素element):访问鼠标点击的颜色(任意元素)百度a:链接{文字装饰:无;字体粗细:粗体;颜色:黑色;}a:悬停{文字装饰:下划线;颜色:蓝色;}a:活动{文字装饰:无;color:purple;}a:visited{text-decoration:none;color:darkgray;}:first-child:last-child:nth-child:not:only-child:first-child第一个元素style:last-child最后一个元素style:nth-child(n)第n个元素的样式(这个也能玩点花样):not(selector)不符合选择器selector的样式ulli{list-style:none;背景颜色:天蓝色;白颜色;文本对齐:居中;宽度:100px;高度:20px;边距:5px自动;float:left;}ulli:first-child{color:blue;}ulli:last-child{color:red;}ulli:nth-child(3){color:darkgreen;}ulli:not([name="except"]){font-style:italic;}/*下面实现偶数部分的样式变化*/ulli:nth-child(2n){/*2n+1可以表示奇数*/background-color:blue;/*下面实现连续部分样式变化*/ulli:nth-child(n+3):nth-child(-n+8){background-color:blue;}/*:nth-child(n+3)表示第3个以后的元素:nth-??child(-n+8)表示第8个之前的元素,所以这里选择第3到第8个元素*/:only-child放在下一节和:only-of-type比较解释:first-of-type:last-of-type:nth-of-type:nth-last-of-type:only-of-type除了CSS3:first-of-type:last-of-type:nth-of-type(n):nth-last-of-type(n)用法与上面类似,效果相同,其中:nth-last-of-type(n)表示最后第n个元素type和child这两种类型有如下区别://伪代码

div1中唯一的子元素h3

div2中第一个h3

div2中第一个h4

div2中的第二个h3

div3中的第一个h3

div3中第一个h4

div3

中的第二个h3

第二个h4indiv3

第三个h3indiv3

thirdh4indiv3

h3:nth-of-type(2){color:#00f;}h4:nth-child(4){颜色:#ff0;}h4:only-of-type{background-color:#ff0;}h3:only-child{background-color:#f0f;}在上面的例子中,还有:only-child和:only-of-CSS3中的type有两个伪类,代表单个元素,即前后没有相同的元素。具体效果如下图所示::target:target选择器可以用来选择当前activity的目标元素(即url中的锚元素)。接下来使用target制作tab样式(点击切换)Label1标签二标签三内容一内容二内容三
#tab.titlea{float:left;文字修饰:无;宽度:100px;高度:35px;行高:35px;文本对齐:居中;边框:1px纯黑色;}#tab.标题a:nth-child(n+2){border-left:none;}#tab.content{clear:both;位置:相对;}#tab.contentli{width:302px;高度:300px;边框:1px纯黑色;边框顶部:无;背景色:白色;位置:绝对;左:0;top:0;}#tab.contentli:target{z-index:1;}:before:after这是最值得一提的,可以在元素前后添加内容。当然,你也可以添加块元素。这个块有无限的变化。下面举几个例子:首当其冲的就是清除浮动的1
2
3
4*{margin:0;填充:0;}.float{宽度:40px;高度:40px;背景色:蓝色;边距:5px;向左飘浮;颜色:黄色;}.clr:之后{内容:“”;明确:两者;溢出:隐藏;高度:0;display:block;}自动计算

hello

world!

world!

world!

世界!

世界!

你好

世界!

世界!

世界!

world!

world!

h3{counter-increment:myNumH3;counter-reset:myNumH4;}h3:before{content:'第'counter(myNumH3)'章';颜色:#08f;}h4{反增量:myNumH4;margin-left:20px;}h4:before{content:'Section'counter(myNumH3)'-'counter(myNumH4)'';color:#00f;}图片右上角标签:new.new,img{width:300px;高度:200px;}.newspan{位置:相对;显示:块;字母间距:2px;字体大小:20px;宽度:30px;高度:20px;白颜色;顶部:-190px;左:262px;z-索引:1;(45deg);}.new:after{内容:"";显示:块;字体大小:20px;宽度:0;高度:0;border:solid35px透明;边框顶部颜色:红色;右边框颜色:红色;职位:相对;顶部:-224px;left:230px;}按钮点击展开:button.button{width:80px;高度:40px;border:2pxsoliddimgray;背景色:道奇蓝;颜色:#202020;文本对齐:居中;行高:40px;字体大小:20px;margin:20px;}.enlarge:after{内容:“”;显示:块;高度:60px;宽度:100px;位置:相对;顶部:-50px;左:-10px;背景色:rgba(100,100,100,0.4);点击区域,应该是透明的*/}按钮右上角提示:button这是一个对话框.dialog{background-color:粉色的;边框:2px纯灰色;文本对齐:居中;行高:80px;宽度:150px;高度:80px;margin-bottom:40px;}.dialog:after{content:"";显示:块;背景:继承;边界:继承;边界顶部:0;左边框:0;位置:相对;宽度:30px;高度:30px;顶部:-15px;左:20px;变换:rotate(45deg);}幸好,我自己写的Fu.luck{浮动:左;宽度:100px;高度:100px;边距:30px;margin-bottom:45px;}.luckspan{颜色:金色;位置:相对;字体大小:4em;宽度:70px;高度:70px;变换:旋转(180度);显示:块;顶部:-80px;left:16px;}.luck:before{content:"";显示:块;宽度:100px;高度:100px;红色;transform:rotate(45deg);}不足之处请多多指点