1 css 的声明
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css的声明学习</title>
<!--
CSS声明学习:
1、在head标签中使用style标签声明:
作用:此声明一般声明当前网页的公共样式或者给某个标签的单独样式
2、在标签上使用style属性进行声明:
作用:此声明会将css样式直接作用于当前标签。
3、在head标签中使用link标签引入外部声明好的css文件
作用:此声明相当于调用,解决了不同网页间样式重复使用的问题
一次声明,随处使用
问题:
不同的声明给同一个标签操作了同一个样式,会使用谁的?
如果Css的声明全部在head标签中,则遵循就近原则,谁离标签近,谁就会被显示。
-->
<!--引入外部声明好的css文件-->
<link rel="stylesheet" type="text/css" href="css/my.css"/>
<!--声明css代码域-->
<style type="text/css">
hr{
width: 50%;
height: 20px;
color: red;
background-color: red;
}
</style>
</head>
<body>
<h3>css的声明学习</h3>
<hr style="background-color: blue;height:50px;"/>
<hr/>
</body>
</html>
2 css的选择器选择
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css的选择器学习</title>
<!--
css的选择器学习:
标签选择器:
标签名{样式名1:样式值1;……}
作用:会将当前网页内的所有该标签增加相同的样式
id选择器:
#标签的id属性值{样式名1:样式值1;……}
作用:给某个指定的标签添加指定的样式
类选择器:
.类选择器名{样式名1:样式值1;……}
作用:给不同的标签添加相同的样式
全部选择选择器
*{样式名1:样式值1;……}
作用:选择所有的HTML标签,并添加相同的样式
-----------------------------------------------------------------------------
组合选择器:
选择器1,选择器2,……{样式名1:样式值1;……}
作用:解决不同的选择器之间重复样式的问题
子标签选择器
选择器1 子标签选择器{样式名1:样式值1;……}
属性选择器:
标签名[属性名=属性值]{样式名1:样式值1;……}
作用:选择某标签指定具备某属性并且属性值为某属性值的标签
css的使用过程:
1、声明css代码域
2、使用选择器选择要添加样式的标签
根据需求来。
使用*选择器来给整个页面添加基础样式
使用类选择器给不同的标签添加基础样式
使用标签选择器给某类标签添加基础样式
使用id、属性选择器、style属性声明方式给某个标签添加个性化样式。
3、书写样式单
边框设置
border:solid 1px;
字体设置:
font-size:10px;设置字体大小
font-family:"黑体";(设置字体的格式)
font-weight:bold;设置字体加粗
字体颜色设置
color:颜色;
背景颜色设置
background-color:颜色;
背景图片设置
background-img:url(图片的相对路径);
background-repeate:no-repeate;设置图片不重复
bacground-size:cover;图片平铺整个页面
高和宽设置
浮动设置
float:left|right
行高设置
line-height:10;
-->
<!--声明css代码域-->
<style type="text/css">
table{
width: 300px;
height: 200px;
border: solid 1px;
background-color: red;
}
b{
width: 300px;
height: 200px;
border: solid 1px;
background-color: red;
}
#t1{
background-color: gray;
}
.common{
color: red;
}
.common,table{
background-color: blue;
}
ul li a{
color: black;
}
#p1 a{
color: yellow;
}
input[type=text]{
background-color: red;
}
</style>
</head>
<body>
<h3>css的选择器学习</h3>
<hr />
<hr />
<table id="t1" class="common">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</table>
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<b class="common">css学习</b>
<hr />
<!--子选择器-->
<ul id="u">
<li><a href="">哈哈</a></li>
<li><a href="">呵呵</a></li>
<li><a href="">嘿嘿</a></li>
</ul>
<ul>
<li><a href="">哈哈</a></li>
<li><a href="">呵呵</a></li>
<li><a href="">嘿嘿</a></li>
</ul>
<p id="p1">
<a href="">嘻嘻</a>
</p>
<p>
<a href="">嘻嘻</a>
</p>
<a href="">嘻嘻</a>
<hr />
<input type="text" name="" id="" value="" /><br />
<input type="password" name="" id="" value="" />
</body>
</html>
3 css 的样式使用
<html>
<head>
<title>css的样式使用</title>
<meta charset="UTF-8"/>
<!--声明css代码域-->
<style type="text/css">
body{
background-image: url(img/a1.jpg);
background-repeat: no-repeat;
background-size: cover;
}
table{
}
tr{
height: 40px;
}
td{
width: 100px;
border: solid 1px red;
border-radius: 10px;
background-color: orange;
text-align: center;
color: blueviolet;
font-weight: bold;
letter-spacing: 10px;
}
ul{
background-color: gray;
height: 50px;
}
li{
list-style-type:none;
float: left;
}
li a{
color: black;
text-decoration: none;
font-weight: bold;
font-size: 20px;
margin-left: 20px;
position: relative;
top:10px;
}
</style>
</head>
<body>
<h3>css的样式使用</h3>
<hr />
<table>
<tr>
<td>姓名</td>
<td>性别</td>
<td>爱好</td>
</tr>
<tr>
<td>张三</td>
<td>男</td>
<td>唱歌</td>
</tr>
<tr>
<td>李四</td>
<td>男</td>
<td>跳舞</td>
</tr>
</table>
<hr />
<ul>
<li><a href="">首页</a></li>
<li><a href="">百战</a></li>
<li><a href="">java风情</a></li>
<li><a href="">客服</a></li>
</ul>
</body>
</html>
4 照片墙案例
<html>
<head>
<title>照片墙案例</title>
<meta charset="UTF-8"/>
<!--声明css代码域-->
<style type="text/css">
body{
text-align: center;
background-color: gray;
}
img{
width: 12%;
padding: 10px;
background-color: white;
transform: rotate(-10deg);
margin: 20px;
}
img:hover{
transform: rotate(0deg) scale(1.5);
z-index: 1;
transition: 1.5s;
}
</style>
</head>
<body>
<br /><br /><br />
<img src="img/a1.jpg" alt="" />
<img src="img/a2.jpg" alt="" />
<img src="img/a3.jpg" alt="" />
<img src="img/a4.jpg" alt="" /><br />
<img src="img/a5.jpg" alt="" />
<img src="img/a6.jpg" alt="" />
<img src="img/a7.jpg" alt="" />
<img src="img/a8.jpg" alt="" />
</body>
</html>
5 css的盒子模型
<html>
<head>
<title>css的盒子模型学习</title>
<meta charset="UTF-8"/>
<!--
css的盒子模型学习:
div标签:
块级标签,主要是用来进行网页布局的,会将其中的子元素内容作为一个独立的整体存在。
特点:
默认宽度是页面的宽度,但是可以设置。
高度默认是没有的,但是可以设置。(可以顶开)
如果子元素设置了百分比的高或者宽,占据的是div的百分比,不是页面的。
盒子模型:
外边距:margin
作用:用来设置元素和元素之间的间隔。
居中设置:margin:0px auto;上下间隔是0px,水平居中。
可以根据需求单独的设置上下左右的外边距。
边框:border
作用:用来设置元素的边框大小
可以单独设置上下左右
内边距:padding
作用:设置内容和边框之间的距离
注意:内边距不会改变内容区域的大小
可以单独的设置上下左右的内边距
内容区域:
作用:改变内容区域的大小。
设置宽和高即可改变内容区域的大小。
-->
<style type="text/css">
img{
width: 49.53%;
height: 150px;
}
#showdiv{
border: solid 100px;
width: 40%;
height: 200px;
margin-bottom: 10px;
margin: 0px auto;
padding: 20px;
}
#div01{
border: dashed 3px orange;
width: 40%;
height: 200px;
margin-left: 100px;
}
</style>
</head>
<body>
<div id="showdiv">
<img src="img/a1.jpg"/>
<img src="img/a1.jpg"/>
</div>
<div id="div01">
</div>
</body>
</html>
6 盒子模型简单布局
<html>
<head>
<title>盒子模型简单的布局</title>
<meta charset="UTF-8"/>
<style type="text/css">
div{
width: 300px;
height: 300px;
}
#header,#footer{
width: 650px;
margin: auto;
margin-top: 20px;
}
#div01{
border: solid 1px orange;
float: left;
margin-right: 20px;
}
#div02{
border: solid 1px blueviolet;
float: left;
}
#div03{
border: solid 1px #4791FF;
float: left;
margin-right: 20px;
}
#div04{
border: solid 1px coral;
float: left;
}*/
</style>
</head>
<body>
<div id="header">
<div id="div01">
我是div01
</div>
<div id="div02">
我是div02
</div>
</div>
<div id="footer">
<div id="div03">
我是div03
</div>
<div id="div04">
我是div04
</div>
</div>
</body>
</html>
7 css的定位
<html>
<head>
<title>css的定位</title>
<meta charset="UTF-8"/>
<!--
css的定位学习:
position
相对定位:relative
作用:相对元素原有位置移动指定的距离(相对的自己的原有位置)
可以使用top,left,right,bottom来进行设置。
注意:
其他元素的位置是不改变的。
绝对定位:absolute
作用:可以使用元素参照界面或者相对父元素来进行移动
注意:
如果父级元素成为参照元素,必须使用相对定位属性
默认情况下是以界面为基准进行移动的。
固定定位:fixed
作用:将元素固定现在页面的指定位置,不会随着滚动条的移动而改变位置。
以上定位都可以使用top,left,right,bottom来进行移动。
z-index:此属性是用来声明元素的显示级别的。
-->
<!--声明css代码域-->
<style type="text/css">
#div01{
border: solid 2px orange;
height: 200px;
width: 800px;
margin-bottom: 10px;
margin-top: 50px;
position: relative;
}
#showdiv{
border: solid 3px;
height: 50px;
width: 50px;
position: absolute;
top:10px;
}
#div02{
border: dashed 2px coral;
height:200px;
width: 800px;
margin-bottom: 10px;
position: relative;
left:50px;
top:100px;
background-color: coral;
z-index: 3;
}
#div03{
border: solid 2px #FF7F50;
height: 200px;
width: 800px;
background-color: gray;
position: relative;
z-index: 4;
}
#div04{
border: solid 3px blue;
height: 50px;
width: 50px;
position: fixed;
top:270px;
right: 10px;
}
</style>
</head>
<body>
<div id="div01">
<div id="showdiv">
</div>
</div>
<div id="div02">我是div02</div>
<div id="div03"></div>
<div id="div04">
</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</body>
</html>
8 百度首页
<html>
<head>
<title>百度首页</title>
<meta charset="UTF-8"/>
<link href="title.ico" rel="shortcut icon"/>
<!--引入声明好的样式-->
<link rel="stylesheet" type="text/css" href="css/baidu.css"/>
</head>
<body>
<!--百度首页:网页布局-->
<!--网页标题部分-->
<div id="header">
<!--声明导航栏-->
<ul id="header_nav">
<li><a href="">新闻</a></li>
<li><a href="">hao123</a></li>
<li><a href="">地图</a></li>
<li><a href="">视频</a></li>
<li><a href="">贴吧</a></li>
<li><a href="">学术</a></li>
<li><a href="">登录</a></li>
<li><a href="">设置</a></li>
</ul>
</div>
<!--网页主体部分-->
<div id="main">
<div id="main_logo">
<img src="img/bd_logo.png" height="129px" width="270px"/><br />
</div>
<div id="main_search">
<input type="text" name="" id="" value="" />
<input type="submit" name="" id="" value="百度一下" />
</div>
</div>
<!--网页声明部分-->
<div id="footer">
<img src="img/footer.png"/>
</div>
</body>
</html>
*{margin: 0px;padding: 0px;}
#header{width: 100%;height: 45px;}
#header_nav{position: absolute;right: 94px;top:26px;}
#header_nav li{float: left;list-style-type: none;margin-left: 24px;}
#header_nav li a{color: black;font-weight: 700;color:#333;font-size: 13px; line-height: 24px;}
#main{width: 100%;height: 379px;background-color: white;text-align: center;border-top: solid 1px white;}
#main_logo{margin-top: 60px;margin-bottom: 18px;}
input[type=text]{
width: 537px;
height: 36px;
border: solid 0.5px #4791FF;
background-image: url(../img/inp.png);
background-repeat: no-repeat;
background-position-x: 500px;
background-position-y: 5px;
}
input[type=submit]{
height: 36px;
width: 103px;
border: solid 1px #2d78f4;
background-color: #2d78f4;
font-size: 15px;
letter-spacing: 1px;
color: #fff;
position: relative;
right: 5px;
top:1px;
}
#footer{width: 100%;height: 210px;text-align: center;}
9 百度首页模拟
<html>
<head>
<title>百度首页模拟</title>
<meta charset="UTF-8"/>
<!--
模拟百度首页:
1、首页使用css+div进行页面的布局
css代码一般使用外联方式进行开发
2、使用HTML进行每一个块中的内容填充
-->
<!--引入外部声明的css文件-->
<link rel="stylesheet" type="text/css" href="css/baidu01.css"/>
<!--引入网页标题图片-->
<link href="img/title.ico" rel="shortcut icon"/>
</head>
<body>
<!--声明头 部分-->
<div id="header">
<!--声明导航栏-->
<ul id="header_nav">
<li><a href="">新闻</a></li>
<li><a href="">hao123</a></li>
<li><a href="">地图</a></li>
<li><a href="">视频</a></li>
<li><a href="">贴吧</a></li>
<li><a href="">学术</a></li>
<li><a href="">登录</a></li>
<li><a href="">设置</a></li>
</ul>
</div>
<!--声明主体-->
<div id="main">
<!--引入百度logo-->
<img id="img_logo" src="img/bd_logo.png" width="270px" height="129px"/>
<br />
<!--声明搜索框和按钮-->
<input type="text" name="" id="" value="" />
<input type="submit" name="" id="" value="百度一下" />
</div>
<!--声明底部-->
<div id="footer">
<!--声明网站声明内容-->
<img src="img/footer.png"/>
</div>
</body>
</html>
*{margin: 0px;padding: 0px;}
#header{width: 100%;height: 45px;}
#header_nav{position: absolute;right:92px;top:26px;}
#header_nav li{float: left;list-style-type: none;margin-left: 24px;}
#header_nav li a{color: #333;font-size: 13px;font-weight: 700;line-height: 24px;}
#main{width:100%;height: 384px;text-align: center;}
#img_logo{margin-top: 30px;margin-bottom: 21px;}
input[type=text]{height: 34px;width: 539px;border: solid 1px #4992FF;background-image: url(../img/inp.png);background-repeat: no-repeat;background-position-x: 500px;background-position-y: 3px;}
input[type=submit]{width: 100px;height: 34px;font-size: 15px;color: #fff;background-color: #2d78f4;border: solid #2d78f4;letter-spacing: 1px;position: relative;right: 5px;top:1px;}
#footer{width: 100%;height: 206px;text-align: center;}
#header_nav li a:hover{color: blue;}
10 轮播
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js文字图片轮播</title>
<meta charset="UTF-8"/>
<style type="text/css">
<!--
*
{
margin:0;
padding:0;
font-size:12px;
}
ul,li
{
list-style:none;
}
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
.clearfix{zoom:1;}
#dd { width:303px; height:80px; margin:50px auto 0 auto; border:1px solid #ccc; border-right:none; overflow:hidden;}
#dd li { float:left; width:50px; height:80px; overflow:hidden;border-right:1px solid #ccc;}
#dd li span { width:50px; display:inline-block; text-align:center; line-height:80px;}
#dd li p { width:150px; display:inline-block;}
#dd li.on { width:200px;}
#ii { width:303px; margin:5px auto 0 auto;}
#ii li { float:left; width:10px; height:10px; margin:0 1px; background:#ccc; text-align:center; line-height:10px; cursor:pointer;}
#ii li.on{ background:#6CF;}
-->
</style>
</head>
<body>
<ul id="dd" class="clearfix">
<li><span>表一</span><p>户籍管理松动<br />权益捆绑依旧</p></li>
<li><span>表二</span><p>户籍管理松动<br />权益捆绑依旧</p></li>
<li><span>表三</span><p>户籍管理松动<br />权益捆绑依旧</p></li>
</ul>
<ul id="ii" class="clearfix">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script type="text/javascript">
<!--
function newSlider()
{
var dl=document.getElementById("dd").getElementsByTagName("li");
var il=document.getElementById("ii").getElementsByTagName("li");
var dlen = dl.length;
var timer = null,index = 0,autoTime = 3000;
dl[0].className="on",il[0].className="on";
function play(j)
{
index = j;
stopAuto();
for (var i=0;i<dlen ;i++ )
{
dl[i].className="";
il[i].className="";
}
dl[j].className="on";
il[j].className="on";
startAuto();
}
for ( var i=0;i<dlen ;i++ )
{
dl[i].onmouseover=function(j)
{
return function(){play(j);}
}(i)
}
for ( var i=0;i<dlen ;i++ )
{
il[i].onclick=function(j)
{
return function(){play(j);}
}(i)
}
function startAuto()
{
timer = setInterval
(
function()
{
index++;
index = index>dlen-1?0:index;
play(index);
},autoTime
);
}
function stopAuto()
{
clearInterval(timer);
}
startAuto()
}
window.onload=function(){newSlider();}
</script>
</body>
</html>
11 俄罗方块
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>俄罗斯方块</title>
<style type="text/css">
.c {
margin: 1px;
width: 19px;
height: 19px;
background: red;
position: absolute;
}
.d {
margin: 1px;
width: 19px;
height: 19px;
background: gray;
position: absolute;
}
.f {
top: 0px;
left: 0px;
background: black;
position: absolute;
}
.e {
top: 0px;
background: #151515;
position: absolute;
}
.g {
width: 100px;
height: 20px;
color: white;
position: absolute;
}
</style>
<script type="text/javascript">
var row = 18;
var col = 10;
var announcement = 6;
var size = 20;
var isOver = false;
var shapes = ("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";");
var tetris;
var container;
function createElm(tag, css) {
var elm = document.createElement(tag);
elm.className = css;
document.body.appendChild(elm);
return elm;
}
function Tetris(css, x, y, shape) {
var myCss = css ? css : "c";
this.divs = [createElm("div", myCss), createElm("div", myCss), createElm("div", myCss), createElm("div", myCss)];
if (!shape) {
this.divs2 = [createElm("div", myCss), createElm("div", myCss), createElm("div", myCss), createElm("div", myCss)];
this.score = createElm("div", "g");
this.score.style.top = 10 * size + "px";
this.score.style.left = (col - -1) * size + "px";
this.score.innerHTML = "score:0";
}
this.container = null;
this.refresh = function() {
this.x = (typeof(x) != 'undefined') ? x : 3;
this.y = (typeof(y) != 'undefined') ? y : 0;
if (shape)
this.shape = shape;
else if (this.shape2)
this.shape = this.shape2;
else
this.shape = shape ? shape : shapes[Math.floor((Math.random() * shapes.length - 0.000000001))].split(",");
this.shape2 = shapes[Math.floor((Math.random() * shapes.length - 0.000000001))].split(",");
if (this.container && !this.container.check(this.x, this.y, this.shape)) {
isOver = true;
alert("游戏结束");
} else {
this.show();
this.showScore();
this.showAnnouncement();
}
}
this.show = function() {
for (var i in this.divs) {
this.divs[i].style.top = (this.shape[i * 2 + 1] - -this.y) * size + "px";
this.divs[i].style.left = (this.shape[i * 2] - -this.x) * size + "px";
}
}
this.showAnnouncement = function() {
for (var i in this.divs2) {
this.divs2[i].style.top = (this.shape2[i * 2 + 1] - -1) * size + "px";
this.divs2[i].style.left = (this.shape2[i * 2] - -1 - -col) * size + "px";
}
}
this.showScore = function() {
if (this.container && this.score) {
this.score.innerHTML = "score:" + this.container.score;
}
}
this.hMove = function(step) {
if (this.container.check(this.x - -step, this.y, this.shape)) {
this.x += step;
this.show();
}
}
this.vMove = function(step) {
if (this.container.check(this.x, this.y - -step, this.shape)) {
this.y += step;
this.show();
} else {
this.container.fixShape(this.x, this.y, this.shape);
this.container.findFull();
this.refresh();
}
}
this.rotate = function() {
var newShape = [this.shape[1], 3 - this.shape[0], this.shape[3], 3 - this.shape[2], this.shape[5], 3 - this.shape[4], this.shape[7], 3 - this.shape[6]];
if (this.container.check(this.x, this.y, newShape)) {
this.shape = newShape;
this.show();
}
}
this.refresh();
}
function Container() {
this.init = function() {
var bgDiv = createElm("div", "f");
bgDiv.style.width = size * col + "px";
bgDiv.style.height = size * row + "px";
var bgDiv = createElm("div", "e");
bgDiv.style.left = size * col + "px";
bgDiv.style.width = size * announcement + "px";
bgDiv.style.height = size * row + "px";
this.score = 0;
}
this.check = function(x, y, shape) {
var flag = false;
var leftmost = col;
var rightmost = 0;
var undermost = 0;
for (var i = 0; i < 8; i += 2) {
if (shape[i] < leftmost)
leftmost = shape[i];
if (shape[i] > rightmost)
rightmost = shape[i];
if (shape[i + 1] > undermost)
undermost = shape[i + 1];
if (this[(shape[i + 1] - -y) * 100 - -(shape[i] - -x)])
flag = true;
}
for (var m = 0; m < 3; m++)
for (var n = 0; n < col; n++)
if (this[m * 100 + n])
flag = true;
if ((rightmost - -x + 1) > col || (leftmost - -x) < 0 || (undermost - -y + 1) > row || flag)
return false;
return true;
}
this.fixShape = function(x, y, shape) {
var t = new Tetris("d", x, y, shape);
for (var i = 0; i < 8; i += 2)
this[(shape[i + 1] - -y) * 100 - -(shape[i] - -x)] = t.divs[i / 2];
}
this.findFull = function() {
var s = 0;
for (var m = 0; m < row; m++) {
var count = 0;
for (var n = 0; n < col; n++)
if (this[m * 100 + n])
count++;
if (count == col) {
s++;
this.removeLine(m);
}
}
this.score -= -this.calScore(s);
}
this.calScore = function(s) {
if (s != 0)
return s - -this.calScore(s - 1)
else
return 0;
}
this.removeLine = function(row) {
for (var n = 0; n < col; n++)
document.body.removeChild(this[row * 100 + n]);
for (var i = row; i > 0; i--) {
for (var j = 0; j < col; j++) {
this[i * 100 - -j] = this[(i - 1) * 100 - -j]
if (this[i * 100 - -j])
this[i * 100 - -j].style.top = i * size + "px";
}
}
}
}
function init() {
container = new Container();
container.init();
tetris = new Tetris();
tetris.container = container;
document.onkeydown = function(e) {
if (isOver) return;
var e = window.event ? window.event : e;
switch (e.keyCode) {
case 38:
tetris.rotate();
break;
case 40:
tetris.vMove(1);
break;
case 37:
tetris.hMove(-1);
break;
case 39:
tetris.hMove(1);
break;
}
}
setInterval("if(!isOver) tetris.vMove(1)", 500);
}
</script>
</head>
<body onload="init()">
</body>
</html>