Python之路 第十一篇--CSS

青春壹個敷衍的年華 提交于 2021-01-27 05:34:03

css

  CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离。

 

一.css的四种引入方式

1.行内式
    行内式是在标记的style属性中设定CSS样式。这种方式没有体现出CSS的优势,不推荐使用。

1
< div  style='font-size:20px;'> Hello </ div >

2.嵌入式
    嵌入式是将CSS样式集中写在网页的<head></head>标签对的<style></style>标签对中。格式如下:

1
2
3
4
5
6
7
8
9
< head >
     < meta  charset="UTF-8">
     < title >Document</ title >
     < style  type="text/css">
         div{
             font-size: 20px;
         }
     </ style >
</ head >

3.导入式
    将一个独立的.css文件引入HTML文件中,导入式使用CSS规则引入外部CSS文件,<style>标记也是写在<head>标记中,使用的语法如下:

1
2
3
4
5
< head >
< style  type="text/css">
           @import"mystyle.css";  此处要注意.css文件的路径
</ style >
</ head >

4.链接式
    也是将一个.css文件引入到HTML文件中

1
2
3
< head >
     < link  href="mystyle.css" rel="stylesheet" type="text/css"/>
</ head >

注意:

  导入式 会在整个网页装载完后再装载CSS文件,因此这就导致了一个问题,如果网页比较大,则会先显示无样式的页面,闪烁一下之后,再出现网页的样式。这是导入式固有的一个缺陷。

  使用 链接式 时与导入式不同的是它会以网页文件主体装载前装载CSS文件,因此显示出来的网页从一开始就是带样式的效果的,它不会象导入式那样先显示无样式的网页,然后再显示有样式的网页,这是链接式的优点。

样式应用顺序:

  • 行内样式优先级最高

  • 针对相同的样式属性,不同的样式属性将以合并的方式呈现

  • 相同样式并且相同属性,呈现方式在<head>中的顺序决定,后面会覆盖前面属性

  • !important  指定样式规则应用最优先

1
2
3
.div {
     color red  !important ;
}

 

CSS优先级:

  所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序。

样式表中的特殊性描述了不同规则的相对权重,它的基本规则是:
      1 内联样式表的权值最高       style=""  ------------------- 1000
   2 统计选择符中的ID属性个数。    #id    -------------100
  3 统计选择符中的CLASS属性个数。 .class  ------------- 10
 4 统计选择符中的HTML标签名个数。     p     --------------1

 

二 css的选择器(Selector)

“选择器”指明了{ } 中的“样式”的作用对象,也就是“样式”作用于网页中的哪些元素

1 基础选择器:

  *:通用元素选择器,匹配任何元素

1
2
3
4
*{  #通配符
     margin:0;
     padding:0;
}

  E:标签选择器,匹配所有使用E标签的元素p

1
2
3
div{
     color:green;
}

  .info和E.info:   class选择器,匹配所有class属性中包含info的元素

1
2
3
4
5
6
7
8
9
.info{
     font-size:20px;
}
p.info{
     background:blue;
}
 
< div  class='info'> Hello </ div >
< p  class='info'> World! </ div >

  #info和E#info  id选择器,匹配所有id属性等于info的元素

1
2
3
4
5
6
7
8
#info{
     font-size:20px;
}
p#info{
     background:blue;
}
 
< div  id='info'> Hello </ div >

注意:

  标签中,id的值是唯一的,而class可重复!

2 组合选择器

  E,F  多元素选择器,同时匹配所有E元素或F元素,E和F之间用逗号分隔

1
2
3
div,p{
     color:red;
}

  E F  后代元素选择器,匹配所有属于E元素后代的F元素,E和F之间用空格分隔

1
2
3
4
div p{
     font-size: 20px;
     color:red;
}

  E > F  子元素选择器,匹配所有E元素的子元素F 

1
2
3
div > strong {
     color:#f00;
}

  E + F  毗邻元素选择器,匹配所有紧随E元素之后的同级元素F

1
2
3
4
div+p{
     font-size: 20px;
     color:red;
}

注意嵌套规则

  1. 块级元素可以包含内联元素或某些块级元素,但内联元素不能包含块级元素,它只能包含其它内联元素。

  2. 块级元素不能放在<p>里面

  3. 有几个特殊的块级元素只能包含内联元素,不能包含块级元素。如h1,h2,h3,h4,h5,h6,p,dt

  4. li内可以包含div

  5. 块级元素与块级元素并列、内联元素与内联元素并列。(错误的:<div><h2></h2><span></span></div>)

3 属性选择器

  E[att]  匹配所有具有att属性的E元素,不考虑它的值。

        (注意:E在此处可以省略,比如“[cheacked]”。以下同。)

1
2
3
4
5
6
div[att]{
     font-size: 20px;
     color: red;
}
 
< div  att='abc'>Hello</ div >

  E[att=val]  匹配所有att属性等于“val”的E元素

1
2
3
4
5
6
div[att='abc']{
     font-size: 20px;
     color: red;
}
 
< div  att='abc'>Hello</ div >

  E[att~=val]  匹配所有att属性具有多个空格分隔的值、其中一个值等于“val”的E元素

1
2
3
4
5
6
div[att~='aaa']{
     font-size: 20px;
     color: red;
}
 
< div  att='aaa bbb ccc'>Hello</ div >

  E[att|=val]  匹配所有att属性具有多个连字号分隔(hyphen-separated)的值、其中一个值以“val”开头的E元素。

           主要用于lang属性,如“en”、“en-us”、“en-gb”等等

1
2
3
4
5
6
div[att|='aaa']{
     font-size: 20px;
     color: red;
}
 
< div  att='aaa-bbb-ccc'>Hello</ div >

  E[attr^=val]  匹配属性值以指定值开头的每个元素

  E[attr$=val]  匹配属性值以指定值结尾的每个元素

  E[attr*=val]  匹配属性值中包含指定值的每个元素

  p:before         在每个 <p> 元素的内容之前插入内容

  p:after           在每个 <p> 元素的内容之前插入内容

1
2
3
4
5
6
7
8
9
10
p:before{
     content: 'Start';
     color: red;
}
p:after{
     content: 'Fin';
     color: red;
}
 
< p  >Hello</ p >

4 伪类选择器:  专用于控制链接的显示效果,visited与active必须写在最后!! :

  a:link(没有接触过的链接),用于定义了链接的常规状态。

  a:hover(鼠标放在链接上的状态),用于产生视觉效果。

  a:visited(访问过的链接),用于阅读文章,能清楚的判断已经访问过的链接。

  a:active(在链接上按下鼠标时的状态),用于表现鼠标按下时的链接状态。

     伪类选择器 : 伪类指的是标签的不同状态:

  a ==> 点过状态 没有点过的状态 鼠标悬浮状态 激活状态

1
2
3
4
5
6
7
a:link {color: #FF0000}  /* 未访问的链接 */
 
a:visited {color: #00FF00}  /* 已访问的链接 */
 
a:hover {color: #FF00FF}  /* 鼠标移动到链接上 */
 
a:active {color: #0000FF}  /* 选定的链接 */ 格式: 标签:伪类名称{ css代码; }

三  CSS的常用属性

1  颜色属性:

color                   👉 常用网页颜色搭配

  • HEX(十六进制色:color: #FFFF00 --> 缩写:#FF0)

  • RGB(红绿蓝,使用方式:color:rgb(255,255,0)或者color:rgb(100%,100%,0%))

  • RGBA(红绿蓝透明度,A是透明度在0~1之间取值。使用方式:color:rgba(255,255,0,0.5))

  • HSL(CSS3有效,H表示色调,S表示饱和度,L表示亮度,使用方式:color:hsl(360,100%,50%))

  • HSLA(和HSL相似,A表示Alpha透明度,取值0~1之间。)

transparent

  • 全透明,使用方式:color: transparent;

opacity

  • 元素的透明度,语法:opacity: 0.5;

  • 属性值在0.0到1.0范围内,0表示透明,1表示不透明。

  • filter滤镜属性(只适用于早期的IE浏览器,语法:filter:alpha(opacity:20);)。

1
2
3
4
5
6
7
8
9
10
11
12
<div style= "color:blueviolet" >ppppp</div>
 
<div style= "color:#ffee33" >ppppp</div>
 
<div style= "color:rgb(255,0,0)" >ppppp</div>
 
<div style= "color:rgba(255,0,0,0.5)" >ppppp</div>     # a为透明度
 
border 3px  solid  transparent   #透明色    占用位置,却肉眼看不出来,
  
opacity: 0.6 ;    #设置背景颜色透明度
background-color : rgba( 0 , 0 , 0 ,. 6 )

2  字体属性:

font-style: 用于规定斜体文本

  • normal  文本正常显示

  • italic  文本斜体显示

  • oblique  文本倾斜显示

font-weight: 设置文本的粗细

  • normal(默认)

  • bold(加粗)

  • bolder(相当于<strong>和<b>标签)

  • lighter (常规)

  • 100 ~ 900 整百(400=normal,700=bold)

font-size: 设置字体的大小

  • 默认值:medium

  • <absolute-size>可选参数值:xx-small、 x-small、 small、 medium、 large、 x-large、 xx-large

  • <relative-size>相对于父标签中字体的尺寸进行调节。可选参数值:smaller、 larger

  • <percentage>百分比指定文字大小。

  • <length>用长度值指定文字大小,不允许负值。

font-family:字体名称

  • 使用逗号隔开多种字体(优先级从前向后,如果系统中没有找到当前字体,则往后面寻找)

font简写属性

  • 语法:顺序:font-style | font-variant | font-weight | font-size | line-height | font-family

  • font 属性的值应按以下次序书写(各个属性之间用空格隔开)

  • font-size和line-height只能通过斜杠/组成一个值,不能分开写

  • 这种简写方法只有在同时指定font-size和font-family属性时才起作用。

  • 如果你没有设定font-weight, font-style, 以及 font-varient ,他们会使用缺省值

复制代码
font-size:  20px / 50% / larger

font-family:  'Lucida Bright'

font-weight:  lighter / bold / border 

<h1 style="font-style: oblique">Abcd</h1>  #字体倾斜

.font{font:italic small-caps bold 12px/1.5em arial,verdana;}
复制代码

3  背景属性:

background-color 背景颜色

background-image 设置图像为背景

  • url("http://images.cnblogs.com/cnblogs_com/DOnkey-fly/603718/o_background.png");  图片地址

  • background-image:linear-gradient(green,blue,yellow,red,black); 颜色渐变效果

background-position 设置背景图像的位置坐标

  • background-position: center center; 图片置中,x轴center,y轴center

  • 1px -195px  截取图片某部分,分别代表坐标x,y轴

background-repeat 设置背景图像不重复平铺

  • no-repeat 设置图像不重复,常用

  • round 自动缩放直到适应并填充满整个容器

  • space 以相同的间距平铺且填充满整个容器

background-attachment 背景图像是否固定或者随着页面的其余部分滚动

background 简写

  • background: url("o_ns.png") no-repeat 0 -196px;

  • background: url("o_ns.png") no-repeat center bottom 15px;

  • background: url("o_ns.png") no-repeat left 30px bottom 15px;

1
2
3
4
5
6
7
8
9
10
11
12
13
padding: 0px 1em !important; border-radius: 0px !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; white-space: nowrap !important;"> 
background-image: url('1.jpg');
 
background-repeat: no-repeat;  (repeat:平铺满)
 
background-position: center center  /(20px 20px);
                     (横向:left center right)    (纵向:top center bottom)
 
组合简写:
<body style="background: 20px 20px no-repeat #ff4 url('1.jpg')">
 
<div style="width: 300px;height: 300px;background: 20px 20px no-repeat #ff4 url('1.jpg')">

注意:

  如果将背景属性加在body上,要记得给body加上一个height,否则结果异常,这是因为body为空,无法撑起背景图片,

  另外,如果此时要设置一个width=100px,你也看不出效果,除非你设置出html。   

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         html{
 8             background-color: antiquewhite;
 9             
10         }
11         body{
12             width: 100px;
13             height: 600px;
14             background-color: deeppink;
15             background-image: url(1.jpg);
16             background-repeat: no-repeat;
17             background-position: center center;
18         }
19     </style>
20 </head>
21 <body>
22 
23 </body>
24 </html>
View Code

4  文本属性:

 white-space: 设置元素中空白的处理方式

  • normal:默认处理方式。

  • pre:保留空格,当文字超出边界时不换行

  • nowrap:不保留空格,强制在同一行内显示所有文本,直到文本结束或者碰到br标签

  • pre-wrap:保留空格,当文字碰到边界时换行

  • pre-line:不保留空格,保留文字的换行,当文字碰到边界时换行

注意: 此处所说的空白处理为标签内的内容间的空白以及换行。

1
2
3
4
5
<div>Hello !< / div>
 
<div>                    # 空格
     Hello !              # 换行
< / div>  

direction: 规定文本的方向 

  • ltr 默认,文本方向从左到右。

  • rtl 文本方向从右到左。

text-align: 文本的水平对齐方式 

  • left

  • center

  • right

line-height: 文本行高

  • normal 默认

vertical-align: 文本所在行高的垂直对齐方式

  • baseline 默认

  • sub 垂直对齐文本的下标,和<sub>标签一样的效果

  • super 垂直对齐文本的上标,和<sup>标签一样的效果

  • top 对象的顶端与所在容器的顶端对齐

  • text-top 对象的顶端与所在行文字顶端对齐

  • middle 元素对象基于基线垂直对齐

  • bottom 对象的底端与所在行的文字底部对齐

  • text-bottom 对象的底端与所在行文字的底端对齐

text-indent: 文本缩进

letter-spacing: 添加字母之间的空白

word-spacing: 添加每个单词之间的空白

text-transform: 属性控制文本的大小写

  • capitalize 文本中的每个单词以大写字母开头。

  • uppercase 定义仅有大写字母。

  • lowercase 定义仅有小写字母。

text-overflow: 文本溢出样式

  • clip 修剪文本。

  • ellipsis 显示省略符号...来代表被修剪的文本。

  • string 使用给定的字符串来代表被修剪的文本。

不可独立使用必须结合overflow:hidden;  white-space:nowrap;才生效。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         
 7         <style type="text/css">
 8             .t1{
 9                 width: 400px;
10                 border: 1px solid red;
11                 white-space: nowrap;
12                 overflow: hidden;
13                 text-overflow: ellipsis;
14             }
15             .t1:hover{
16                 
17                 overflow: visible;
18             }
19         </style>
20     </head>
21     <body>
22         <div class="t1">
23             2016年首次中央深改组会议清晰划分了各类改革主体的责任:专项小组对本领域改革负有牵头抓总、特别是协调解决矛盾的责任,既要抓统筹部署,也要抓督察落实;改革牵头部门是落实中央部署具体改革任务的责任主体;地方党委对本地区全面深化改革承担主体责任;党委书记作为第一责任人,既要挂帅、又要出征……
24 
25 2016年4月,习总书记来到中国农村改革的发源地安徽小岗村。在这里,他又谆谆告诫各级党委和政府及其领导干部,要把自己摆进去想改革、议改革、抓改革,争当击楫中流的改革先锋。在党中央率先垂范下,中央和国家有关部门算大账、总账、长远账,坚决地破、坚决地改,一步一步往前推、一层一层往下落,以更加积极主动的精神研究和推进改革;地方各级党委真刀真枪抓实目标任务、抓实精准落地、抓实探索创新、抓实跟踪问效、抓实机制保障,使各项改革更加富有成效。
26             
27             
28         </div>
29     </body>
30 </html>
单行文字溢出demo
 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         
 7         <style type="text/css">
 8             .t1{
 9                 width: 400px;
10                 border: 1px solid red;
11                 overflow: hidden;
12                 text-overflow: ellipsis;
13                 display: -webkit-box;  
14                 -webkit-line-clamp: 3;  
15                 -webkit-box-orient: vertical;
16             }
17             .t1:hover{
18                 overflow: visible;
19             }
20         </style>
21     </head>
22     <body>
23         <div class="t1">
24             2016年首次中央深改组会议清晰划分了各类改革主体的责任:专项小组对本领域改革负有牵头抓总、特别是协调解决矛盾的责任,既要抓统筹部署,也要抓督察落实;改革牵头部门是落实中央部署具体改革任务的责任主体;地方党委对本地区全面深化改革承担主体责任;党委书记作为第一责任人,既要挂帅、又要出征……
25 
26 2016年4月,习总书记来到中国农村改革的发源地安徽小岗村。在这里,他又谆谆告诫各级党委和政府及其领导干部,要把自己摆进去想改革、议改革、抓改革,争当击楫中流的改革先锋。在党中央率先垂范下,中央和国家有关部门算大账、总账、长远账,坚决地破、坚决地改,一步一步往前推、一层一层往下落,以更加积极主动的精神研究和推进改革;地方各级党委真刀真枪抓实目标任务、抓实精准落地、抓实探索创新、抓实跟踪问效、抓实机制保障,使各项改革更加富有成效。
27             
28             
29         </div>
30     </body>
31 </html>
多行文字溢出demo

text-decoration: 文本的装饰

  • none 默认。

  • underline 下划线。

  • overline 上划线。

  • line-through 中线。

text-shadow:文本阴影

  • 第一个参数是左右位置

  • 第二个参数是上下位置

  • 第三个参数是虚化效果

  • 第四个参数是颜色

  • text-shadow: 5px 5px 5px #888;

word-wrap:允许长单词或 URL 地址换行到下一行

  • word-wrap: break-word;

word-break:在恰当的断字点进行换行, 在边界换行

  • word-break: break-all;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
font-size: 10px;
 
text-align: center;    文本对齐方式
 
line-height: 200px;  文本行高 通俗的讲,文字高度加上文字上下的空白区域的高度 50%:基于字体大小的百分比
 
p{
width: 200px;
height: 200px;
text-align: center;
 
line-height: 200px;
}
 
text-indent: 150px; 首行缩进,50%:基于父元素(weight)的百分比
 
letter-spacing: 10px;   每一个字母的间隔
 
word-spacing: 20px;    每一个单词的间隔
 
direction: rtl;    设置文本方向
 
text-transform: capitalize;     控制文本的大小写
#capitalize         文本中的每个单词以大写字母开头。
#uppercase  定义仅有大写字母。
#owercase   定义无大写字母,仅有小写字母。

5  边框属性:

border-style:边框样式

  • solid 默认,实线

  • double 双线

  • dotted 点状线条

  • dashed 虚线

border-color:边框颜色

border-width:边框宽度

border-radius:圆角

  • 1个参数:四个角都应用

  • 2个参数:第一个参数应用于 左上、右下;第二个参数应用于 左下、右上

  • 3个参数:第一个参数应用于 左上;第二个参数应用于 左下、右上;第三个参数应用于右下

  • 4个参数:左上、右上、右下、左下(顺时针)

border: 简写

  • border: 2px yellow solid; 

box-shadow:边框阴影

  • 第一个参数是左右位置

  • 第二个参数是上下位置

  • 第三个参数是虚化效果

  • 第四个参数是颜色

  • box-shadow: 10px 10px 5px #888;

1
2
3
4
5
6
7
border-style: solid;
 
border-color: chartreuse;
 
border-width: 20px;
 
组合简写:border: 30px rebeccapurple solid;
 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         
 7         <style type="text/css">
 8             .d1{
 9                 width: 140px;
10                 border:3px solid blue;
11                 border-radius: 25px;
12                 text-align: center;
13             }
14 
15         </style>
16     </head>
17     <body>
18         <div class="d1"> FaceBook! </div>
19     </body>
20 </html>
圆角应用-demo1

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         
 7         <style type="text/css">
 8             .d1{
 9                 width: 100px;
10                 height: 100px;
11                 border:3px solid blue;
12                 background-color: blue;
13                 border-radius: 30px 10px;
14                 display: inline-block;
15             }
16             .d2{
17                 width: 100px;
18                 height: 100px;
19                 background-color: red;
20                 border-radius: 30px 10px 25px;
21                 display: inline-block;
22             }
23             .d3{
24                 width: 100px;
25                 height: 100px;
26                 background-color: darkslategray;
27                 border-radius: 0 100px 0 100px;
28                 display: inline-block;
29             }
30             .d4{
31                 width: 100px;
32                 height: 100px;
33                 background-color: greenyellow;
34                 border-radius: 0 100px 100px;
35                 display: inline-block;
36             }
37             .d5{
38                 width: 100px;
39                 height: 100px;
40                 background-color: green;
41                 border-radius: 50px;
42                 display: inline-block;
43             }
44         </style>
45     </head>
46     <body>
47         <div class="d1"></div>
48         <div class="d2"></div>
49         <div class="d3"></div>
50         <div class="d4"></div>
51         <div class="d5"></div>
52     </body>
53 </html>
圆角应用-demo2

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style>
 7             .d1{
 8                 display: inline-block;
 9                 border-top: 50px solid red;
10                 border-right: 50px solid blue;
11                 border-bottom: 50px solid yellow;
12                 border-left: 50px solid green;
13             }
14             .d2{
15                 display: inline-block;
16                 border-top: 50px solid red;
17                 border-right: 0px solid blue;
18                 border-bottom: 0px solid yellow;
19                 border-left: 50px solid green;
20             }
21             .d3{
22                 display: inline-block;
23                 border: 50px transparent solid;
24                 border-top: 50px red solid;
25             }
26             .d4{
27                 display: inline-block;
28                 border: 50px transparent solid;
29                 border-top: 50px black solid;
30                 border-right:0;
31             }
32         </style>
33     </head>
34     <body>
35         <div class="d1"></div>
36         <div class="d2"></div>
37         <div class="d3"></div>
38         <div class="d4"></div>
39     </body>
40 </html>
边框应用-demo3

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style>
 7             .outer{
 8                 width: 400px;
 9                 height: 150px;
10                 border: 1px solid red;
11                 position: relative;
12             }
13             .inner{
14                 width: 350px;
15                 height: 150px;
16                 background-color: #ddd;
17                 float: right;
18             }
19             .content{
20                 width: 370px;
21                 height: 30px;
22                 color: white;
23                 background-color: #2F4F4F;
24                 position: absolute;
25                 left: 30px;
26                 top:50px;
27                 z-index: 20;
28             }
29             .little-block{
30                 display: inline-block;
31                 border: 20px transparent solid;
32                 border-top:10px solid gray;
33                 border-right:0px solid blue;
34                 position: absolute;
35                 top: 79px;
36                 left: 30px;
37                 z-index: 10;
38             }
39         </style>
40     </head>
41     <body>
42         <div class="outer">
43             <div class="inner"></div>
44             <div class="content">你好 , 世界!</div>
45             <div class="little-block"></div>
46         </div>
47     </body>
48 </html>
边框应用-demo4

6  列表属性

list-style-type: 列表项标志的类型

  • none 去除标志

  • decimal-leading-zero;  02.

  • square;  方框

  • circle;  空心圆

  • upper-alph;  大写字母

  • disc;   实心圆

list-style-image:将图象设置为列表项标志

list-style-position:列表项标志的位置

  • inside

  • outside

list-style:缩写

  • list-style:square inside url('/i/arrow.gif');
复制代码
ul,ol{ list-style: decimal-leading-zero;    # 数字01,02,
  list-style: none;    # 不显示
  list-style: circle;    # 空心圆
  list-style: upper-alpha;    # 字母A,B,
  list-style: disc;   # 实心圆(默认)
}
复制代码

7  dispaly属性

 

1
2
3
4
none       # 不显示
block      # 块元素
inline     # 设置为内联 , 内联标签不能设置宽和高!!
inline - block   # 设置为内联,并且可以设置高度,宽度!!

 

8  盒子模型

  padding:用于控制内容与边框之间的距离;

  margin: 用于控制元素与元素之间的距离;margin的最基本用途就是控制元素周围空间的间隔,从视觉角度上达到相互隔开的目的。

我们通过设置width 和 height 只是设置了content的宽度与高度!

盒子的宽度(高度)=content+padding+border+margin

实例: 300px*300px的盒子装着100px*100px的盒子,分别通过margin和padding设置将小盒子移到大盒子的中间

注意1:边框在默认情况下会定位于浏览器窗口的左上角,但是并没有紧贴着浏览器的窗口的边框。

  这是因为body本身也是一个盒子(外层还有html),在默认情况下,body距离html会有若干像素的margin,具体数值因各个浏览器不尽相同,所以body中的盒子不会紧贴浏览器窗口的边框了。

1
2
3
4
5
6
7
8
9
10
11
# 验证方法:
body{
     border: 1px solid;
    
}
 
>>>>解决方法:
 
body{
     margin: 0;
}

注意2:margin collapse(边界塌陷或者说边界重叠)

  外边距的重叠只产生在普通流文档的上下外边距之间,这个看起来有点奇怪的规则,其实有其现实意义。

  设想,当我们上下排列一系列规则的块级元素(如段落P)时,那么块元素之间因为外边距重叠的存在,段落之间就不会产生双倍的距离。

  1.兄弟div:上面div的margin-bottom和下面div的margin-top会塌陷,也就是会取上下两者margin里最大值作为显示值

  2.父子div如果 父级div中没有 border,padding,inline content,子级div的margin会一直向上找,直到找到某个标签包括border,padding,inline content 中的其中一个,然后按此div 进行margin !!!

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <style  rel='stylesheet' type="text/css">
 7             body{
 8                 margin: 0px;
 9             }
10 
11             .div1{
12                 background-color: aqua;
13                 width: 300px;
14                 height: 300px;
15             }
16             .div2{
17                 background-color: blueviolet;
18                 width: 100px;
19                 height: 100px;
20                 margin: 20px;
21 
22             }
23             .div3{
24                 background-color: blueviolet;
25                 width: 100px;
26                 height: 100px;
27                 margin: 100px;
28 
29             }    
30 
31     </style>
32 </head>
33 <body>
34     <div style="background-color: cadetblue;width: 300px;height: 300px;border: solid">
35         
36         <div  class="div3"></div>
37     </div>
38 
39     <div class="div1">
40         <div class="div2"></div>
41         <div class="div2"></div>
42     </div>
43 </html>
View Code
1
2
3
4
解决方法:
     1: border:1px solid transparent;
     2: padding:1px;   必须大于0
     3: overflow:hidden;

9  float

首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的

  可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为div元素是独占一行的。注意,以上这些理论,是指标准流中的div。

  无论多么复杂的布局,其基本出发点均是:“如何在一行显示多个div元素”。

  浮动可以理解为让某个div元素脱离标准流,漂浮在标准流之上,和标准流不是一个层次。

例如,假设上图中的绿色div2浮动,那么它将脱离标准流,但div1、div3、div4仍然在标准流当中,所以div3会自动向上移动,占据div2的位置,重新组成一个流。如图:

  从图中可以看出,由于对div2设置浮动,因此它不再属于标准流,div3自动上移顶替div2的位置,div1、div3、div4依次排列,成为一个新的流。

  又因为浮动是漂浮在标准流之上的,因此div2挡住了一部分div3,div3看起来变“矮”了

  这里div2用的是左浮动( float:left;),可以理解为漂浮起来后靠左排列,右浮动(float:right;)当然就是靠右排列。 这里的靠左、靠右是说页面的左、右边缘。

  如果我们把div2采用右浮动,会是如下效果:

  此时div2靠页面右边缘排列,不再遮挡div3,读者可以清晰的看到上面所讲的div1、div3、div4组成的流。

  目前为止我们只浮动了一个div元素,多个呢?

  下面我们把div2和div3都加上左浮动,效果如图:

  同理,由于div2、div3浮动,它们不再属于标准流,因此div4会自动上移,与div1组成一个“新”标准流,而浮动是漂浮在标准流之上,因此div2又挡住了div4。

  当同时对div2、div3设置浮动之后,div3会跟随在div2之后,不知道读者有没有发现,一直到现在,div2在每个例子中都是浮动的,但并没有跟随到div1之后。

  因此,我们可以得出一个重要结论

  1.假如某个div元素A是浮动的,如果A元素上一个元素也是浮动的,那么A元素会跟随在上一个元素的后边(如果一行放不下这两个元素,那么A元素会被挤到下一行);如果A元素上一个元素是标准流中的元素,那么A的相对垂直位置不会改变,也就是说A的顶部总是和上一个元素的底部对齐。

  2.div的顺序是HTML代码中div的顺序决定的。

  3.靠近页面边缘的一端是前,远离页面边缘的一端是后。

为了帮助读者理解,再举几个例子。

假如我们把div2、div3、div4都设置成浮动,效果如下:

  先从div4开始分析,它发现上边的元素div3是浮动的,所以div4会跟随在div3之后;div3发现上边的元素div2也是浮动的,所以div3会跟随在div2之后;而div2发现上边的元素div1是标准流中的元素,因此div2的相对垂直位置不变,顶部仍然和div1元素的底部对齐。由于是左浮动,左边靠近页面边缘,所以左边是前,因此div2在最左边。

 

假如把div2、div3、div4都设置成浮动,效果如下:

  道理和左浮动基本一样,只不过需要注意一下前后对应关系。由于是右浮动,因此右边靠近页面边缘,所以右边是前,因此div2在最右边。

 

假如我们把div2、div4浮动,效果图如下:

  依然是根据结论,div2、div4浮动,脱离了标准流,因此div3将会自动上移,与div1组成标准流。div2发现上一个元素div1是标准流中的元素,因此div2相对垂直位置不变,与div1底部对齐。div4发现上一个元素div3是标准流中的元素,因此div4的顶部和div3的底部对齐,并且总是成立的,因为从图中可以看出,div3上移后,div4也跟着上移,div4总是保证自己的顶部和上一个元素div3(标准流中的元素)的底部对齐

 

至此,恭喜读者已经掌握了添加浮动,但还有清除浮动,有上边的基础清除浮动非常容易理解。

经过上边的学习,可以看出:元素浮动之前,也就是在标准流中,是竖向排列的,而浮动之后可以理解为横向排列。

 

清除浮动可以理解为打破横向排列。

清除浮动的关键字是clear,官方定义如下:

复制代码
语法:

clear : none | left | right | both

取值:

none  :  默认值。允许两边都可以有浮动对象
left   :  不允许左边有浮动对象
right  :  不允许右边有浮动对象
both  :  不允许有浮动对象
复制代码

定义非常容易理解,但是读者实际使用时可能会发现不是这么回事。

定义没有错,只不过它描述的太模糊,让我们不知所措。

根据上边的基础,假如页面中只有两个元素div1、div2,它们都是左浮动,场景如下:

  此时div1、div2都浮动,根据规则,div2会跟随在div1后边,但我们仍然希望div2能排列在div1下边,就像div1没有浮动,div2左浮动那样。

  这时候就要用到清除浮动(clear),如果单纯根据官方定义,读者可能会尝试这样写:在div1的CSS样式中添加clear:right;,理解为不允许div1的右边有浮动元素,由于div2是浮动元素,因此会自动下移一行来满足规则。

  其实这种理解是不正确的,这样做没有任何效果。

  对于CSS的清除浮动(clear),一定要牢记:这个规则只能影响使用清除的元素本身,不能影响其他元素。

  怎么理解呢?就拿上边的例子来说,我们是想让div2移动,但我们却是在div1元素的CSS样式中使用了清除浮动,试图通过清除div1右边的浮动元素(clear:right;)来强迫div2下移,这是不可行的,因为这个清除浮动是在div1中调用的,它只能影响div1,不能影响div2。

  要想让div2下移,就必须在div2的CSS样式中使用浮动。

  本例中div2的左边有浮动元素div1,因此只要在div2的CSS样式中使用 clear:left; 来指定div2元素左边不允许出现浮动元素,这样div2就被迫下移一行。

 

那么假如页面中只有两个元素div1、div2,它们都是浮动呢?读者此时应该已经能自己推测场景,如下:

此时如果要让div2下移到div1下边,要如何做呢?

  我们希望移动的是div2,就必须在div2的CSS样式中调用浮动,因为浮动只能影响调用它的元素。

  可以看出div2的右边有一个浮动元素div1,那么我们可以在div2的CSS样式中使用 clear:right; 来指定div2的右边不允许出现浮动元素,这样div2就被迫下移一行,排到div1下边。

总结:

float : 先判断上一个是不是浮动对象, 如果是 紧跟其后(还要判断浮动方向), 如果不是,自己浮起来,脱离文档流! (原来的位置可以被顶替)

clear : both  => 左右都不能有浮动,只针对自己!

清除浮动:

  在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,

  在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外面而影响(甚至破坏)布局的现象。

  这个现象叫浮动溢出,为了防止这个现象的出现而进行的CSS处理,就叫CSS清除浮动。

方式一:

复制代码
.clearfix:after {             <----在类名为“clearfix”的元素内最后面加入内容; 
content: ".";                 <----内容为“.”就是一个英文的句号而已。也可以不写。 
display: block;               <----加入的这个元素转换为块级元素。 
clear: both;                  <----清除左右两边浮动。 
visibility: hidden;           <----可见度设为隐藏。注意它和display:none;是有区别的。visibility:hidden;仍然占据空间,只是看不到而已; 
line-height: 0;               <----行高为0; 
height: 0;                    <----高度为0; 
font-size:0;                  <----字体大小为0; 
} 
.clearfix { *zoom:1;}         <----这是针对于IE6的,因为IE6不支持:after伪类,这个神奇的zoom:1让IE6的元素可以清除浮动来包裹内部元素。
复制代码

  整段代码就相当于在浮动元素后面跟了个宽高为0的空div,然后设定它clear:both来达到清除浮动的效果。 

  之所以用它,是因为,你不必在html文件中写入大量无意义的空标签,又能清除浮动。 

方式二:

overflow:hidden;

overflow:hidden的含义是超出的部分要裁切隐藏

  float的元素虽然不在普通流中,但是他是浮动在普通流之上的,可以把普通流元素+浮动元素想象成一个立方体。

  如果没有明确设定包含容器高度的情况下,它要计算内容的全部高度才能确定在什么位置hidden,这样浮动元素的高度就要被计算进去。这样包含容器就会被撑开,清除浮动。

 

10 position

1.static,默认值 static:无特殊定位,对象遵循正常文档流。

  top,right,bottom,left 等属性不会被应用。说到这里我们不得不提一下一个定义——文档流,文档流其实就是文档的输出顺序,也就是我们通常看到的由左到右、由上而下的输出形式,在网页中每个元素都是按照这个顺序进行排序和显示的,而float和position两个属性可以将元素从文档流脱离出来显示。 默认值就是让元素继续按照文档流显示,不作出任何改变。

2.position : relative / absolute

  relative:(相对定位) 对象遵循正常文档流,仅仅定义对本参无影响. 但将依据top,right,bottom,left等属性在正常文档流中偏移位置。而其层叠通过z-index属性定义。

  元素实际上依然占据文档中的原有位置,只是视觉上相对于它在文档中的原有位置移动了

  absolute:(绝对定位) 对象脱离正常文档流,使用top,right,bottom,left等属性进行绝对定位。而其层叠通过z-index属性定义。 

  当指定 position:absolute 时,元素就脱离了文档[即在文档中已经不占据位置了],可以准确的按照设置的 top,bottom,left 和 right 来定位了。

  如果一个元素绝对定位后,其参照物以离自身最近元素是否设置了相对定位,如果有设置将以离自己最近元素定位,如果没有将往其祖先元素寻找相对定位元素, 一直找到html为止。

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <style  rel='stylesheet' type="text/css">
 7             body{
 8                 margin: 0px;
 9             }
10             .father{
11 
12                 position:relative;
13             }
14             .div1{
15                 background-color: red;
16                 width: 600px;
17                 height: 200px;
18                 
19             }
20             .div2{
21                 background-color: blueviolet;
22                 width: 500px;
23                 height: 100px;
24                 position:absolute;
25                 left:300px;
26                 top:80px;
27                 
28             }
29             .div3{
30                 background-color: gray;
31                 width: 400px;
32                 height: 150px;
33             }
34             .div4{
35                 background-color: green;
36                 width: 200px;
37                 height: 100px;
38 
39             }    
40 
41     </style>
42 </head>
43 <body>
44     <div class='father'> 
45     <div class='div1' style='font-size:60px;text-align:center;'>DIV1</div>
46     <div class='div2' style='font-size:60px;text-align:center;'>DIV2</div>
47     <div class='div3' style='font-size:60px;text-align:center;'>DIV3</div>
48     <div class='div4' style='font-size:60px;text-align:center;'>DIV4</div>
49     </div>
50 </html>
View Code

3.position:fixed

  在理论上,被设置为fixed的元素会被定位于浏览器窗口的一个指定坐标,不论窗口是否滚动,它都会固定在这个位置。

  fixed:对象脱离正常文档流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。而其层叠通过z-index属性定义。

  注意点: 一个元素若设置了 position:absolute | fixed; 则该元素就不能设置float。这是一个常识性的知识点,因为这是两个不同的流,一个是浮动流,另一个是“定位流”。

  但是 relative 却可以。因为它原本所占的空间仍然占据文档流。

 

仅使用margin属性布局绝对定位元素

  此情况,margin-bottom 和margin-right的值不再对文档流中的元素产生影响,因为该元素已经脱离了文档流。

  另外,不管它的祖先元素有没有定位,都是以文档流中原来所在的位置上偏移参照物。  

  图9中,使用margin属性布局相对定位元素。

  层级关系为:

  <div ——————————— position:relative;

  <div—————————-没有设置为定位元素,不是参照物

  <div———————-没有设置为定位元素,不是参照物

  <div box1

  <div box2 ——–position:absolute; margin-top:50px; margin-left:120px;

  <div box3

  效果图:

  

 

补充:

1.css属性可以定义给多个class!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< style >
.bd .div1{
    
     width: 600px;
     height: 200px;             
}
.bd .test{
    
}
</ style >
 
 
< div  class='bd'>
     < div  class='div1 test'>DIV1</ div >    # 最终效果为:#ddd
</ div >

2.background-position:  定位背景图像

background-position 属性设置背景图像的起始位置。

这个属性设置背景原图像(由 background-image 定义)的位置,背景图像如果要重复,将从这一点开始。

实例:

1
2
3
4
5
6
7
8
9
10
.dj{
     width:18px;
     height:16px;
     border:solid 1px red;
     background:url('http://misc.360buyimg.com/jdf/1.0.0/unit/globalImages/1.0.0/jd2015img.png');
     background-repeat:no-repeat;
     background-position:0 -58px;
}
 
< div  class='dj'></ div >

可以通过调节:background-position 的值,显示图像!

   background-position : 0 0px

  background-position : 0 -58px

通过background-position 实现了从图片上抠图的效果!

3.z-index:

z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素 总是会处于 堆叠顺序较低的元素的前面 !

仅在定位元素上有效(例:position:absolute;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
.z-index 1  {
     width 100px ;
     height 100px ;
     background-color : yellow;
     position absolute ;
     z-index -1 ;
}
.z-index 2  {
     width 100px ;
     height 100px ;
     background-color red ;
     position absolute ;
     top 20px ;
     left 20px ;
     z-index 5 ;
}
 
<div class= "z-index1" ></div>
<div class= "z-index2" ></div>

4.overflow

  • visible 默认值,内容不会被修剪,会呈现在元素框之外。

  • hidden 内容会被修剪,并且其余内容是不可见的。

  • scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。

  • auto 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。

          scroll;      auto;

5.outline  边框轮廓

轮廓不占据页面空间,也不一定是矩形。

1
outline none ;   # 可以去除input标签的轮廓!!

除了IE以外的浏览器都直接支持outline。只有规定了!DOCUMENT之后的IE8以上版本的浏览器才支持outline。

  • outline-width 轮廓宽度

  • outline-color 轮廓颜色

  • outline-style 轮廓样式

6.zoom  缩放比例

7.transform、transition 动画效果

transform 转换,变形

  • origin 定义旋转基点(left top center right bottom 坐标值)    transform-origin: 50px 50px; transform-origin: left;。

  • rotate 旋转            transform:rotate(50deg) 旋转角度可以为负数,需要先定义origin。

  • skew  扭曲             transform:skew(50deg,50deg)  分别为相对x轴倾斜,相对y轴倾斜。

  • scale  缩放             transform:scale(2,3) 横向放大2倍,纵向放大3倍;transform:scale(2) 横竖都放大2倍。 

  • translate 移动        transform:translate(50px, 50px) 分别为相对x轴移动,相对y轴移动。

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title>nick</title>
 6     <meta charset="utf-8" />
 7     <style type="text/css">
 8         div {
 9             border: 1px solid black;
10             height: 30px;
11             width: 30px;
12             background-color: yellow;
13  
14             /*transform-origin: 50px 50px;*/
15             transform-origin: left;
16             transform: rotate(50deg);
17             /*transform: skew(50deg,50deg);*/
18             /*transform: translate(50px,50px);*/
19             /*transform: scale(2);*/
20         }
21     </style>
22 </head>
23 <body>
24     <div></div>
25 </body>
26 </html>
27 
28 demo
View Code

Transition 平滑过渡

  • transition-property:           变换的属性(none(没有属性改变)、all(所有属性改变)、具体属性)

  • transition-duration:           变换持续时间

  • transition-timing-function: 变换的速率(ease:(逐渐变慢)、linear:(匀速)、ease-in:(加速)、ease-out:(减速)、ease-in-out:(加速然后减速)、cubic-bezier:(自定义时间曲线))

  • transition-delay:               变换延迟时间

  • transition:                        缩写

 1 #支持执行transition效果的属性
 2 
 3 Property Name    Type
 4 background-color    as color
 5 background-position    as repeatable list of simple list of length, percentage, or calc
 6 border-bottom-color    as color
 7 border-bottom-width    as length
 8 border-left-color    as color
 9 border-left-width    as length
10 border-right-color    as color
11 border-right-width    as length
12 border-spacing    as simple list of length
13 border-top-color    as color
14 border-top-width    as length
15 bottom    as length, percentage, or calc
16 clip    as rectangle
17 color    as color
18 font-size    as length
19 font-weight    as font weight
20 height    as length, percentage, or calc
21 left    as length, percentage, or calc
22 letter-spacing    as length
23 line-height    as either number or length
24 margin-bottom    as length
25 margin-left    as length
26 margin-right    as length
27 margin-top    as length
28 max-height    as length, percentage, or calc
29 max-width    as length, percentage, or calc
30 min-height    as length, percentage, or calc
31 min-width    as length, percentage, or calc
32 opacity    as number
33 outline-color    as color
34 outline-width    as length
35 padding-bottom    as length
36 padding-left    as length
37 padding-right    as length
38 padding-top    as length
39 right    as length, percentage, or calc
40 text-indent    as length, percentage, or calc
41 text-shadow    as shadow list
42 top    as length, percentage, or calc
43 vertical-align    as length
44 visibility    as visibility
45 width    as length, percentage, or calc
46 word-spacing    as length
47 z-index    as integer
transition 支持的属性
 1 #property 指定属性对应类型
 2 
 3 1、color: 通过红、绿、蓝和透明度组件变换(每个数值单独处理),如:background-color,border-color,color,outline-color等CSS属性;
 4 
 5 2、length:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,padding,outline-width,margin,min-width,min- height,max-width,max-height,line-height,height,border-width,border- spacing,background-position等属性;
 6 
 7 3、percentage:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等属性;
 8 
 9 4、integer 离散步骤(整个数字),在真实的数字空间,以及使用floor()转换为整数时发生,如:outline-offset,z-index等属性;
10 
11 5、number真实的(浮点型)数值,如:zoom,opacity,font-weight等属性;
12 
13 6、transform list。
14 
15 7、rectangle:通过x、 y、 width和height(转为数值)变换,如:crop;
16 
17 8、visibility:离散步骤,在0到1数字范围之内,0表示“隐藏”,1表示完全“显示”,如:visibility;
18 
19 9、shadow:作用于color、x、y、和blur(模糊)属性,如:text-shadow;
20 
21 10、gradient:通过每次停止时的位置和颜色进行变化。它们必须有相同的类型(放射状的或是线性的)和相同的停止数值以便执行动画,如:background-image;
22 
23 11、paint server (SVG):只支持下面的情况:从gradient到gradient以及color到color,然后工作与上面类似;
24 
25 12、space-separated list of above:如果列表有相同的项目数值,则列表每一项按照上面的规则进行变化,否则无变化;
26 
27 13、a shorthand property:如果缩写的所有部分都可以实现动画,则会像所有单个属性变化一样变化。
property 指定属性对应类型

实例:demo

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style type="text/css">
 7         .div2{
 8             width:100px;
 9             height:100px;
10             background:pink;
11             transition:width 3s,background-color 3s;
12         }
13         .div2:hover{
14             width: 300px;
15             background-color:gray;
16         }
17         .img-1{
18             width: 600px;
19             height: 300px;
20             background-image: url("http://images.cnblogs.com/cnblogs_com/suoning/845162/o_sea.jpg");
21             background-repeat: no-repeat;
22             background-size: 600px;
23             transition-duration: 10s;
24             transition-timing-function: ease;
25             transition-property: background-size;
26         }
27         .img-1:hover{
28             background-size: 2000px;
29         }
30     </style>
31     </head>
32     <body>
33         <div class="div1"></div>
34         <div class="div2"></div>
35         <p>鼠标放在以下图片上,There will be a miracle:</p>
36         
37         <div class="img-1"></div>
38     </body>
39 </html>
demo
 

鼠标放在以下图片上,There will be a miracle:

 

 

练习题:

1.http://dig.chouti.com/

2.http://v.dangdang.com/

3.商城页面

 

补充:

1.关于 line-height 和 height  居中效果

1
2
3
4
5
a{
display:inline-block;
height: 40px;
text-align:center;
}

line-height是把多余的高度平分到上面和下面 所以明显会居中。设置height就是自然而然的从上到下排列所以贴顶了!

2.float 效果影响背景色

子级div 使用float 效果时,父级div的背景色会失效

原因: 当块元素的高度是默认或者auto时,W3C 在可视化格式模型中规定了高度的计算细节,其中有一条就是:如果元素包含浮动元素,那么浮动元素不参与高度计算。所以没有撑起父级元素颜色失效。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style type="text/css">
 7             .father{
 8                 background-repeat-y: red;
 9                 width: 200px;
10                 height: 200px;
11             }
12             .father .item{
13                 background-color: green;
14                 float: left;
15             }
16         </style>
17     </head>
18     <body>
19         <div class="father">
20             <div class="item">123</div>
21             <div class="item">456</div>
22         </div>
23     </body>
24 </html>
View Code

解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1 . 父级div中 添加  overflow : hidden
# 当块元素上有 “ overflow : hidden ” 时,会创建 Block Formatting context,这时,元素在计算其高度时,浮动的子元素会被计算在内。
 
2 .父级div里面 额外添加一个<div style= "clear:both" ></div>
 
3 .自定义一个样式, 添加到父级div class 后面
.clearfix:after{
     content 'x' ;            //在div末尾插入一个x
     visibility hidden ;      //隐藏这个x值
     height 0px ;             //设置高度为 0
     clear : both ;
     display : block ;
}
 
<div class= "father clearfix" >
     <div class= "item" > 123 </div>
     <div class= "item" > 456 </div>
</div>

3.hover

hover 后可以添加选择器指明需要修改的样式

1
2
3
.c:hover .item{
    
}

4.透明色

1
2
3
4
border: 3px solid transparent  #透明色    占用位置,却肉眼看不出来,
 
opacity:0.6;   设置背景颜色透明度
font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px;">5.图片的使用

 

如果需要在div里面插入img标签的图片,div中需要使用  overflow:hidden  来防止图片超出边界撑开div

6.position 使用

position 常使用于 div的定位

1
2
3
4
5
6
7
8
# position: relative 对本身div没影响
 
position:absolute
left:0
top:0
right:0
buttom:0
=>  表示全部占满一个屏幕

7.小尖角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.angle{
     border-top:30px solid red ;
     border-left:30px solid blue ;
     border-bottom:30px solid green ;
     border-right:30px solid yellow ;
     display: inline-block;         
}
.up{
     border: 30px solid transparent;
     border-top:30px solid red ;
     display: inline-block;     
}
.down{
     border: 30px solid transparent;
     border-bottom:30px solid red ;
     display: inline-block; 
}
 
 
< div  class="angle"></ div >
< div  class="up"></ div >
< div  class="down"></ div >

8.图标素材

现成的图标网站,  http://fontawesome.io/  里面有各种小图标, 下载解压,页面中,引用css即可

1
2
3
< link  rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.css">
 
< span  class="fa fa-bullhorn"></ span >  #具体参考官方网站中演示代码

9.a标签中嵌套图片(默认样式)

  a 标签中有边框 (ie浏览器下)

1
2
3
img{
     border:0;
}

10.目录结构

1
2
3
4
5
6
7
8
9
10
11
hao123.com
     -app
         -s1.html
         -s2.html
     -css
         -commons.css
     -script
         -commons.js
     -plugin
         -bootstrap
         -bxslider

11.样式.重复修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.c1{
    
}
.c2{
    
}
 
< div  class="c1 c2">1</ div >   # c2离得近所以,使用c2中样式, div背景色被设置成绿色!
 
# 为了避免这种情况可以使用 !important; 保证了使用原来定义的背景色c1,防止被篡改
.c1{
     !important;
}
.c2{
    
}

12.布局,分左右

使用position方法 效果最好!

复制代码
.pg-left{
    width: 180px;
    position: absolute;
    left: 0;
    background-color: red;
}
.pg-right{
    height: 600px;
    position: absolute;
    left: 184px;
    right: 0;
    overflow: auto;
    background-color: lightcoral;
}
复制代码

13.设置滚动条

先设置一个高度,再使用  overflow:auto   超出大小,出现滚动条!

14.input框插入图标

思路: 利用 absolute 插入图标  ,padding 来设置内边距,留出空白位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.c1{
  position: relative;
}
.c1-input{
  width: 180px;
  padding-right: 20px;
}
.c1-icon{
  position: absolute;
  left:190px;
  top:4px;
}
 
< div  class="c1">
     < input  type="text" class="c1-input" />
     < span  class="c1-icon">R</ span >
</ div >

15.全屏提示框(模态对话框)

思路:底层 + 遮罩层(position:fixed 透明) + 顶层 (div fixed)居中显示

1.两个fixed 在一起. z-index设置顺序

2.居中 top left 可以用百分比!  margin-left 负值左移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.middle{
     position: fixed;
     left: 0;
     right: 0;
     top: 0;
     bottom: 0;
    
     z-index: 10;
}
.roof{
     position: fixed;
     top: 50%;
     left: 50%;
     margin-left:-200px;        # 使用百分比后,左上角在中心点,还需要适当调整
     margin-top: -200px;      
     width: 500px;
     height: 300px;
    
     z-index: 99;
}
         
< div  class="middle"></ div >
< div  class="roof"></ div >

16.加降框

input 默认有个padding  需要使用 padding:0 去除

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style>
 7             .left{
 8                 float: left;
 9             }
10             .count{
11                 width: 120px;
12                 height: 20px;
13                 border: 1px solid #ddd;
14             }
15             .cut{
16                 width: 20px;
17                 height: 20px;
18                 text-align: center;
19                 line-height: 20px;
20                 cursor: pointer;
21             }
22             .plus{
23                 width: 20px;
24                 height: 20px;
25                 text-align: center;
26                 line-height: 20px;
27                 cursor: pointer;
28             }
29             .user-insert{
30                 padding: 0;
31                 border: 0;
32                 width: 78px;
33                 height: 20px;
34                 border-left: 1px solid #ddd;
35                 border-right: 1px solid #ddd;
36             }
37         </style>
38     </head>
39     <body>
40         <div class="count">
41             <div class="cut left"  onclick="Cut();">-</div>
42             <div><input type="text" id='bugs' class='user-insert left' value="1" /></div>
43             <div class="plus left" onclick="Plus();">+</div>
44         </div>
45         
46         
47         <script type="text/javascript">
48             function Plus(){                
49                 var old_str = document.getElementById('bugs').value;
50                 var old_int = parseInt(old_str);
51                 var new_int = old_int + 1;
52                 document.getElementById('bugs').value = new_int;
53             }
54             function Cut(){
55                 var old_str = document.getElementById('bugs').value;
56                 var old_int = parseInt(old_str);
57                 var new_int = old_int - 1;
58                 document.getElementById('bugs').value = new_int;
59             }
60         </script>
61     </body>
62 </html>
View Code

17.cursor: 鼠标的类型形状

cursor:pointer

请把鼠标移动到单词上,可以看到鼠标指针发生变化:

Auto
Crosshair
Default
Pointer
Move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
not-allowed

18. display: inline-block

设置 inline-block 让元素刚好占满一行,却无法实现

设置父级 font-size:0  然后子级元素重新设置font-size 就可以了

产生原因:  标签与标签之间的空白位置,占用了1个字符!

19. 防止缩小浏览器宽度,页面内容挤在一起

设置一个width 固定宽度,不要用百分比!

20.去掉谷歌input记住账号或密码时默认出现的黄色背景

在谷歌浏览器会默认记住账号,而记住账号之后其input的背景会变成黄色,解决的办法如下:

方法一:直接用css的内阴影来覆盖黄色,代码如下:

1
2
3
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0px 1000px white inset;
}

在form标签上直接关闭了表单的自动填充功能: autocomplete = ”off” 

21.英文换行显示,边界单词切割成两部分,显示

1
2
word- break break -all;
word-wrap: break -word;

22.垂直对齐

1
vertical-align: ..px;

23.上传表单,美化

1 <a href="javascript:;" class="a-upload">
2     <input type="file" name="" id="">点击这里上传文件
3 </a>
4 
5 <a href="javascript:;" class="file">选择文件
6     <input type="file" name="" id="">
7 </a>
Dom结构
 1 /*a  upload */
 2 .a-upload {
 3     padding: 4px 10px;
 4     height: 20px;
 5     line-height: 20px;
 6     position: relative;
 7     cursor: pointer;
 8     color: #888;
 9     background: #fafafa;
10     border: 1px solid #ddd;
11     border-radius: 4px;
12     overflow: hidden;
13     display: inline-block;
14     *display: inline;
15     *zoom: 1
16 }
17 
18 .a-upload  input {
19     position: absolute;
20     font-size: 100px;
21     right: 0;
22     top: 0;
23     opacity: 0;
24     filter: alpha(opacity=0);
25     cursor: pointer
26 }
27 
28 .a-upload:hover {
29     color: #444;
30     background: #eee;
31     border-color: #ccc;
32     text-decoration: none
33 }
样式一
 1 .file {
 2     position: relative;
 3     display: inline-block;
 4     background: #D0EEFF;
 5     border: 1px solid #99D3F5;
 6     border-radius: 4px;
 7     padding: 4px 12px;
 8     overflow: hidden;
 9     color: #1E88C7;
10     text-decoration: none;
11     text-indent: 0;
12     line-height: 20px;
13 }
14 .file input {
15     position: absolute;
16     font-size: 100px;
17     right: 0;
18     top: 0;
19     opacity: 0;
20 }
21 .file:hover {
22     background: #AADFFD;
23     border-color: #78C3F3;
24     color: #004974;
25     text-decoration: none;
26 }
样式二

上面美化,把默认显示的文件名也给隐藏掉了,那么如何显示文件名称呢?没关系,我们可以用jquery来获取文件的文件名。

我们可以写个change事件

复制代码
$(".a-upload").on("change","input[type='file']",function(){
    var filePath=$(this).val();
    if(filePath.indexOf("jpg")!=-1 || filePath.indexOf("png")!=-1){
        $(".fileerrorTip").html("").hide();
        var arr=filePath.split('\\');
        var fileName=arr[arr.length-1];
        $(".showFileName").html(fileName);
    }else{
        $(".showFileName").html("");
        $(".fileerrorTip").html("您未上传文件,或者您上传文件类型有误!").show();
        return false 
    }
})
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!