Table of Contents
强大魔力
结构解析
算法-system
使用范围-range
使用符号-symbols和additive-symbols
使用前后缀-prefix和suffix
匿名counter-style-symbols()函数
预定义style
相关阅读
致谢
Home Web Front-end HTML Tutorial CSS Counter Style demo_html/css_WEB-ITnose

CSS Counter Style demo_html/css_WEB-ITnose

Jun 24, 2016 am 11:49 AM

2015年2月3日,CSS Counter Style level3成为了W3C的候选标准,是时候来一探究竟, 看看强大魔力的 at counter-style如何自定义list-style和counter 。进来一坐,且听庆哥分解。

强大魔力

使用CSS Counter Style可以做什么,来看看庆哥做的简单案例。当然,如果要发挥最大魔力,需要您的美好创意,这不是哥的特长,就不献丑了,欢迎各位通过留言提交您的创意。






需要注意的是, 目前仅有firefox浏览较好的支持 at counter-style 。我依然把代码放到了codepen,请大家自由选择-在线研究-or-下载收藏-。
使用 at counter-style命令 ,我们可以自定义列表样式,可以用在list-style、counter、counters等上。

结构解析

定义一个counter-style的典型格式如下:

@counter-style counter名字{	 system  : 算法;	 range   : 使用范围;	 symbols : 符号; or additive-symbols: 符号	 prefix  : 前缀;	 suffix  : 后缀;	 pad     : 补零(eg. 01,02,03);	 negative: 负数策略;	 fallback: 出错后的默认值;	 speakas : 语音策略;}
Copy after login

上面详细列出了 at counter-style的所有命令 ,大部分情况下,我们只需要使用其中几个命令即可,例如实现“天干编号”的命令如下。

@counter-style cjk-heavenly-stem {  system: alphabetic;  symbols: "\7532" "\4E59" "\4E19" "\4E01" "\620A" "\5DF1" "\5E9A" "\8F9B" "\58EC" "\7678";  /* 甲 乙 丙 丁 戊 己 庚 辛 壬 癸 */  suffix: "、";}
Copy after login

接下来我们来简要看看各个命令的用法。

算法-system

命令 意义 可接受值 默认值
system 符号生成算法 cyclic 、 numeric 、 alphabetic 、 symbolic 、 additive 、 [fixed ?] 、 [ extends ] symbolic

算法详细解释如下表所说。

参数 意义 示例代码 表现方式
cyclic 循环使用符号 system: cyclic; symbols:'a' 'b' 'c'; a,b,c,a,b,c,a
fixed 只用一遍符号,数字表示开始使用的位置 system: fixed 3; symbols:'a' 'b' 'c' 'd'; 1,2,a,b,c,d,7
symbolic 循环使用符号,第二遍双倍重复 system:symbolic; symbols:'a' 'b' 'c'; a,b,c,aa,bb,cc,aaa
alphabetic 类似于小写字母循环 system:alphabetic; symbols:'a' 'b' 'c'; a,b,c,aa,ab,ac,ba
numeric 类似于数字进制 system:alphabetic; symbols:'a' 'b' 'c'; b,c,c,ba,bb,bc,ca,cb
additive 类似于罗马数字的编号系统 system:additive; additive-symbols: c 3, b 2, a 1; a,b,c,ca,cb,cc,cca
extends 继承其他编号系统 system: extends decimal; suffix: ') '; 1),2),3),4),5),6),7)

使用范围-range

指定自定义符号系统的使用范围,超出范围外的将采用fallback指定的默认编号。
可以使用[ [ | infinite ]{2} ]或 auto两种形式指定range范围,auto为默认值。
auto在不同system下的含义如下列表所示:

  • 在cyclic、numeric、fixed算法下,auto表示从负无穷到正无穷
  • 在alphabetic、symbolic算法下,auto表示从1到正无穷
  • additive算法下,auto表示从0到正无穷
  • extend算法下,auto取继承的父系统的取值范围,如果为预定义的复杂系统,取预定义的取值
  • 下面的示例都是正确的范围指定。

    /*指定范围为auto,默认值*/range: auto;/*指定范围为两个数字:前面表示下限,后面表示上限,两个数字都包含在内,列表之间用逗号隔开*/range: 1 6;range: 1 2,4 6;/*指定范围为关键字*/range: infinite;   /*从负无穷到正无穷*/range: infinite 4; /*从负无穷到4*/range: -6 infinite;/*从-6到正无穷*/
    Copy after login

    使用符号-symbols和additive-symbols

    这是自定义counter-style中非常重要的属性,在 cyclic、numeric、alphabetic、symbolic、fixed等算法中必须指定symbols属性,在additive算法中必须指定additive-symbols。

    使用前后缀-prefix和suffix

    prefix的默认自为“ ”,suffix的默认值为"\2E\20",一个点加一个空格。
    ###使用补零策略-pad
    pad可以让开发者指定固定宽度的符号系统,例如三位的数字系统,001, 002,……,100, 101等。
    pad指定的格式为 pad && ,例如 pad 3 "0"等。

    匿名counter-style-symbols()函数

    symblos函数将创建一个没有名字,prefix为"",suffix为" ",range为auto,fallback为decimal,negative为"\2d"(-), pad为0 "",speak-as为auto的匿名style。也即我们只需要在匿名counter-style中指定system和symbols即可。

    ol { list-style: symbols(cyclic "a" "b" "c" "d"); }
    Copy after login

    预定义style

    CSS Counter Styles Level 3预定了很多不错的符号系统,例如针对中国用户的天干、地支、中文数字、中文大写等符号系统。这些预定义的系统,使我们学习counter-style的不错资源,下面我们就来看几个。

    /*两位数字编号-01,02,03……10,11*/@counter-style decimal-leading-zero {  system: extends decimal;  pad: 2 '0';}/*中文数字编号*/@counter-style cjk-decimal {  system: numeric;  range: 0 infinite;  symbols: \3007  \4E00  \4E8C  \4E09  \56DB  \4E94  \516D  \4E03  \516B  \4E5D;  /* ? 一 二 三 四 五 六 七 八 九 */  suffix: "\3001";  /* "、" */}/*天干编号*/@counter-style cjk-heavenly-stem {  system: alphabetic;  symbols: "\7532" "\4E59" "\4E19" "\4E01" "\620A" "\5DF1" "\5E9A" "\8F9B" "\58EC" "\7678";  /* 甲 乙 丙 丁 戊 己 庚 辛 壬 癸 */  suffix: "、";}/*地支编号*/@counter-style cjk-earthly-branch {  system: alphabetic;  symbols: "\5B50" "\4E11" "\5BC5" "\536F" "\8FB0" "\5DF3" "\5348" "\672A" "\7533" "\9149" "\620C" "\4EA5";  /* 子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥 */  suffix: "、";}/*中文大写编号-壹仟壹佰壹拾壹*/@counter-style simp-chinese-formal {/* this is a predefined complex style in the CSS3 Counter Styles specification */system: additive;range: -9999 9999;additive-symbols: 9000 \7396\4EDF, 8000 \634c\4EDF, 7000 \67d2\4EDF, 6000 \9646\4EDF, 5000 \4f0d\4EDF, 4000 \8086\4EDF, 3000 \53C1\4EDF, 2000 \8CB3\4EDF, 1000 \58F9\4EDF, 900 \7396\4F70, 800 \634C\4F70, 700 \67D2\4F70, 600 \9646\4F70, 500 \4f0d\4F70, 400 \56DB\4F70, 300 \53C1\4F70, 200 \8CB3\4F70, 100 \58F9\4F70, 90 \7396\62FE, 80 \634C\62FE, 70 \67D2\62FE, 60 \9646\62FE, 50 \4f0d\62FE, 40 \8086\62FE, 30 \53C1\62FE, 20 \8CB3\62FE, 10 \58F9\62FE, 9 \7396, 8 \634C, 7 \67D2, 6 \9646, 5 \4f0d, 4 \8086, 3 \53C1, 2 \8CB3, 1 \58F9, 0 \96F6;suffix:'、 ';negative: "\8D1F";}
    Copy after login

    相关阅读

    w3c 候选标准
    Predefined Counter Styles

    致谢

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1666
    14
    PHP Tutorial
    1272
    29
    C# Tutorial
    1251
    24
    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    See all articles