CSS,Cascading Style Sheet层叠样式表
是一组样式设置规则,用于控制页面的外观样式
案例:
Document
CSS从入门到精通
主讲:刘老师
也成为CSS引用方式,有三种方式:内部样式、行内样式、外部样式
也称为内嵌样式,在页面头部通过style标签定义
对当前页面当中所有符合样式选择器的标签都起作用
也称为嵌入样式,使用HTML标签的style属性定义
只对设置style属性的标签起作用
使用单独的CSS文件定义,然后在页面中使用或者
<@import指令>
引入
使用链接外部样式文件(推荐使用)
type属性可以省略
<@import指令>
导入外部样式文件
案例:
Document
welcome to CSS!
欢迎来到CSS课堂!
WEB前端工程师
java发工程师
哈哈
嘿嘿
也称为元素选择器,使用HTML标签作为选择器名称
以标签名作为样式应用的依据
使用自定义名称,以<.点号>
作为前缀,然后通过HTML标签的class属性调用类选择器
以标签的class属性作为样式应用的依据
注意事项:
<.点号>
<空格>
分隔使用自定义名称,以<#>
号作为前缀,然后通过HTML标签的id属性进行名称匹配
以标签的id属性作为样式应用的依据,一对一的关系
案例:
Document
welcome to CSS!
hello world!
WEB前端开发
java开发
welcome to CSS!
hello world!
WEB前端开发
java开发
主讲:刘老师
主讲:刘老师
CSS从入门到精通
嘿嘿
标签选择器和类选择器、标签选择器和ID选择器,一起使用
必须同时满足两个条件才能应用样式
也称为集体声明
将多个具有相同样式的选择器放在一起声明,使用逗号隔开
在某个选择器内部设置选择器,通过空格隔开
只有满足层次关系最里面的选择器所对应的标签才会应用样式
注意:使用<空格>
时不区分父子还是后代,使用CSS3中新增的">"必须是父子关系才行
案例:
Document
welcome
CSS
hello
world
html
WEB开发
hello
html
web开发
java开发
div内部的p标签
div内部的h3标签
div内部的h2标签内部的p标签
div内部的p标签
div内部的h3
ppppp
四种状态:
注意:默认超链接为:蓝色,下划线
案例:
Document
IT教育,高教培训
css从入门到精通
:first-letter为第一个字符的样式
:first-line为第一行添加样式
:before 在元素内容的最前面添加的内容,需要配合content属性使用
:after 在元素内容的最后面添加的内容,需要配合content属性使用
案例:
Document
hello world
hello world
welcome to css
ID选择器>类选择器>标签选择器
原因:首先加载标签选择器,在加载类选择器。然后加载ID选择器,最后加载行内样式
后加载会覆盖先加载的同名样式
就近原则
原因:按照书写顺序以此加载,在同优先级的前提下,后加载的会覆盖先加载的同名样式,所以离得越近越优先
可以使用!important使某个样式有最高的优先级
案例:
Document
css从入门到精通
主讲:刘老师
定义字体相关的样式
属性 | 含义 | 说明 |
---|---|---|
font-size | 大小、尺寸 | 可以使用多种单位 |
font-weight | 粗细 | |
font- family | 字体名称 | |
font-style | 字体样式 | |
font | 简写 |
取值:
HTML根元素默认字体大小为16px,也称为基础字体大小
取值:
要求系统中要安装指定的字体
一般建议写3种字体,首选、其次、备用,以逗号隔开
取值:
简写属性:font:font-style|font-weight|font-size|font-family
必须按此顺序书写
案例:
Document
CSS从入门到精通
主讲:高教培训
主讲:刘老师
我的DIV
CSS从入门到精通
主讲:高教培训
主讲:高教培训
- 嘿嘿
属性 | 含义 | 说明 |
---|---|---|
color | 颜色 | |
line-height | 行高 | 行之间的高度 |
text-align | 水平对齐方式 | 取值:left、center、right |
vertical-align | 垂直对齐方式 | 取值:top、middle、bottom 可用于图片和文字的对齐方式 |
text-indent | 首行缩进 | |
text-decoration | 文本修饰 | 取值:underline、overline、line-through |
text-transform | 字母大小写 | 取值:lowercase、uppercase、capitalize首字母大写 |
letter-spacing | 字符间距 | |
word-spacing | 单词间距 | 只对英文有效 |
white-space | 空白的处理方式 | 文本超出后是否换行显示,取值:nowrap |
案例:
Document
welcome to CSS!
welcome to CSS!
welcome to CSS!
welcome to CSS!
welcome to CSS!
HTML和CSS很简单
welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css
hello world
welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css welcome to css
取值:四种写法
颜色名称:使用英文单词
16进制的RGB取值:#RRGGBB
#fffff--->#FFF 白色
#000000--->#000 黑色
#FF0000--->#F00 红色
#00FF00--->#0F0 绿色
#0000FF--->#00F 蓝色
#cccccc--->#ccc 灰色
#ff7300--->无法简写
注意:不区分大小写
rgb函数:rgb(red,green、blue)
每个颜色的取值范围:[0,255]
rgb(255,0,0)--->红
rgb(0,255,0)--->绿
rgb(0,0,255)--->蓝
rgb函数:rgba(red,green,blue,alpha)
可以设置透明度,alpha的取值范围:[0,1] 0–表示完全透明 1–表示完全不透明
rgb(255,0,0,1)---纯红色
grb(255,0,0,0.5)---红色的半透明
属性 | 含义 | 说明 |
---|---|---|
backgroung-color | 背景颜色 | |
background-image | 背景图片 | |
background-repeat | 背景图片的重复方式 | |
background-position | 背景图片的显示位置 | |
background-attachment | 背景图片是否跟随滚动 | |
background | 简写 |
取值:transparent透明
取值:repeat(默认)、repeat-x、repeat-y、no-repeat
默认背景图片显示在左上角
取值:
CSS雪碧图:即CSS Sprites,也称为CSS精灵,是一种CSS图像合并技术
含义:将网页中许多非常小的图片整合到一张图片中,当访问该网页时只需要下载一次,可以减少访问服务器次数,提高性能
原理:使用background-position进行背景定位,使用坐标
取值:scroll默认、fixed固定不变
简写属性:background:background-color、background-image、background-repeat、background-position、background-attachment
以空格隔开,书写顺序没有要求
案例:
Document
welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css
购物车
style.css
div{
color:red;
/* background-color:#cccccc; */
/* background-image:url(../images/bg.gif); */
/* background-image:url(../../images/heihei.gif);
background-repeat:no-repeat;
background-position:right top;
height:1000px;
background-attachment:fixed; */
background:red url(../../images/qq.jpg) repeat-x 30px 100px;
}
.cart{
width:30px;
height:30px;
background-color:#ccc;
background-image:url(../../images/icon.gif);
background-color:transparent;
background-position:-160px -30px;
}
属性 | 含义 | 说明 |
---|---|---|
list-style-type | 设置咧白哦前的标记 | |
list-style-image | 将图像作为列表前的标记 | |
list-style-position | 设计标记的位置 | 取值:outside默认、inside |
list-style | 简写 |
取值:none、disc、circle、square、decimal
此时不在区分有序列表还是无序列表,只要设置列表前面的标记就可以了
简写属性:list-style:list-style-type、list-style-image、list-style-position
书写顺序没有要求
案例:
Document
- hello
- hello
- hello
- hello
border-collapse表格中相邻边框是否合并(折叠)为单一边框
取值:separate默认、collapse
案例:
Document
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
可以实现当保存页面文件时实时刷新浏览器
步骤:
在chrome浏览器中安装LiveReload扩展程序
将livereload.rar文件解压—》在chrome浏览器的右上角找“…”—》更多工具—》扩展程序—》提示:需要打开开发者模式—》加载已解压的扩展程序—》注意后续的操作 勾选“允许访问所有网站留存数据”
在sublime中的livereload插件
将LiveReload-sublime.rar
解压放到sublime的插件目录中(packages)中
配置sublime中的livereload插入
preference–》packages settings–》livereload–》settings-default
"enabled_plugins": [
"SimpleReloadPlugin"
"SimpleRefresh"
]
在浏览器中启用livereload
先打开要访问的页面,然后点击浏览器地址栏右边的黑色圆圈,当中心的小圆圈变成实心时表示已启用
在sublime中启用livereload
按住crtl+shift+p–》搜索livereload 选择enable–》搜索simple reload 选择enable
盒子模型是网页布局的基础,将页面中的所有元素均看作是一个盒子,盒子都包含以下几个属性:
盒子边框
分为四个方向(顺时针):
上top、右right、下bottom、左left
border-top、border-right、border-bottom、border-left
每个边框都包含三种样式:
颜色color、粗细width、样式style
border-top-color、border-top-width、border-top-style
border-right-color、border-right-width、border-right-style
border-bottom-color、border-bottom-width、border-bottom-style
border-left-color、border-left-width、border-left-style
样式的取值:
solid实线、dashed虚线、dotted点线、double双线、inset内嵌的3D线、outset外嵌的3D线
简写,三种方式:
按方向简写
border-top、border-right、border-bottom、border-left
书写顺序:
border-方向:width、style、color
按样式简写
border-color、border-width、border-style
书写顺序:
border-样式:top right bottom left
必须按顺时针方向书写,同时可以缩写
border-width:2px—>四个边框宽度均为2px
border-width:2px 4px—>上下一样,左右一样
border-width:1px 2px 4px
规则:如果省略,则认为上下一样、左右一样
终极简写
如果四个边样式完全相同,border:width style color
表示盒子的内距,即内容与边框之间的距离
同样也分为四个方向,也可以简写(按顺时针方向,默认上下一样,左右一样)
注意:如果上下冲突,则以上为准,如果左右冲突,则以左为准
表示盒子的外边距,即盒子与盒子之间的距离
同样也分为四个方向,也可以简写(按顺时针方向,默认上下一样,左右一样)
居中对齐:
/* 元素的水平居中 */
/* 1.块级元素的水平居中 */
margin:0 auto;
/* 提示:块级元素必须指定宽度 */
/* 2.文本的水平居中 */
text-align:center;
/* 3.垂直居中,将height和line-height设置为相同 */
height:100px;
line-height:100px;
案例:
Document
welcome to html
welcome to html
welcome to html
页面中的元素实际所占的空间
不同标签的盒子属性默认值可能不同,需要自己设置
body,ul,dl,ol,li,p,h1,h2,h3,h4,h5,h6,form{
margin:0;
padding:0;
}
案例:
Document
welcome to css
hello world
- hello1
- hello2
- hello3
也成为外边距的折叠,指定是两个块级元素垂直外边距相遇时,他们将合并为一个外边距,合并后的外边距值为其中较大的那个外边距值
两种情况:
外边距合并的好处,让排版在视觉上显得更美观
案例:
Document
div1
div2
p1
p2
p3
p4
p5
p6
p7
p8
通过position属性实现对元素的定位,有四种定位方式
常用取值:
取值 | 含义 | 说明 |
---|---|---|
static | 默认值 | 按照常规文档流进行显示 |
relative | 相对定位 | 相对于标签原来的位置进行的定位 |
absolute | 绝对定位 | 相对于第一个非static定位的父标签的定位 |
fixed | 固定定位 | 相对于浏览器窗口进行定位 |
设置定位方式后,还要设置定位属性(偏移量):top、bottom、left、right
先设置元素的position属性为relative,然后再设置偏移量
先设置父标签为非static定位,然后设置元素的position属性为absolute,最后再设置偏移量
注意:
一般来说都会将父标签设置为非static定位
如果父标签不是非static定位,则会相对于浏览器窗口进行定位
设置元素为绝对定位后,元素会浮到页面上方
先设置元素的position属性为fixed,然后再设置偏移量
设置元素为固定定位后,元素会浮动在面面上方
设置元素定位方式后,元素会浮在页面上方,此时可以通过z-index属性设置优先级,控制元素的堆叠顺序
取值为数字,值越大优先级越高,默认为auto(大多数浏览器默认为0)
注意:只能给非static定位的元素设置z-index属性
案例:
Document
div1
div2
div3
div4
通过float属性来实现元素的浮动,可以让块级元素脱离常规的文档流,向左或者向右移动,在同一行显示,如果一行显示不下,则会换行显示
常用取值:
设置float属性后,元素会浮在页面上层,此时父容器无法计算自己的宽度或者自己的尺寸,如果我们还想显示父容器通常会在末尾添加一个清除了float属性的空的div来解决
通过clear属性来实现清除,设置元素的哪一侧不允许有浮动元素
常用取值:
结论:
案例:
Document
div1
div2
div3
div4
welcome to css
通过display属性设置元素是否显示,以及是否独占一行
常用取值:
取值 | 含义 | 说明 |
---|---|---|
none | 不显示 | |
inline | 显示为内联元素,行级元素的默认值 | 将块级元素变为行级元素,不在独占一行 |
block | 显示为块级元素,块级元素的默认值 | 将行级元素变为块级元素,独占一行 |
inline-block | 显示为内联元素,但是可以设置宽和高 | 在inline基础上允许设置宽度和高度 |
注意:
行级元素是无法设置宽度和高度的,可以为行级元素设置display:inline-block
然后就可以设置宽和高了
也可以通过visibilty属性设置元素的显示和隐藏
常用属性:
取值 | 含义 | 说明 |
---|---|---|
visible | 显示 | |
hidden | 隐藏 |
案例:
Document
div1
span1
嘿嘿
p1
p2
登 录
轮廓outline:用于在元素的周围绘制一个轮廓,位于border的外围,可以突出显示元素
常用属性:
在浏览器中,当鼠标单击或使用TAB键让一个表单或者链接获得焦点时,该元素会有一个轮廓outline
优点:可以提高使用表单的用户体验
缺点:有时会影响美观
案例:
Document
welcome to css
用户名:
css
邮箱:
div
当元素内容溢出时该如何处理
常用取值:
用于设置光标的形状
常用属性:
案例:
Document
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
welcome to css welcome to css welcome to css welcome to css
光标形状
常见页面布局:
不适用于复杂布局,仅用于简单有规则的结构
定位相对准确,与浏览器基本无关,适用于简单分隔
table常用样式的属性
th/td常用样式属性:
案例:
Document
th1
th2
th3
th4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
定位绝对精确,使用灵活,适合于复杂的布局方式
两种形式:
案例:
Document
th1
th2
th3
th4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
td1
td2
td3
td4
style.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
width:980px;
border:1px solid #ccc;
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .main{
width:100%;
height:600px;
background:blue;
margin:10px 0px;
}
#container .footer{
width:100%;
height:120px;
background:green;
}
案例:
Document
header
main
style2.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
width:980px;
border:1px solid red;
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
width:100%;
height:600px;
background:blue;
margin:10px 0;
}
#container .wrapper aside{
background:pink;
width:200px;
height:400px;
float:left;
}
#container .wrapper .main{
background:cyan;
width:770px;
height:600px;
float:left;
/* margin:0 10px; */
margin-right:10px;
}
#container .footer{
width:100%;
height:120px;
background:green;
margin-top:10px;
}
页面的结构,两边的边栏宽度固定,中间的主体在一定范围内可自适应,并且主体优先被加载
一般防止页面缩放影响浏览,都会为页面设置一个最小宽度
案例:
Document
main
style3.css
@charset "utf-8";
/*
父容器
1.溢出时隐藏
2.设置容器的内边距padding,用来空出位置放置左右两个侧边栏
*/
#wrapper{
overflow:hidden; /* 溢出时隐藏 */
/* 实现左侧边栏和右侧边栏 */
padding:0 200px; /* 左右分别空出200px用于放置左、右侧边栏 */
min-width:300px;
border:1px solid #ccc;
}
/*
主体:
1.宽度自适应
2.设置浮动布局
*/
#wrapper .main{
width:100%;
height:300px;
background:green;
float:left;
}
#wrapper aside{
width:190px;
height:300px;
background:blue;
float:left;
position:relative;
}
#wrapper .left{
margin-left:-100%;
left:-200px;
}
#wrapper .right{
margin-left:-190px;
left:200px;
}
案例:
Document
header
main
style4.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
overflow:hidden;
padding:0 200px;
min-width:300px;
margin:10px 0;
}
#container .main{
width:100%;
height:400px;
background:blue;
float:left;
}
#container aside{
float:left;
width:190px;
height:300px;
background:cyan;
position:relative;
}
#container .left{
margin-left:-100%;
left:-200px;
}
#container .right{
margin-left:-190px;
left:200px;
}
#container .footer{
width:100%;
height:150px;
background:green;
}
源自淘宝的UED(用户体验设计)团队
双飞翼布局和圣杯布局要实现的效果是相同的,只是思路不同
圣杯布局和双飞翼布局的区别
实际开发中建议使用css3中新增的fiex弹性盒子布局,更简洁
案例:
style
Document
header
main
style5.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
overflow:hidden;
min-width:300px;
margin:10px 0;
}
#container .main{
width:100%;
float:left;
}
#container .main-wrapper{
background:pink;
height:400px;
margin:0 200px;
}
#container aside{
float:left;
width:190px;
height:300px;
background:cyan;
}
#container .left{
margin-left:-100%;
}
#container .right{
margin-left:-190px;
left:200px;
}
#container .footer{
width:100%;
height:150px;
background:green;
}
style4.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
overflow:hidden;
padding:0 200px;
min-width:300px;
margin:10px 0;
}
#container .main{
width:100%;
height:400px;
background:blue;
float:left;
}
#container aside{
float:left;
width:190px;
height:300px;
background:cyan;
position:relative;
}
#container .left{
margin-left:-100%;
left:-200px;
}
#container .right{
margin-left:-190px;
left:200px;
}
#container .footer{
width:100%;
height:150px;
background:green;
}
源自淘宝的UED(用户体验设计)团队
双飞翼布局和圣杯布局要实现的效果是相同的,只是思路不同
圣杯布局和双飞翼布局的区别
实际开发中建议使用css3中新增的fiex弹性盒子布局,更简洁
案例:
style
Document
header
main
style5.css
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
overflow:hidden;
min-width:300px;
margin:10px 0;
}
#container .main{
width:100%;
float:left;
}
#container .main-wrapper{
background:pink;
height:400px;
margin:0 200px;
}
#container aside{
float:left;
width:190px;
height:300px;
background:cyan;
}
#container .left{
margin-left:-100%;
}
#container .right{
margin-left:-190px;
left:200px;
}
#container .footer{
width:100%;
height:150px;
background:green;
}