




这里注意,css文件记得加上以下这句:
@charset "utf-8";



第四点,属性值与属性值不分先后顺序。可以用下面代码中的border属性的属性值来参考,10px、#f00和solid是border的三个属性值,先后顺序不影响最终得结果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div { width: 100px;height: 100px;background: #f00; border: 10px #000 solid;}
</style>
</head>
<body>
<div></div>
</body>
</html>
结果如下:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{width: 300px; height: 30px;background: #f00;margin-bottom: 20px;}
h1{width: 600px;height: 50px;background: #0f0;font-weight: normal;}
span{color: #fff;}
</style>
</head>
<body>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div><span>阿里巴巴</span></div>
<h1><span>阿里巴巴</span></h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
<h1>阿里巴巴</h1>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{width: 300px; height: 30px;background: #f00;margin-bottom: 20px;}
h1{width: 600px;height: 50px;background: #0f0;font-weight: normal;}
span{color: #fff;}
#box1{width: 500px;height: 50px;background: #0ff;}
#box2{width: 800px;height: 100px;background: #ff0;}
</style>
</head>
<body>
<div id="box1">阿里巴巴</div>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<div><span>阿里巴巴</span></div>
<h1><span>阿里巴巴</span></h1>
<h1>阿里巴巴</h1>
<h1 id="box2">阿里巴巴</h1>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{width: 300px; height: 30px;background: #f00;margin-bottom: 20px;}
h1{width: 600px;height: 50px;background: #0f0;font-weight: normal;}
span{color: #fff;}
#box1{width: 500px;height: 50px;background: #0ff;}
#box2{width: 800px;height: 100px;background: #ff0;}
.aa{border: 5px #000 dotted;}
</style>
</head>
<body>
<div id="box1">阿里巴巴</div>
<div class="aa">阿里巴巴</div>
<div>阿里巴巴</div>
<div><span>阿里巴巴</span></div>
<h1><span>阿里巴巴</span></h1>
<h1 class="aa">阿里巴巴</h1>
<h1 id="box2">阿里巴巴</h1>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;}
div{width: 300px;height: 30px;background: #f00;}
h1{background: #0f0;}
p{background: #00f;}
</style>
</head>
<body>
<div>阿里巴巴</div>
<div>阿里巴巴</div>
<h1>阿里巴巴</h1>
<p>阿里巴巴</p>
<ul>
<li>阿里巴巴</li>
<li>阿里巴巴</li>
<li>阿里巴巴</li>
<li>阿里巴巴</li>
<li>阿里巴巴</li>
</ul>
</body>
</html>

如下面代码中,
.box,h1,#fox{width: 600px;height: 50px;border: 5px #000 dashed;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.box,h1,#fox{width: 600px;height: 50px;border: 5px #000 dashed;}
.box{background: #f00;}
h1{background: #0f0;}
#fox{background: #00f;}
</style>
</head>
<body>
<div class="box">阿里巴巴</div>
<h1>阿里巴巴</h1>
<p id="fox">阿里巴巴</p>
</body>
</html>
效果如下:


如下面代码中,在不同的父选择器里,用了名字相同的<span>选择器,可以通过包含选择符这个特点,用“父选择器 同名选择器”来分别控制区分。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.box1,.box2{width: 600px;border: 3px #000 solid;margin-bottom: 20px; }
.box1 span{background: #0f0;}
.box2 span{background: #030;}
</style>
</head>
<body>
<div class="box1">1998年11月11日,马化腾和同学张志东在广东省深圳市正式注册成立“深圳市腾讯计算机系统有限公司”,之后许晨晔、陈一丹、曾李青相继加入。<span class="tt">当时公司的业务是拓展无线网络寻呼系统,</span>为寻呼台建立网上寻呼系统,这种针对企业或单位的软件开发工程是所有中小型网络服务公司的最佳选择。</div>
<div class="box2">1998年11月11日,马化腾和同学张志东在广东省深圳市正式注册成立“深圳市腾讯计算机系统有限公司”,之后许晨晔、陈一丹、曾李青相继加入。<span>当时公司的业务是拓展无线网络寻呼系统,</span>为寻呼台建立网上寻呼系统,这种针对企业或单位的软件开发工程是所有中小型网络服务公司的最佳选择。</div>
</body>
</html>
效果如下,父选择器box1和box2里面分别控制了不同的样式。解决了控制两个同名选择器<span>实现不同样式。(可以理解为利用父选择器对同名选择器进行准确定位。)



具体如下的代码例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
a{color: #f00;}
a:hover{color:#0f0; }
</style>
</head>
<body>
<h1><a href="#">深圳市腾讯计算机系统有限公司</a></h1>
</body>
</html>
结果如下:




比如下面的例子:
div选择器的背景颜色为 #f00(红色)。
id选择器的背景颜色为:#0f0(绿色)。
class选择器的背景颜色为:#00f(蓝色)。
根据上面的规则:
div类型选择器的权重为:0001
class选择符的权重为:0010
id选择器的权重为:0100
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{width: 100px;height: 100px;background: #f00;}
#box{background: #0f0;}
.fox{background: #00f;}
</style>
</head>
<body>
<div id="box" class="fox"></div>
</body>
</html>
权重比较后发现id选择器的权重最大,表现为id选择器的样式,背景颜色为绿色。效果如下:

第三章 CSS核心属性


<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";}
</style>


需要注意到:这里的<h1>标签的样式是2em,这里的em大小默认为16px,<h1>标签的样式为2乘以16px。但是,如果像下面的代码中,父标签body里面设置了font-size为24px,那么<h1>的2em就是2乘以24px为48px。(意味着字体变大了)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;}
</style>
</head>
<body>
<h1>days</h1>
The days of his youth appeared like dreams before him, and he recalled the serious moment when his father placed him at the entrance of the two roads--one leading to a peaceful,sunny place, covered with flowers,fruits and resounding with soft,sweet songs;the other leading to a deep , dark cave, which was endless, where poison flowed instead of water and where devils and poisonous snakes hissed and crawled. 年轻岁月,如梦般展现在他面前,老人想起父亲把他带到岔路口的那个庄严时刻--一条路通向安宁,快乐的世界,鲜花遍布,果实丰硕,甜美轻柔的歌声在空中回荡;另一条路则通向幽深黑暗,没有尽头的洞穴,洞内流淌着的不是水而是毒液,群魔乱舞,毒蛇嘶嘶爬动。
</body>
</html>

1、这里的#000000中,共有6位。前两位代表红色,中间代表绿色,最后两位代表蓝色(rgb——red green blue)。
2、rgba模式的a表示透明度,读作阿尔法,取值区间在0-1。0表示全透明,1表示实心状态。

1、0到F中,越靠近F,颜色越亮。
例子:color取值模式(十六进制模式、rgb模式、rgba模式)
1、十六进制模式
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;color: #f00;}
</style>
2、rgb模式
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;color: rgb(255,0,0);}
</style>
3、rgba模式
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;color: rgba(255,0,0,1);}
</style>
以上三种颜色模式,同样实现将文字变红。(如下图)


例子:
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-weight: bold;font-size: 24px;color: rgba(255,0,0,1);}
</style>


text-align:left(左对齐)
text-align:right(右对齐)
text-align:center(居中)
text-align:justify(两端对齐)

这个理解为设置“行间距”。
例子:
1、标签<h1>中,<h1>容器的大小为height:200px;要使day水平和垂直方向都居中,需要使用line-height:200px。(同时text-align:center)
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;color: rgba(255,0,0,1);font-weight: bold;}
h1{height: 200px;text-align: center;line-height: 200px;background: #0f0;}
</style>

2、标签<h1>中,<h1>容器的大小为height:200px;要使day水平居中和垂直方向偏上,需要使用line-height:150px。(小于200px)(同时text-align:center)

2、标签<h1>中,<h1>容器的大小为height:200px;要使day水平居中和垂直方向偏上,需要使用line-height:300px。(大于200px)(同时text-align:center)



如下,一个字18px,两个字的距离就是36px。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{font-family: "Times New Roman","微软雅黑";font-size: 24px;color: rgba(255,0,0,1);font-weight: bold;}
h1{text-decoration: underline; height: 200px;text-align: center;line-height: 200px;background: #0f0;}
p{text-indent: 36px;}
</style>
</head>
<body>
<h1>days</h1>
<p> The days of his youth appeared like dreams before him, and he recalled the serious moment when his father placed him at the entrance of the two roads--one leading to a peaceful,sunny place, covered with flowers,fruits and resounding with soft,sweet songs;the other leading to a deep , dark cave, which was endless, where poison flowed instead of water and where devils and poisonous snakes hissed and crawled.</p>
<p> 年轻岁月,如梦般展现在他面前,老人想起父亲把他带到岔路口的那个庄严时刻--一条路通向安宁,快乐的世界,鲜花遍布,果实丰硕,甜美轻柔的歌声在空中回荡;另一条路则通向幽深黑暗,没有尽头的洞穴,洞内流淌着的不是水而是毒液,群魔乱舞,毒蛇嘶嘶爬动。</p>
</body>
</html>
网友评论