<head>
<style>
选择器{
属性名:属性值;
属性名:属性值;
}
style>
head>
也称为CSS引用方式,有三种方式:内部样式、行内样式、外部样式
使用单独的CSS文件定义,然后在页面中使用 link标签 或 @import指令 引入
<link rel="stylesheet" type="text/css" href="css样式文件路径">
type属性可以省略
<style>
@import "CSS样式文件的路径";
@import url(CSS样式文件的路径);
style>
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* 1.内部样式(并没有实现样式与内部的分离) */
p{
color:red;
}
style>
<link rel="stylesheet" type="text/css" href="style/hello.css">
<style>
/* @import "style/hello.css"; */
/* @import url(style/hello.css); */
style>
head>
<body>
<p>weilcom to CSS!p>
<p>欢迎来到CSS课堂!p>
<h2 style="color:blue">WEB前端工程师h2>
<h2>JAVA开发工程师h2>
<div>嘿嘿div>
<div>哈哈div>
body>
html>
注意事项:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* 1.标签选择器 */
p{
color:red;
font-size:20px;
}
h2{
color:yellow;
}
/* 2.类选择器 */
.hello{
background:#cccccc;
}
.world{
font-weight:bold;
}
#heihei{
color:blue;
}
style>
head>
<body>
<p>welcome to CSS!p>
<p>WEB前端开发p>
<h3>JAVA开发h3>
<hr>
<p class="hello">welcome to CSS!p>
<p>hello world!p>
<h2>WEB前端开发h2>
<h3>JAVA开发h3>
<div class="hello">主讲div>
<div class="hello world">主讲div>
<span class="world">CSS从入门到精通span>
<hr>
<h1 id="heihei">嘿嘿h1>
body>
html>
案例:
注意:使用 空格 时不区分父子还是后代,使用CSS3中新增的 > 时必须是父子关系才行
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* 1.标签选择器与类选择器结合起来-----复合选择器 */
h1.aaa{
color:red;
}
/* 1.让标签选择器与ID选择器结合起来----复合选择器 */
p#bbb{
color:blue;
}
/* 2.组合选择器 */
/* 分开书写 */
/* 合起来写 */
h1,p,div,span,.ccc{
font-size:30px;
}
div{
background:#CCCCCC;
}
.ccc{
font-weight:bold;
}
/* 3.嵌套选择器 */
div>p{
color:green;
text-decoration:underline;
}
/* 对div内部的类选择器进行修饰 */
div h3.ddd{
color:pink;
}
style>
head>
<body>
<h1 class="aaa">welcomeh1>
<h4 class="aaa">cssh4>
<h1>helloh1>
<hr>
<p id="bbb">worldp>
<p>htmlp>
<h2 id="bbb">WEB开发h2>
<hr>
<h1>helloh1>
<p>htmlp>
<div>web开发div>
<span class="ccc">Java开发span>
<hr>
<div>
<p>div内部的p标签p>
<h3>div内部的h3标签h3>
div>
<hr>
<div>
<h2>
<p>div内部的h2标签内部的p标签p>
h2>
div>
<hr>
<div>
<p>div内部的p标签p>
<h3 class="ddd">div内部的h3标签h3>
<p class="ddd">ppppp>
div>
body>
html>
注意:默认超链接为:蓝色,下划线
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* a:link{
font-size:12px;
color:black;
text-decoration:none;
}
a:visited{
font-size:15px;
color:red;
}
a:hover{
font-size:20px;
color:blue;
}
a:active{
font-size:40px;
color:green;
} */
a:link,a:visited{
font-size:13px;
color:#666666;
text-decoration:none;
}
a:hover,a:active{
color:#ff7300;
text-decoration:underline;
}
/* 普通标签也库使用伪类选择器 */
p:hover{
color:red;
}
p:active{
color:blue;
}
style>
head>
<body>
<a href="2.CSS的应用样式.html">IT教育,在线培训a>
<p>CSS从入门到精通p>
body>
html>
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
p:first-letter{
color:red;
font-size:30px;
}
p:first-line{
background:yellow;
}
p:before{
content:"嘿嘿";
}
p:before{
content:"哈哈";
}
p:
style>
head>
<body>
<p>hello worldp>
<hr>
<p>
hello world! <br>
welcome to css!
p>
body>
html>
可以使用!important使某个样式有最高的优先级
设置字体相关的样式
属性 | 含义 | 说明 |
---|---|---|
font-size | 大小、尺寸 | 可以使用多种单位 |
font-weight | 粗细 | |
font-family | 字体 | |
font-style | 样式 | |
font | 简写 |
取值:
inherited继承,默认从父标签继承字体大小(默认值),所有CSS属性的默认值都是inherited
px像素 pixel
%百分比,相对父标签字体大小的百分比
em倍数,相对于父标签字体大小的倍数
HTML根元素默认字体的大小为16px,也称为基础字体大小
取值:
1.3 font-family
1.4 font-style
取值:
1.5 font
简写属性:font:font-style|font-weight|font-size|font-family
必须按此顺序书写
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
div{
font-size:30px;
}
p{
/* font-size:20px; */
/* font-size:80%; */
font-size:2em;
}
span.hello{
/* font-size:80%; */
font-size:2em;
}
body{
font-size:62.5%;
}
#ddd{
font-size:3em;
}
ul li{
/* font-size:30px;
font-weight:500;
font-family:华文行楷,黑体,宋体;
font-style:normal; */
font:italic normal 20px 黑体,楷体,宋体;
}
style>
head>
<body>
<p>
CSS从入门到精通
<span>成员:span>
p>
<span id="ddd">成员:zhangspan>
<hr>
<div>
我的div
<p>
CSS从入门到精通
<span>成员:span>
p>
div>
<hr>
<span class="hello">成员:span>
<hr>
<ul>
<li>嘿嘿li>
ul>
body>
html>
案例:
属性 | 含义 | 说明 |
---|---|---|
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 |
取值:四种写法:
颜色名称:使用英文单词 1
6进制的RGB值:#RRGGBB
特定情况下可以缩写
#FFFFFF--->#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)----->蓝
rgba函数:rbga(red,green,blue,alpha)
可以设置透明度,alpha取值范围:[0,1] 0表示完全透明 1表示完全不透明
rgba(255,0,0,1)----->纯红
rgba(255,0,0,0.5)---->红色半透明
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
p{
color:red;
/* background:#0f0; */
/* background-color:rgb(0, 0, 255); */
/* background-color:rgba(0, 255, 0, 0.5); */
background-color:#54d2c9;
line-height:50px;
text-align:center;
}
img{
vertical-align:middle;
}
div{
text-indent:25px;
}
span{
text-decoration:line-through;
text-transform:capitalize;
letter-spacing:3px;
word-spacing:10px;
}
h3{
width:300px;
height:200px;
background-color:#cccccc;
white-space:nowrap;
overflow:hidden;
}
style>
head>
<body>
<p>welcome worldp>
<p>welcome worldp>
<p>welcome worldp>
<p>welcome worldp>
<hr>
<img src="../images/qq.jpg" alt="">
html和CSS很简单
<hr>
<div>
welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world
div>
<div>
welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world
div>
<hr>
<span>hello cssspan>
<hr>
<h3>
welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world welcome world
h3>
body>
html>
案例:
属性 | 含义 | 说明 |
---|---|---|
background-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
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* div{
color:red;
background-color:#cccccc;
background-color:transparent;
background-image:url(../images/heihei.gif);
} */
style>
<link rel="stylesheet" href="css/style.css">
head>
<body>
<div>
<p>welcome to css welcome to css welcome to cssp>
<p>welcome to css welcome to css welcome to cssp>
<p>welcome to css welcome to css welcome to cssp>
<p>welcome to css welcome to css welcome to cssp>
<p>welcome to css welcome to css welcome to cssp>
<p>welcome to css welcome to css welcome to cssp>
div>
<p class="cart">p>
购物车
body>
html>
div中的代码:
div{
color:red;
/* background-color:#cccccc; */
/* background-color:transparent; */
/* background-repeat:repeat-y; */
/* 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
此时不再区分有序列表还是无序列表,只要设置列表前的标记就可以了
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/* li{
list-style-type:decimal;
} */
.first{
list-style-type:decimal;
}
.second{
/* list-style-type:square; */
list-style-image:url(../images/male.gif);
}
.third{
list-style-type:circle;
list-style-position:inside;
}
.fourth{
/* list-style:square url(../images/female.gif) outside; */
list-style:none;
}
.nav{
list-style:none;
/* float:left; */
}
.nav li{
list-style:none;
float:left;
width:50px;
}
style>
head>
<body>
<ul>
<li class="first">helloli>
<li class="second">helloli>
<li class="third">helloli>
<li class="fourth">helloli>
ul>
<hr>
<nav>
<ul class="nav">
<li>新闻li>
<li>地图li>
<li>视频li>
<li>贴吧li>
ul>
nav>
body>
html>
案例:
border-collapse:表格中相邻的边框是否合并(折叠)为单一边框
取值:separated(默认) collapse
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
table{
width:500px;
border:1px solid red;
border-collapse:collapse;
}
td{
border:1px solid red;
}
style>
head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
table>
body>
html>
案例:
可以实现当保存页面文件时实时刷新浏览器、
步骤:
在Chrome浏览器中安装 LiveReload 扩展程序
将livereload.rar文件解压–>在chrome浏览器的右上角找“…”–>更多工具–>扩展程序–>提示:
需要打 开开发者模式–>加载已解压的扩展程序–>注意后项的操作 勾选"允许访问所有网站留存数据"
在Sublime中安装LiveRelad插件
将 LiveRelad-sublime.rar 解压放到sublime的插件目录中(packages)中
preference–>packages settings–>LiveReload–>settings - default
"enabled_plugins": [
"SimpleReloadPlugin"
"SimpleRefresh"
]
在浏览器中启用LiveReload
先打开要访问的页面,然后点击浏览器地址栏右边的黑色圆圈,当中心的小圆圈变成实心时表示已启用
在sublime中启用LiveReload
按ctrl+Shift+P–>搜索livereload,选择enable–>搜索simple reload:选择enable
盒子模型是网页布局的基础,将页面中所有元素都看作是一个盒子,盒子都包含以下几个属性:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-76047xmf-1587541205043)(F:\sublime\19020103 张杰\CSS\截屏\盒子.png)]
表示盒子的边框
分为四个方向:
每个边框包含三种样式:
样式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:1px 2px;
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;
案例:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
p{
width:250px;
background:#ccc;
}
.first{
/* border-top-color:red;
border-top-width:1px;
border-top-style:solid;
border-right-color:blue;
border-right-width:2px;
border-right-style:dashed;
border-bottom-color:green;
border-bottom-width:4px;
border-bottom-style:dotted;
border-left-color:gray;
border-left-width:6px;
border-left-style:double;
*/
/* border-top:1px solid red;
border-bottom:2px dashed blue; */
/* border-color:red yellow blue green;
border-width:1px 2px 4px 6px;
border-style:solid dashed dotted double; */
/* border-color:red green pink;
border-width:1px 2px 4px 6px;
border-style:solid dashed dotted double; */
border:1px dotted red;
}
.second{
/* padding-top:15px;
padding-left:10px;
padding-bottom:20px;
padding-right:30px; */
/* padding:1px 2px 4px 6px; */
padding:5px;
}
style>
head>
<body>
<p class="first">welcome to htmlp>
<p class="second">welcome to cssp>
<p class="third">welcome to javap>
body>
html>
案例:
页面中的元素实际所占的空间
不同标签的盒子属性默认值可能不同,需要自己设置
body,ul,o;,dl,li,p,h1,h2,h3,h4,h5,h6,form{
margin:0;
padding:0;
}
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
/*body默认margin不为0*/
body{
margin:0;
padding:0;
}
/*p默认margin不为0*/
p{
margin:0;
}
/* ul默认的margin和padding都不为0 */
ul{
list-style:none;
padding:0;
}
body,ul,o;,dl,li,p,h1,h2,h3,h4,h5,h6,form{
margin:0;
padding:0;
}
style>
head>
<body>
welcome to CSS
<p>hello worldp>
<ul>
<li>hello1li>
<li>hello2li>
<li>hello3li>
ul>
body>
html>
案例:
3.3 外边距的合并
也称为外边距的折叠,指的是两个块级元素垂直外边距相遇时,它们将合并为一个外边距,合并后的外边 距值为其中较大的那个外边距值
两种情况:
外边距的合并的好处,让排版在视觉上显得更美观
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
div{
width:50px;
height:50px;
background:#ccc;
}
.div1{
margin-bottom:20px;
}
.div2{
margin-top:30px;
}
.div3{
width:100px;
height:100px;
background:blue;
margin-top:20px;
/* border:1px solid red; */
padding:1px;
}
.div4{
margin-top:10px;
}
p{
margin:20px 0;
}
style>
head>
<body>
<div class="div1">div1div>
<div class="div2">div2div>
<hr>
<div class="div3">
<div class="div4">div>
div>
<hr>
<p>p1p>
<p>p2p>
<p>p3p>
<p>p4p>
<p>p5p>
<p>p6p>
<p>p7p>
<p>p8p>
body>
html>
案例:
通过position属性实现对元素的定位,有四种定位方式
常用取值:
取值 | 含义 | 说明 |
---|---|---|
static | 默认值 | 按照常规文档流进行显示 |
relative | 相对定位 | 相对于标签原来的位置进行的定位 |
absolute | 绝对定位 | 相对于第一个非static定位的父标签的定位 |
fixed | 固定定位 | 相对于浏览器窗品进行定位 |
设置定位方式后,还要设置定位属性(偏移量):top、bottom、left、right
先设置元素的position属性为relative,然后再设置偏移量
先设置父标签为非static定位,然后设置元素的position属性为absolute,最后再设置偏移量
注意:
先设置元素的position属性为fixed,然后再设置偏移量
设置元素为固定定位后,元素会浮动在面面上方
设置元素定位方式后,元素会浮在页面上方,此时可以通过z-index属性设置优先级,控制元素的堆叠顺序
取值为数字,值越大优先级越高,默认为auto(大多数浏览器默认为0)
注意:只能给非static定位的元素设置z-index属性
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
div{
width:50px;
height:50px;
background:#ccc;
}
.div1{
margin-bottom:20px;
}
.div2{
margin-top:30px;
}
.div3{
width:100px;
height:100px;
background:blue;
margin-top:20px;
/* border:1px solid red; */
padding:1px;
}
.div4{
margin-top:10px;
}
p{
margin:20px 0;
}
style>
head>
<body>
<div class="div1">div1div>
<div class="div2">div2div>
<hr>
<div class="div3">
<div class="div4">div>
div>
<hr>
<p>p1p>
<p>p2p>
<p>p3p>
<p>p4p>
<p>p5p>
<p>p6p>
<p>p7p>
<p>p8p>
body>
html>
案例:
通过float属性实现元素的浮动,可以让块级元素脱离常规文档流,向左或向右移动,在
同一行显示,如果 一行显示不下则会换行显示
常用取值:
left左浮动
right右浮动
none不浮动,默认值
设置float属性后,元素会浮在页面的上层,此时父容器无法计算自己的尺寸,通常
会在容器末尾添加一个 清除了float的空的div来解决
通过clear属性实现清除,设置元素的哪一侧不允许出现浮动元素,目的是为了和其他
浮动元素换行隔开, 只对块级元素有效
常用取值:
结论:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
#container{
/* width:800px; */
border:1px solid #000;
}
.div1,.div2,.div3,.div4{
width:300px;
height:50px;
}
.div1{
background:red;
float:left;
}
.div2{
background:blue;
float:left;
clear:left;
}
.div3{
background:green;
float:left;
}
.div4{
background:cyan;
float:left;
}
.clr{
clear:both;
}
style>
head>
<body>
<div id="container">
<div class="div1">div1div>
<div class="div2">div2div>
<div class="div3">div3div>
<div class="div4">div4div>
<div class="clr">div>
div>
welcome to css
body>
html>
案例:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
body{
margin:0;
padding:0;
}
#container{
width:720px;
margin:0 auto;
border:2px solid #0f0;
}
.div1,.div2,.div3,.div4{
width:200px;
height:180px;
float:left;
margin:5px;
border:5px outset #ff7300;
padding:10px;
}
#container img{
width:100%;
height:100%;
}
#container .clr{
clear:both;
}
style>
head>
<body>
<div id="container">
<div class="div1"><img src="../images/adv1.jpg" alt="">div>
<div class="div2"><img src="../images/adv2.jpg" alt="">div>
<div class="div3"><img src="../images/adv3.jpg" alt="">div>
<div class="div4"><img src="../images/adv4.jpg" alt="">div>
<div class="clr">div>
div>
<p>welcome to cssp>javascript
body>
html>
案例:
通过display属性设置元素是否显示,以及是否独占一行显示
常用取值:
取值 | 含义 | 说明 |
---|---|---|
none | 不显示 | |
inline | 显示为内联元素,行级元素的默认值 | 将块级元素变为行级元素,不再独占一行 |
block | 将块级元素变为行级元素,不再独占一行 | 将行级元素变为块级元素,独占一行 |
inline-block | 将块级元素变为行级元素,不再独占一行 | 在inline基础上允许设置宽和高 |
注意:行级元素默认无法设置宽和高,可以为行级元素设置display:inline-block,然后
就可以设置宽和高了
常用取值:
取值 | 含义 | 说明 |
---|---|---|
visible | 显示 | |
hidden | 隐藏 |
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
.div1{
width:100px;
height:100px;
background:blue;
/* display:none; */
display:inline;
/* 将块级元素转换为行级元素进行显示,失去了块级元素 有独占一行的特点 */
}
span{
width:100px;
height:100px;
background:yellow;
display:block;
/* 若设计为block元素,将行级元素转换为块级元素进行显示,独占一行进行显示 */
/* 如果 我想即有对行级元素设置宽和高,但是我还要让行级不独占一晃显示,怎么弄? */
/* 方法1:设置浮动 float:left; */
/* 方法2:display:inline-block; */
display:inline-block;
}
i{
display:inline-block;
width:100px;
height:100px;
background:red;
}
p{
width:50px;
height:50px;
background:red;
}
.p1{
/* display:none; */
visibility:hidden;
}
#login{
display:inline-block;
width:100px;
text-decoration:none;
background:rgba(255,0,0,0.7);
color:#fff;
padding:10px;
text-align:center;
border-radius:8px;
}
#login:hover{
background:rgba(255,0,0,0.5);
}
style>
head>
<body>
<div class="div1">div1div>
<span>span1span>
<i>呵呵i>
<hr>
<p class="p1">p1p>
<p class="p2">p2p>
<hr>
<a href="javascript:alert('哈哈')" id="login">登 陆a>
body>
html>
案例:
轮廓outline,用于在元素周围绘制一个轮廓,位于border的外围,可以突出显示元素
常用属性:
在浏览器,当鼠标单击或使用TAB键让一个表单元素或链接元素获得焦点时,该元素周围会出现一个轮廓 outline
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
span{
border:2px solid red;
/* outline-width:4px;
outline-color:blue;
outline-style:dashed; */
outline:4px green dashed;
}
.usrname{
border:1px solid red;
outline:none; /* 用户名文本框不设置轮廓 */
padding-left:3px;
width:80px;
}
.email{
border:0;
outline:0;
border-bottom:1px solid #000;
}
.btnsubmit{
border:0;
padding:5px;
width:100px;
}
.mydiv{
width: 100px;
height: 50px;
background:#ccc;
border:2px solid red;
outline:2px solid red;
}
style>
head>
<body>
<span>welcome to cssspan>
<hr>
用户名:<input type="text" class="usrname">
<hr>
<a href="#">SCCa>
<hr>
邮箱:<input type="text" class="email">
<input type="submit" value="提交" class="btnsubmit">
<hr>
<div class="mydiv">divdiv>
body>
html>
案例:
当元素内容溢出时该如何处理
用来设置光标的形状
常用属性:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
p{
border:1px solid red;
min-width:500px;
}
div{
border:1px solid red;
width: 300px;
height: 100px;
overflow:auto;
}
span{
cursor:help;
}
style>
head>
<body>
<p>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 p>
<hr>
<div>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 div>
<hr>
<span>光标的形状span>helloworld
body>
html>
案例:
常见页面布局
不适合于复杂布局,仅适用于简单、有规则的结构
定位相对准确,与浏览器基本无关,适用于简单分隔
table常用样式属性:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<style>
.hello{
/* width: 600px; */
width:80%;
border:1px solid #ccc;
border-spacing:0;
border-collapse:collapse;
}
.hello th,.hello td{
border:1px solid #ccc;
padding:5px;
}
style>
head>
<body>
<table class="hello">
<thead>
<tr>
<th>th1th>
<th>th2th>
<th>th3th>
<th>th4th>
tr>
thead>
<tbody>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
<tr>
<td>td1td>
<td>td2td>
<td>td3td>
<td>td4td>
tr>
tbody>
table>
body>
html>
案例:
定位绝对准确,使用灵活,适合于复杂的布局方式
两种形式:
简单布局1
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<link rel="stylesheet" href="css/style1.css">
head>
<body>
<div id="container">
<header class="header">
header
header>
<article class="main">
main
article>
<footer class="footer">
footer
footer>
div>
body>
html>
style1.css的代码:
@charset "utf-8";
body{
padding:0;
margin: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-top:10px; */
margin:10px 0;
}
#container .footer{
width:100%;
height:120px;
background:green;
/* margin-top:10px; */
}
案例:
简单布局2
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<link rel="stylesheet" href="css/style2.css">
head>
<body>
<div id="container">
<header class="header">
header
header>
<article class="wrapper">
<section class="main">
main
section>
<aside>
right aside
aside>
article>
<footer class="footer">
footer
footer>
div>
body>
html>
style2.css的代码:
@charset "utf-8";
body{
padding:0;
margin:0;
}
#container{
width:980px;
border:1px solid #ccc;
margin:0 auto;
}
#container .header{
width:100%;
height:80px;
background:red;
}
#container .wrapper{
width:100%;
height:600px;
background:blue;
margin:10px 0;
}
#container .wrapper .main{
background:cyan;
width:760px;
height:600px;
float:left;
margin:0 10px;
/* margin-right:10px; */
}
#container .wrapper aside{
background:pink;
width:200px;
height:400px;
float:left;
}
#container .footer{
width:100%;
height:120px;
background:green;
}
案例:
页面结构,两边的边栏宽度固定,中间主体在一定范围内可自适应,并且主体优先被加载
一般防止页面缩放影响浏览,都会为页面设置一个最小宽度
圣杯布局1
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<link rel="stylesheet" href="css/style3.css">
head>
<body>
<article id="wrapper">
<section class="main">
main
section>
<aside class="left">
left aside
aside>
<aside class="right">
right aside
aside>
article>
body>
html>
style3.css的代码:
@charset "utf-8";
/*
父容器
1.溢出隐藏
2.设置容器的内边距padding,用来空出位置放置在左右两个侧边栏
*/
#wrapper{
overflow:hidden; /*溢出时隐藏*/
/* 实现左侧边栏和右侧边栏 */
padding:0 200px; /* 左右分别空出200像素,用于放置左、右侧边栏 */
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;
right:-200px;
}
案例:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<link rel="stylesheet" href="css/style4.css">
head>
<body>
<div id="container">
<header class="header">
header
header>
<article class="wrapper">
<section class="main">
main
section>
<aside class="left">
left
aside>
<aside class="right">
right
aside>
article>
<footer class="footer">
footer
footer>
div>
body>
html>
style4.css的代码:
@charset "utf-8";
body{
margin:0;
padding:0;
}
#container{
/* 不设置宽度 默认就是100% */
margin:0 auto;
}
#container .header{
width: 100%;
height: 80px;
background:red;
}
#container .wrapper{
/* 不设置宽度 默认就是100% */
overflow:hidden;
padding:0 200px;
min-width:300px;
margin:10px 0;
}
#container .main{
width: 100%;
/* 在上面已经在main的左右预留了200像素的宽度放侧边栏 */
height: 400px;
background:pink;
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(用户体验设计)团队
双飞翼布局和圣杯布局要实现的效果是相同的,只是思路不同
圣杯布局和双飞翼布局的区别
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Documenttitle>
<link rel="stylesheet" href="css/style5.css">
head>
<body>
<div id="container">
<header class="header">
header
header>
<article class="wrapper">
<section class="main">
<div class="main-wrapper">
main
div>
section>
<aside class="left">
left aside
aside>
<aside class="right">
right aside
aside>
article>
<footer class="footer">
footer
footer>
div>
body>
html>
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%;
/* 此处的100%是main的100% */
}
#container .right{
margin-left:-190px;
}
#container .footer{
width: 100%;
height: 150px;
background:green;
}
案例:
链接:https://pan.baidu.com/s/1E_fIuX5V5BRorkccWeF77Q
提取码:tkub