CSS:层叠样式表,用于html文档的呈现
一、定义和应用样式
background-color(属性):grey(值); color:white
关于css的属性请参考:http://www.w3school.com.cn/cssref/index.asp
内嵌样式:
<a href="http://apress.com" style="background-color:grey; color:white">
Visit the Apress website
</a>
文档内嵌样式:
<head>
<title>Example</title>
<style type="text/css">
a {
background-color:grey;
color:white
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a href="http://w3c.org">Visit the W3C website</a>
</body>
顺便讲一下padding 和margin的区别:
用盒子模型说明一下:假设盒子是一个div,盒子的内壁是border,盒子里放的是硬盘,硬盘与边框之间的距离叫做padding ,盒子与盒子之间的距离叫margin.
比如设一个div的padding属性,它的子标签元素会相应移动。而设置它的margin属性,这个div整个标签会依据兄弟元素或父元素移动.
<head>
<title>Example</title>
<style type="text/css">
a {
background-color:grey;
color:white
}
span {
border: thin black solid;
padding: 10px;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a href="http://w3c.org">Visit the W3C website</a>
</body>
外部样式使用:
import:样式表中导入其它样式
charset:样式表中的字符编码。不申明依赖文档编码。
二、样式的层叠和继承
样式来源:除上面讲的三种还有两种:浏览器样式和用户样式
浏览器样式:用户没有设置样式时,浏览器自动加在元素身上的样式
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a href="http://w3c.org">Visit the W3C website</a>
用户样式:允许用户自定义自已的样式表custom.css.特殊功能
什么是层叠?
优先级:
内嵌--文档--外部样式--用户样式--浏览器样式
import用打乱这种顺序,但是用户样式表中的import能干过他。
<head>
<title>Example</title>
<style type="text/css">
a {
color: black !important;
}
</style>
</head>
<body>
<a style="color:red" href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a href="http://w3c.org">Visit the W3C website</a>
</body>
什么是继承?
如果找不到就会云父元素找。
<head>
<title>Example</title>
<style type="text/css">
p {
color:white;
background:grey;
border: medium solid black;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>
不是所有的css属性都可以继承,与无素外观相关的会被继续,其它的不会
inherit:强型继承
<head>
<title>Example</title>
<style type="text/css">
p {
color:white;
background:grey;
border: medium solid black;
}
span {
border: inherit;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>
三、CSS中的颜色
表示方式:名称,十六进制,rgb函数
详细参考:http://www.w3school.com.cn/cssref/css_colors.asp
四、CSS中的长度
绝对长对:in,cm,mm,pt,pc
<head>
<title>Example</title>
<style type="text/css">
p {
background: grey;
color:white;
width: 5cm;
font-size: 20pt;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</bod
相对长度:
em:与字号挂勾
<head>
<title>Example</title>
<style type="text/css">
p {
background: grey;
color:white;
font-size: 15pt;
height: 2em;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<p style="font-size:12pt">I also like mangos and cherries.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>
ex:相对于字符“x”的高度,一般为字体高度的一半
rem:与根元素字号挂勾
<head>
<title>Example</title>
<style type="text/css">
html {
font-size: 0.2in;
}
p {
background: grey;
color:white;
font-size: 2rem;
height: 2em;
}
</style>
</head>
<body style="font-size: 14pt">
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>
px :
像素为显示设备上可寻址的最小单元---图像的基本元素
<head>
<title>Example</title>
<style type="text/css">
p {
background: grey;
color:white;
font-size: 20px;
width: 200px;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>
%
<head>
<title>Example</title>
<style type="text/css">
p {
background: grey;
color:white;
font-size: 200%;
width: 50%;
}
</style>
</head>
<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass1 myclass2" href="http://w3c.org">Visit the W3C website</a>
</body>