css3的content属性
CSS中主要的伪元素有四个:before/after/first-letter/first-line,在before/after伪元素选择器中,有一个content属性,能够实现页面中的内容插入。
插入纯文字
content : ”插入的文章”,或者 content:none 不插入内容
#html <h1>这是h1</h1> <h2>这是h2</h2> #css h1::after{ content:"h1后插入内容" } h2::after{ content:none }
嵌入文字符号
可以使用content属性的 open-quote 属性值和 close-quote 属性值在字符串两边添加诸如括号、单引号、双引号之类的嵌套文字符号。open-quote 用于添加开始的文字符号,close-quote 用于添加结束的文字符号。修改上述的css:
h1{ quotes:"(" ")"; /*利用元素的quotes属性指定文字符号*/ } h1::before{ content:open-quote; } h1::after{ content:close-quote; } h2{ quotes:"\"" "\""; /*添加双引号要转义*/ } h2::before{ content:open-quote; } h2::after{ content:close-quote; }
插入图片
content属性也可以直接在元素前/后插入图片
#html <h3>这是h3</h3> #css h3::after{ content:url(图片路径) }
插入元素的属性值
content属性可以直接利用attr获取元素的属性,将其插入到对应位置。
#html <a href="//m.sbmmt.com">这是链接</a> #css a:after{ content:attr(href); }
插入项目编号
利用content的counter属性针对多个项目追加连续编号.
#html <h1>大标题</h1> <p>文字</p> <h1>大标题</h1> <p>文字</p> <h1>大标题</h1> <p>文字</p> <h1>大标题</h1> <p>文字</p> #css h1:before{ content:counter(my)'.'; } h1{ counter-increment:my; }
项目编号修饰
默认插入的项目编号是数字型的,1,2,3.。。。自动递增,也能给项目编号追加文字和样式,依旧利用上面的html,css修改如下:
h1:before{ content:'第'counter(my)'章'; color:red; font-size:42px; } h1{ counter-increment:my; }
指定编号种类
利用content(计数器名,编号种类)格式的语法指定编号种类,编号种类的参考可以依据ul的list-style-type属性值。利用上述的html,css修改如下:
h1:before{ content:counter(my,upper-alpha); color:red; font-size:42px; } h1{ counter-increment:my; }
编号嵌套
大编号中嵌套中编号,中编号中嵌套小编号。
#html <h1>大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> <h1>大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> <h1>大标题</h1> <p>文字1</p> <p>文字2</p> <p>文字3</p> #css h1::before{ content:counter(h)'.'; } h1{ counter-increment:h; } p::before{ content:counter(p)'.'; margin-left:40px; } p{ counter-increment:p; }
在示例的输出中可以发现,p的编号是连续的。如果对于每一个h1后的三个p重新编号的话,可以使用counter-reset属性重置,修改上述h1的css:
h1{ counter-increment:h; counter-reset:p; }
还可以实现更复杂的嵌套,例如三层嵌套。
#html <h1>大标题</h1> <h2>中标题</h2> <h3>小标题</h3> <h3>小标题</h3> <h2>中标题</h2> <h3>小标题</h3> <h3>小标题</h3> <h1>大标题</h1> <h2>中标题</h2> <h3>小标题</h3> <h3>小标题</h3> <h2>中标题</h2> <h3>小标题</h3> <h3>小标题</h3> #css h1::before{ content:counter(h1)'.'; } h1{ counter-increment:h1; counter-reset:h2; } h2::before{ content:counter(h1) '-' counter(h2); } h2{ counter-increment:h2; counter-reset:h3; margin-left:40px; } h3::before{ content:counter(h1) '-' counter(h2) '-' counter(h3); } h3{ counter-increment:h3; margin-left:80px; }
下面给出完整DEMO
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS content</title> <style> .string p:after { margin-left: -16px; background: #fff; content: "支持"; color: #f00; } .attr p:after { content: attr(title); } .url p:before { content: url(https://img.php.cn/upload/article/000/000/009/587d87ecca52d563.jpg); display: block; } .test ol { margin: 16px 0; padding: 0; list-style: none; } .counter1 li { counter-increment: testname; } .counter1 li:before { content: counter(testname)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter2 li { counter-increment: testname2; } .counter2 li:before { content: counter(testname2,lower-roman)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter3 ol ol { margin: 0 0 0 28px; } .counter3 li { padding: 2px 0; counter-increment: testname3; } .counter3 li:before { content: counter(testname3,float)":"; color: #f00; font-family: georgia,serif,sans-serif; } .counter3 li li { counter-increment: testname4; } .counter3 li li:before { content: counter(testname3,decimal)"."counter(testname4,decimal)":"; } .counter3 li li li { counter-increment: testname5; } .counter3 li li li:before { content: counter(testname3,decimal)"."counter(testname4,decimal)"."counter(testname5,decimal)":"; } </style> </head> <body> <ul> <li> <strong>string:</strong> <p>你的浏览器是否支持content属性:否</p> </li> <li> <strong>attr:</strong> <p title="如果你看到我则说明你目前使用的浏览器支持content属性"></p> </li> <li> <strong>url():</strong> <p>如果你看到图片则说明你目前使用的浏览器支持content属性</p> </li> <li> <strong>counter(name):</strong> <ol> <li>列表项</li> <li>列表项</li> <li>列表项</li> </ol> </li> <li> <strong>counter(name,list-style-type):</strong> <ol> <li>列表项</li> <li>列表项</li> <li>列表项</li> </ol> </li> <li> <strong>counter(name)拓展应用:</strong> <ol> <li>列表项 <ol> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> <li>列表项</li> </ol> </li> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> <li>列表项 <ol> <li>列表项</li> <li>列表项</li> </ol> </li> </ol> </li> </ul> </body> </html>

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

不同浏览器对CSS解析存在差异,导致显示效果不一致,主要包括默认样式差异、盒模型计算方式、Flexbox和Grid布局支持程度及某些CSS属性行为不一致。1.默认样式处理不一致,解决方法是使用CSSReset或Normalize.css统一初始样式;2.旧版IE的盒模型计算方式不同,建议统一使用box-sizing:border-box;3.Flexbox和Grid在边缘情况或旧版本中表现有差异,应多测试并使用Autoprefixer;4.某些CSS属性行为不一致,需查阅CanIuse并提供降级

accent-color是CSS中用于自定义复选框、单选按钮和滑块等表单元素高亮颜色的属性;1.它直接改变表单控件选中状态的默认颜色,如将复选框的蓝色勾选标记改为红色;2.支持的元素包括type="checkbox"、type="radio"和type="range"的输入框;3.使用accent-color可避免复杂的自定义样式和额外DOM结构,保持原生可访问性;4.现代浏览器普遍支持,旧浏览器需降级处理;5.设置accent-col

1.ItAdjustSelementsLikeImagesRikeImagesOrformInputswithIntExtLineSustLineSlineSlineSlineSlikeLikeLikeBaseline,中间,Super,Super,Super和Sub.2.intablebecells,ItControlScontentalStalteNtalmscontentalMedwithThtop,Middle,Middle,Midder,Midder,经常

UseAutomatedToolSlikePurgecsSoruncsStoscanAndRemoveUnusedcss; 2. integratePuratePurgingIntoyourBuildProcessviawebpack,vite,vite,ortailwind ’scontentConfiguration; 3.AuditcsSusageWithChroMedEvtoolScoverAgeTabBeforgeForgingToavoidRemovingNeedEdedStyles; 4.safelistdynamic

要改变CSS中文本颜色,需使用color属性;1.使用color属性可设置文本前景色,支持颜色名称(如red)、十六进制码(如#ff0000)、RGB值(如rgb(255,0,0))、HSL值(如hsl(0,100%,50%))以及带透明度的RGBA或HSLA(如rgba(255,0,0,0.5));2.可将颜色应用于包含文本的任何元素,如h1至h6标题、段落p、链接a(需注意a:link、a:visited、a:hover、a:active不同状态的颜色设置)、按钮、div、span等;3.最

首先通过JavaScript获取用户系统偏好和本地存储的主题设置,初始化页面主题;1.HTML结构包含一个按钮用于触发主题切换;2.CSS使用:root定义亮色主题变量,.dark-mode类定义暗色主题变量,并通过var()应用这些变量;3.JavaScript检测prefers-color-scheme并读取localStorage决定初始主题;4.点击按钮时切换html元素上的dark-mode类,并将当前状态保存至localStorage;5.所有颜色变化均带有0.3秒过渡动画,提升用户

backdrop-filter用于对元素背后的内容应用视觉效果,1.使用backdrop-filter:blur(10px)等语法实现毛玻璃效果;2.支持blur、brightness、contrast等多种滤镜函数并可叠加;3.常用于玻璃态卡片设计,需确保元素与背景重叠;4.现代浏览器支持良好,可用@supports提供降级方案;5.避免过大模糊值和频繁重绘以优化性能,该属性仅在元素背后有内容时生效。

用户代理样式表是浏览器自动应用的默认CSS样式,用于确保未添加自定义样式的HTML元素仍具基本可读性。它们影响页面初始外观,但不同浏览器存在差异,可能导致不一致显示。开发者常通过重置或标准化样式来解决这一问题。使用开发者工具的“计算”或“样式”面板可查看默认样式。常见覆盖操作包括清除内外边距、修改链接下划线、调整标题大小及统一按钮样式。理解用户代理样式有助于提升跨浏览器一致性并实现精准布局控制。
