网站描述修改,娱乐视频直播网站建设,2024年1月新冠高峰,输入文字自动生成图片CSS 伪类详解与示例
在日常的前端开发中#xff0c;CSS 伪类可以帮助我们非常精准地选择元素或其特定状态#xff0c;从而达到丰富页面表现的目的。本文将详细介绍以下伪类的使用#xff1a; 表单相关伪类 :checked、:disabled、:enabled、:in-range、:invalid、:optional、…CSS 伪类详解与示例
在日常的前端开发中CSS 伪类可以帮助我们非常精准地选择元素或其特定状态从而达到丰富页面表现的目的。本文将详细介绍以下伪类的使用 表单相关伪类 :checked、:disabled、:enabled、:in-range、:invalid、:optional、:out-of-range、:read-only、:read-write、:required、:valid 结构相关伪类 :empty、:first-of-type、:last-child、:last-of-type、:nth-child(n)、:nth-last-child(n)、:nth-of-type(n)、:nth-last-of-type(n)、:only-of-type、:only-child、:not(selector) 链接与用户交互状态伪类 :link、:visited、:active、:hover、:focus、:target 其他常用伪类 :root、:lang(language) 伪元素 ::first-letter、::first-line、::before、::after
下面我们通过具体实例来逐一说明它们的用法。 1. 表单相关伪类
1.1 :checked
用于选中已被选中的表单元素。比如下面的复选框和单选按钮在选中后文本颜色变为绿色。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:checked 示例/titlestyleinput:checked label {font-weight: bold;color: green;}/style
/head
bodyh3:checked 示例/h3input typecheckbox idcb1label forcb1复选框 1/labelbrinput typeradio namegroup idr1label forr1单选按钮 1/labelinput typeradio namegroup idr2label forr2单选按钮 2/label
/body
/html1.2 :disabled 与 :enabled
选择被禁用或启用的表单元素。可以通过不同的样式提示用户哪些输入项不能使用或可以交互。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:disabled 和 :enabled 示例/titlestyleinput:disabled {background-color: #f5f5f5;cursor: not-allowed;}input:enabled {border: 1px solid #66afe9;}/style
/head
bodyh3表单状态示例/h3input typetext placeholder启用的文本框brbrinput typetext placeholder禁用的文本框 disabled
/body
/html1.3 :in-range 与 :out-of-range
用于为指定范围内或超出范围的输入框设置样式。适用于例如数字输入、日期输入等场景。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:in-range 与 :out-of-range 示例/titlestyleinput:in-range {border-color: green;}input:out-of-range {border-color: red;}/style
/head
bodyh3取值范围验证/h3!-- 设置 min 和 max 属性 --input typenumber min1 max10 value5brbrinput typenumber min1 max10 value20
/body
/html1.4 :invalid 与 :valid
结合 HTML5 表单验证可以区分用户输入的合法性为合法或非法的状态加以不同样式提示。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:valid 与 :invalid 示例/titlestyleinput:valid {border-color: green;}input:invalid {border-color: red;}/style
/head
bodyh3表单验证/h3!-- 使用 required 限制必填pattern 限制格式 --input typeemail placeholder请输入有效的邮箱 required
/body
/html1.5 :optional, :read-only, :read-write, :required
这些伪类针对不同属性的输入框进行样式区分比如区分必填与选填、只读与可编辑
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:optional 与 :required 示例/titlestyleinput:optional {background-color: #e6f7ff;}input:required {background-color: #fff1f0;}/style
/head
bodyh3必填与选填示例/h3input typetext placeholder选填项brbrinput typetext placeholder必填项 required
/body
/html2. 结构相关伪类
2.1 :empty
选择没有任何子元素包括文本节点的元素。比如下面的 p 标签如果没有内部内容会被设置背景色。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:empty 示例/titlestylep:empty {background-color: #f9f9f9;border: 1px dashed #ccc;min-height: 50px;}/style
/head
bodyh3:empty 示例/h3p这一段有内容不会被选中/pp/p
/body
/html2.2 :first-of-type 与 :last-of-type
用于选择同一父元素中的第一个或最后一个特定类型的子元素。假设一个容器中有多个 p 标签下面示例分别标记第一个和最后一个 p
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:first-of-type 与 :last-of-type 示例/titlestylep:first-of-type {color: blue;}p:last-of-type {color: red;}/style
/head
bodyh3结构中的第一个与最后一个p/h3divp第一段应该显示为蓝色/pp中间段/pp最后一段应该显示为红色/p/div
/body
/html2.3 :first-child 与 :only-child
:first-child 选择作为父元素第一个子元素的指定元素:only-child 选择仅存在唯一子元素时的指定元素
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:first-child 与 :only-child 示例/titlestylep:first-child {font-weight: bold;}p:only-child {font-style: italic;color: purple;}/style
/head
bodyh3只有一个子元素的示例/h3divp我是唯一的子元素应该应用 :only-child 样式/p/divdivp我是第一个子元素应该应用 :first-child 样式/pp我是第二个子元素不应用 :first-child 样式/p/div
/body
/html2.4 :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n)
这四个伪类可以用来根据子元素的索引位置来选取元素。
p:nth-child(2)选择父元素的第二个子元素并且它是 p 标签p:nth-last-child(2)选择倒数第二个子元素如果它是 pp:nth-of-type(2)在同一类型中选择第二个 pp:nth-last-of-type(2)在同一类型中选择倒数第二个 p
下面通过一个示例来说明
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:nth-child 与 :nth-of-type 示例/titlestyle/* 父元素中第二个子元素无论类型 */div *:nth-child(2) {background-color: #dff0d8;}/* 父元素中第二个p标签 */p:nth-of-type(2) {border: 1px solid #f0ad4e;}/style
/head
bodyh3:nth-child 与 :nth-of-type 示例/h3divspan第一个子元素/spanp这是第二个子元素也同时是第一个p/pp这是第二个p类型的子元素将被边框标记/p/div
/body
/html2.5 :not(selector)
用于排除某些元素。例如下面的示例中除 p 外的其他所有元素都会被应用灰色背景
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:not 示例/titlestyle*:not(p) {background-color: #eee;}/style
/head
bodyh3:not 示例/h3p我不会有灰色背景/pdiv我会有灰色背景/divspan我也会有灰色背景/span
/body
/html3. 链接与用户交互状态伪类
3.1 链接的伪类:link、:visited、:active、:hover
这些伪类用于定义链接在不同状态下的样式例如未访问、已访问、鼠标悬停以及活动时的表现。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title链接伪类示例/titlestylea:link {color: blue;}a:visited {color: purple;}a:hover {text-decoration: underline;}a:active {color: red;}/style
/head
bodyh3链接状态示例/h3p这是一个 a hrefhttps://www.example.com target_blank示例链接/a。尝试点击或将鼠标悬停在链接上看看效果。/p
/body
/html3.2 :focus
用于选中当前获得焦点的元素常见于表单输入帮助用户清楚知道当前输入位置。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:focus 示例/titlestyleinput:focus {border-color: #66afe9;box-shadow: 0 0 8px rgba(102,175,233,0.6);}/style
/head
bodyh3:focus 示例/h3input typetext placeholder点击后试试
/body
/html3.3 :target
用于选中当前 URL 锚点对应的元素。假设页面中有一个元素的 id 为 news当 URL 包含 #news 时该元素会被选中。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:target 示例/titlestyle#news:target {background-color: #ffe58f;padding: 10px;}/style
/head
bodyh3:target 示例/h3p点击下面的链接跳转到新闻区域/pa href#news跳转到新闻/adiv stylemargin-top: 50px;p idnews这是新闻内容区域当 URL 中包含 #news 时我会被高亮显示。/p/div
/body
/html4. 其他伪类与伪元素
4.1 :root
:root 选择器选中文档的根元素通常是 html经常用来定义全局 CSS 变量或全局样式。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:root 示例/titlestyle:root {--main-bg-color: #f0f8ff;}body {background-color: var(--main-bg-color);}/style
/head
bodyh3:root 示例/h3p背景颜色由 CSS 变量控制。/p
/body
/html4.2 :lang(language)
根据元素的语言属性设定样式。比如下面示例中所有 langit意大利语的 p 元素会使用特殊样式。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title:lang 示例/titlestylep:lang(it) {color: green;font-weight: bold;}/style
/head
bodyh3:lang 示例/h3p langitQuesto è un testo in italiano./pp langenThis is an English text./p
/body
/html4.3 伪元素 ::first-letter、::first-line、::before、::after
伪元素可以在元素内容前后或内部特定位置插入样式比如首字母、首行等为排版加分。
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8title伪元素 示例/titlestylep::first-letter {font-size: 200%;color: #d9534f;}p::first-line {font-weight: bold;}p::before {content: 【开始】;color: #5bc0de;}p::after {content: 【结束】;color: #5cb85c;}/style
/head
bodyh3伪元素应用/h3p这是一段用于展示伪元素效果的文本。/p
/body
/html结语
CSS 中的伪类和伪元素能让我们无需增加额外的 HTML 结构通过直接在样式中操作状态和结构特性打造出更智能和富有表现力的页面效果。上面的示例覆盖了常用的伪类应用场景希望大家能够在实际项目中灵活使用极大地提升前端开发效率。
如果你对某个伪类的用法还有疑问欢迎在评论区留言讨论互相学习交流 这篇博客详细讲述了如何使用 CSS 伪类为页面元素添加交互、校验和结构化样式不仅展示了基本用法也提供了实际案例。希望这篇文章能帮助你在实际项目中更好地理解和应用 CSS 伪类。 文章转载自: http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.hngmg.cn.gov.cn.hngmg.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.fmry.cn.gov.cn.fmry.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.dpsgq.cn.gov.cn.dpsgq.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn