这篇文章主要围绕html–如何在我的css代码中增加计数器?和html添加css代码展开,旨在为您提供一份详细的参考资料。我们将全面介绍html–如何在我的css代码中增加计数器?的优缺点,解答htm
这篇文章主要围绕html – 如何在我的css代码中增加计数器?和html添加css代码展开,旨在为您提供一份详细的参考资料。我们将全面介绍html – 如何在我的css代码中增加计数器?的优缺点,解答html添加css代码的相关问题,同时也会为您带来CSS3 计数器_html/css_WEB-ITnose、css代码中的加号(+)相邻选择器的作用_html/css_WEB-ITnose、CSS代码实例:用CSS代码写出的各种形状图形_html/css_WEB-ITnose、css如何使用计数器给元素自动编号?css计数器的使用(代码示例)的实用方法。
本文目录一览:- html – 如何在我的css代码中增加计数器?(html添加css代码)
- CSS3 计数器_html/css_WEB-ITnose
- css代码中的加号(+)相邻选择器的作用_html/css_WEB-ITnose
- CSS代码实例:用CSS代码写出的各种形状图形_html/css_WEB-ITnose
- css如何使用计数器给元素自动编号?css计数器的使用(代码示例)
html – 如何在我的css代码中增加计数器?(html添加css代码)
> Create line numbers on pre with CSS only3个
在我的HTML代码中,我定义了以下列表:
<p>first.</p> <p>second.</p>
现在,我在css中:
.list { display: table; } .list:before { display: table-cell; counter-increment: counter; content: counter(counter) '.'; padding-right: 5px; }
并且页面上的结果是:
1. first 1. second
如何将第二个数字增加到2的值?
解决方法
body { counter-reset:counter; } .list { display: table; } .list:before { display: table-cell; counter-increment: counter; content: counter(counter) '.'; padding-right: 5px; }
<p>first.</p> <p>second.</p>
CSS3 计数器_html/css_WEB-ITnose
css3 计数器(css counters)可以允许我们使用css对页面的 任意元素进行计数,实现类似于有序列表 的功能。
与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化计数。
立即学习“前端免费学习笔记(深入)”;
计数器相关属性一览:
立即学习“前端免费学习笔记(深入)”;
属性 | 属性说明 |
counter-reset | 定义计数器,包括初始值、作用域等 |
counter-increment | 设置计数器的增数 |
content | 早::before和::after中生成内容 |
counter() | 在content属性中使用,用来调用计数器 |
@counter-style | 自定义列表样式 |
立即学习“前端免费学习笔记(深入)”;
语法
counter-reset :[
立即学习“前端免费学习笔记(深入)”;
含义
用来定义计数器的初值和作用域,默认值为none。
当元素display为None时,该属性失效
计数器定义方式解析
代码 | 代码解析 |
counter-reset:counterA | 定义计数器counterA,初始值为默认值0 |
counter-reset:counterA 6; | 定义计数器counterA,初始值为6 |
counter-reset:counterA 4,counterB; | 定义计数器counterA、counterB,初始值分别为4和0 |
counter-reset:counterA 4,counterB 2; | 定义计数器counterA、counterB,初始值分别为4和2 |
语法
counter-increment:[
含义
用来增数计数器,默认值为none(阻止计数器增加)
我们可以同时增数多个计数器
当元素display为none时,该属性失效。
立即学习“前端免费学习笔记(深入)”;
立即学习“前端免费学习笔记(深入)”;
计数器增数方式解析
代码 | 代码解析 |
counter-increment:counterA | 增数计数器counterA,每次增加1 |
counter-increment:counterA 2 | 增数计数器counterB,每次增加2 |
counter-increment:counterA 2,counterB -1 | 同时增数计数器counterA、counterB,分别加2、-1 |
立即学习“前端免费学习笔记(深入)”;
语法
content :[
counters(name,string,list-style-type)
含义
使用计数器,需要结合::before和::after使用。可以同时使用多个计数器。
立即学习“前端免费学习笔记(深入)”;
立即学习“前端免费学习笔记(深入)”;
计数器使用方式解析
代码 | 代码解析 |
content:"Fig." counter(imgCounter); | 混合字符串和计数器imgCounter |
content:"Fig." counter(imgCounter,lower-alpha) | 指定计数器的列表格式 |
content:counters(section,".") " "; | 在计数器之间加上点号,同时在计数器最加上一个空格 |
content:counters(section,".",lower-roman) " "; | 定义计数器为小写罗马数字格式,同时加点号,空格 |
语法格式
立即学习“前端免费学习笔记(深入)”;
@counter-style counterStyleName{
system:算法;
range:使用范围;
symbols:符合;or additive-symbols:符号;
prefix:前缀;suffix:后缀;
pad:补零(eg.01,02,03);
negative:负数策略:
fallback:出错后的默认值;
speakas:语音策略;
}
立即学习“前端免费学习笔记(深入)”;
自定义counter style示例
@counter-style cjk-heavenly-stem{
system:alphabetic;
symbols:"\7532" "\4E59" "\4E19" "\4E01" "\620A" "\5DF1" "\5E9A" "\8F9B" "\58EC" "\7678";
/*甲 乙 丙 丁 戊 己 庚 辛 壬 葵*/
suffix:"、";
}
立即学习“前端免费学习笔记(深入)”;
css代码中的加号(+)相邻选择器的作用_html/css_WEB-ITnose
css代码中的加号(+)相邻选择器的作用:
本章节介绍一下css中相邻选择器的作用,此选择器用加号(+)表示。
此选择器能够匹配前面的选择器紧邻的兄弟元素。
代码实例:
<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><style type="text/css">input[type=radio]:checked + span{ background:blue;}input[type=radio]:checked + span:after{ content:"蚂蚁部落[url=]2[/url]";}</style></head><body><form method="post"> <fieldset> <legend>选中下面的项试试</legend> <ul> <li><label><input type="radio" name="colour-group"/><span>蓝色</span></label></li> <li><label><input type="radio" name="colour-group"/><span>红色</span></label></li> <li><label><input type="radio" name="colour-group"/><span>黑色</span></label></li> </ul> </fieldset></form></body></html>
上面的代码演示了此选择器的作用,更多内容可以参阅相关阅读。
相关阅读:
1.相邻选择器可以参阅CSS的相邻选择符(E+F)一章节。
2.:checked伪类选择器可以参阅CSS的伪类选择符E:checked一章节。
3.:after伪元素选择器可以参阅CSS的伪对象选择符E:after/E::after一章节。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=15644
更多内容可以参阅:http://www.softwhy.com/divcss/
CSS代码实例:用CSS代码写出的各种形状图形_html/css_WEB-ITnose
一共收集整理了图形20个,比较实用,同时也为了熟悉css的代码。整合了一下,有错误欢迎指出。
1.正方形
#square {
width: 100px;
height: 100px;
background: red;}
2.长方形
#rectangle {
width: 200px;
height: 100px;
background: red;}
3.左上三角
#triangle-topleft {
width: 0;
height: 0;
border-top: 100px solid red;
border-right: 100px solid transparent;
}
4.右上三角
#triangle-topright {
width: 0;
height: 0;
border-top: 100px solid red;
border-left: 100px solid transparent;}
5.左下三角
#triangle-bottomleft {
width: 0;
height: 0;
border-bottom: 100px solid red;
border-right: 100px solid transparent;
}
6.右下三角
#triangle-bottomright {
width: 0;
height: 0;
border-bottom: 100px solid red;
border-left: 100px solid transparent;
}
7.平行四边形
#parallelogram {
width: 150px;
height: 100px;
-webkit-transform: skew(20deg);
-moz-transform: skew(20deg);
-o-transform: skew(20deg);
background: red;
}
8.梯形
#trapezoid {
border-bottom: 100px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
height: 0;
width: 100px;
}
9.六角星
#star-six {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
position: relative;
}
#star-six:after {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 100px solid red;
position: absolute;
content: "";
top: 30px;
left: -50px;
}
10.五角星
#star-five {
margin: 50px 0;
position: relative;
display: block;
color: red;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-moz-transform: rotate(35deg);
-webkit-transform: rotate(35deg);
-ms-transform: rotate(35deg);
-o-transform: rotate(35deg);
}
#star-five:before {
border-bottom: 80px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
position: absolute;
height: 0;
width: 0;
top: -45px;
left: -65px;
display: block;
content: '''';
-webkit-transform: rotate(-35deg);
-moz-transform: rotate(-35deg);
-ms-transform: rotate(-35deg);
-o-transform: rotate(-35deg);
}
#star-five:after {
position: absolute;
display: block;
color: red;
top: 3px;
left: -105px;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-webkit-transform: rotate(-70deg);
-moz-transform: rotate(-70deg);
-ms-transform: rotate(-70deg);
-o-transform: rotate(-70deg);
content: '''';
}
11.五边形
#pentagon {
position: relative;
width: 54px;
border-width: 50px 18px 0;
border-style: solid;
border-color: red transparent;
}
#pentagon:before {
content: "";
position: absolute;
height: 0;
width: 0;
top: -85px;
left: -18px;
border-width: 0 45px 35px;
border-style: solid;
border-color: transparent transparent red;
}
12.六边形
#hexagon {
width: 100px;
height: 55px;
background: red;
position: relative;
}
#hexagon:before {
content: "";
position: absolute;
top: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 25px solid red;
}
#hexagon:after {
content: "";
position: absolute;
bottom: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 25px solid red;
}
13.桃心
#heart {
position: relative;
width: 100px;
height: 90px;
}
#heart:before,
#heart:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
-moz-transform-origin: 0 100%;
-ms-transform-origin: 0 100%;
-o-transform-origin: 0 100%;
transform-origin: 0 100%;
}
#heart:after {
left: 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin :100% 100%;
}
14。无限大符号
#infinity {
position: relative;
width: 212px;
height: 100px;
}
#infinity:before,
#infinity:after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 60px;
height: 60px;
border: 20px solid red;
-moz-border-radius: 50px 50px 0 50px;
border-radius: 50px 50px 0 50px;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
}
#infinity:after {
left: auto;
right: 0;
-moz-border-radius: 50px 50px 50px 0;
border-radius: 50px 50px 50px 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
15.蛋
#egg {
display:block;
width: 126px;
height: 180px;
-webkit-border-radius: 63px 63px 63px 63px / 108px 108px 72px 72px;
border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
}
16.提示对话框
#talkbubble {
width: 120px;
height: 80px;
background: red;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#talkbubble:before {
content:"";
position: absolute;
right: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-right: 26px solid red;
border-bottom: 13px solid transparent;
}
17.十二角星
#burst-8 {
background: red;
width: 80px;
height: 80px;
position: relative;
text-align: center;
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
-ms-transform: rotate(20deg);
-o-transform: rotate(20eg);
transform: rotate(20deg);
}
#burst-8:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 80px;
width: 80px;
background: red;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-ms-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}
18.八角星
#burst-8 {
background: red;
width: 80px;
height: 80px;
position: relative;
text-align: center;
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
-ms-transform: rotate(20deg);
-o-transform: rotate(20eg);
transform: rotate(20deg);
}
#burst-8:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 80px;
width: 80px;
background: red;
-webkit-transform: rotate(135deg);
-moz-transform: rotate(135deg);
-ms-transform: rotate(135deg);
-o-transform: rotate(135deg);
transform: rotate(135deg);
}
19.钻石
#cut-diamond {
border-style: solid;
border-color: transparent transparent red transparent;
border-width: 0 25px 25px 25px;
height: 0;
width: 50px;
position: relative;
margin: 20px 0 50px 0;
}
#cut-diamond:after {
content: "";
position: absolute;
top: 25px;
left: -25px;
width: 0;
height: 0;
border-style: solid;
border-color: red transparent transparent transparent;
border-width: 70px 50px 0 50px;
}
20.八卦
#yin-yang {
width: 96px;
height: 48px;
background: #eee;
border-color: red;
border-style: solid;
border-width: 2px 2px 50px 2px;
border-radius: 100%;
position: relative;
}
#yin-yang:before {
content: "";
position: absolute;
top: 50%;
left: 0;
background: #eee;
border: 18px solid red;
border-radius: 100%;
width: 12px;
height: 12px;
}
#yin-yang:after {
content: "";
position: absolute;
top: 50%;
left: 50%;
background: red;
border: 18px solid #eee;
border-radius:100%;
width: 12px;
height: 12px;
}
css如何使用计数器给元素自动编号?css计数器的使用(代码示例)
在之前的文章【css计数器(counter)是什么】中我们简单介绍了css计数器的相关属性,以及通过代码实例来简单了解了一下css计数器的使用,本篇文章我们就来具体看看css计数器是如何设置和使用的,如何实现元素自动编号的。【相关视频教程推荐:CSS3教程】
一、设置和使用css计数器,实现简单的元素编号
为了创建和使用CSS计数器,遵循以下步骤:
1、设置计数器的名称并将其重置为我们选择的初始值。这是使用counter-reset属性完成的。
立即学习“前端免费学习笔记(深入)”;
counter-reset: 标识符(计数器的名称) <整数>(起始值,可选,默认值为0);
初始化计数器(指定起始值)是可选的, 如果没有指定一个确切的值,它将会从零开始,则此时的计数器实现的内会从‘1’开始。
counter-reset属性是在要编号的元素的祖先或兄弟元素上设置的。例如,如果在文章中对标题进行编号,则可以在这些标题的祖先上设置计数器。
article { / *设置一个名为“section”的计数器,并将其初始化为0 * / counter-reset:section 0 ; }
其背后的原因是,重置编号元素上的计数器将导致出现具有相同编号的元素。这是因为计数器将被重置为其初始值,然后在显示之前对每个标题进行递增。
2、指定计数器何时递增,以及按什么值递增。
例如,如果希望计数器在每次出现h2标题时递增,那么将可以指定;这是使用counter-increment属性完成的。我们可以选择对每个出现的要编号的元素(本例中为h2)递增计数器的任何值。默认情况下,计数器将递增1;我们还可以使用负值,这样计数器将递减。
h2 { / *在每次出现h2时使用“section”计数器,并每次出现就增加1(默认值)* / counter-increment:section 1 ; }
这里需要注意的一件重要事情是:计数器是在显示之前递增,因此如果我们希望第一个标题从1开始,则应该在计算器中将计数器的counter-reset属性初始值设置为零。
3、显示计数器
设置计数器并指定何时以及应该增加多少后,我们需要显示该计数器。
要显示计数器,我们就需要使用content属性的counter()函数(或counters()嵌套计数器)作为::before伪元素的值。
在我们的示例中,我们是对h2标题进行编号,因此我们将在标题之前显示计数器:
h2 :: before { content:counter(section); }
当然,如果你希望在标题的数字和标题之间添加一些空格和可能的任何其他分隔符,可以通过将分隔符附加到计数器的counter()函数中来执行此操作,使用字符串作为值,例:
h2::before { /* 在数字之后加一个点,后面加上空格 */ content: counter(my-counter) ". "; }
下面我来来看看示例:
html代码:
<h2>css计数器的使用</h2> <p>css计数器的使用css计数器的使用css计数器的使用css计数器的使用css计数器的使用</p> <h2>css计数器的使用</h2> <p>css计数器的使用css计数器的使用css计数器的使用css计数器的使用css计数器的使用</p>
css代码:
body{ counter-reset: section; } h2:before{ counter-increment: section; content: counter(section) "."; }
效果图:
二、设置嵌套计数器,实现元素的嵌套编号
有时在一个大标题下面还会有多个2级标题,3级标题,标题一个嵌套一个(如,下图),如何编号?
下面我们就来介绍使用嵌套计数器,来实现元素的嵌套编号的方法。
要实现元素的嵌套编号,最简单的方法就是使用counters()函数;使用该函数,我们可以在一个声明中设置多个计数器,默认情况下这些计数器将嵌套。
示例介绍:我们将使用counters()函数在嵌套列表上设置嵌套计数器。列表(ul,ol)可以嵌套到标记中的几个级别,因此我们可以使用counters()函数。
html代码:
<div> <ul> <li> 菜单1 <ul> <li>菜单1.1</li> <li>菜单1.2</li> <li>菜单1.3 <ul> <li>菜单1.3.1</li> <li>菜单1.3.2</li> <li>菜单1.3.3</li> <li>菜单1.3.4</li> </ul> </li> </ul> </li> <li>菜单2 <ul> <li>菜单2.1</li> <li>菜单2.2</li> <li>菜单2.3</li> </ul> </li> </ul> </div>
css代码:
首先我们要创建一个计数器,定义计数器的名称为:nested-counter,初始化值为:0;增量为:1。
ul { list-style: none;/* 去除ul中默认的样式*/ counter-reset:nested-counter; } ul li { counter-increment:nested-counter; line-height: 1.6; }
显示计数器很简单。我们将使用一个点作为嵌套计数器之间的分隔符,我们将在计数器和列表项中的文本之间添加一个右括号作为分隔符,仅用于更改。
ul li :before { / * counters()函数内的字符串是两个计数器之间的分隔符,并且函数外部的字符串是生成的数字和列表项的文本之间的分隔符* / content: counters(nested-counter, ".") ") "; font-weight: bold; }
实现效果,看上图。
两个计数器之间的分隔符,可以有很多,如"."、“-”等等。
三、计数器的样式
计数器也是可以设置样式的,不仅仅可以用数字来显示编号,还可以是字母(如a,A),罗马字符(如:ⅰ,ⅱ)等等,只要是css list-style-type属性可用的列表样式类型中的任何一种都可以来设置计数器的样式。在之前的文章【css如何设置列表样式?列表样式的实现】中有介绍,大家可以参考一下。
那么如何设置?
这就需要设置style参数,我们来看看基本语法:
counter(name,style) counters(name,分隔符,style)
name:计数器名称,style就是样式了。
以下是所有可能的计数器样式:
disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek | lower-latin | upper-latin | armenian | georgian | lower-alpha | upper-alpha | none | inherit
总结:以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。
以上就是
关于html – 如何在我的css代码中增加计数器?和html添加css代码的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于CSS3 计数器_html/css_WEB-ITnose、css代码中的加号(+)相邻选择器的作用_html/css_WEB-ITnose、CSS代码实例:用CSS代码写出的各种形状图形_html/css_WEB-ITnose、css如何使用计数器给元素自动编号?css计数器的使用(代码示例)的相关知识,请在本站寻找。
本文标签: