一、CSS简介
CSS是级联样式表(Cascading Style Sheets)的缩写。HTML 用于撰写页面的内容,而 CSS 将决定这些内容该如何在屏幕上呈现。
二、CSS语法
一条CSS样式规则由两个主要的部分构成:选择器,以{ }包裹的一条或多条声明:
h1 {color:red;font-size:36px;}
h1为选择器,选择需要改变样式的对象。
color:red为声明,每条声明有一个属性和一个值组成,其中color为属性,red为值。
- 选择器
1、id选择器
#flowers{
color:red;
}
这条规则表明将页面上id
为flowers
的元素呈现红色
**注意:**元素的id值必须唯一,所以id选择器适用范围只有一个元素
2、class选择器
class
选择器使用比较普遍
/* 注意:class选择器前有 . 号。 */
.center{
text-align: center;
}
.large{
font-size: 30px;
}
.red{
color: red;
}
以上代码定义了三条规则,分别应用于页面上对应的元素,元素的class值可以多个,也可以重复。因此,实际应用中,class 选择器应用非常普遍。
三、CSS生效方法
我们一般有三种方法:外部样式表,内部样式表,内联样式。
- 外部样式表
新建一个HTML文件,然后在同一目录新建一个样式表文件,比如mycss.css。
在HTML文件的<head>
里输入:
<link rel="stylesheet" type="text/css" href="mycss.css">
这样我们就引入了一个外部样式表,引入外部样式表使得CSS与HTML内容分离,结构清晰。同时也能被其他页面引用,达到复用的目的
一般我们会在项目目录下建一个文件夹如css专门存放样式表文件,如此我们引入样式文件时路径就变为 ./css/mycss.css之类的。
- 内部样式表
将样式放在HTML文件中,称为内部样式表
例如在<head>
元素中引入<style>
标签:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 注意下面这个语句,将导入外部的 mycss.css 样式表文件 -->
<link rel="stylesheet" type="text/css" href="mycss.css">
<title>页面标题</title>
<style>
body {
background-color: linen;
text-align: center;
}
h1 {
color: red;
}
.haha {
margin-top: 100px;
color: chocolate;
font-size: 50px;
}
</style>
</head>
<body>
<h1>我是有样式的</h1>
<hr>
<p class="haha">还是有点丑:)</p>
</body>
</html>
- 内联样式
就是直接将样式规则写到要应用的元素中
<h3 style="color:green;">I am a heading</h3>
- 级联优先级
内联样式>内部样式表或外部样式表>浏览器缺省样式。即就近原则
四、页面颜色、大小与位置
(1)颜色
我们可以直接采用颜色名称,也可以使用颜色RGB16进制值设定前景或背景颜色,如:
<h3 style="background-color:SlateBlue;">SlateBlue</h3>
<h3 style="background-color:Violet;">Violet</h3>
<h3 style="background-color:#3cb371;">#3cb371</h3>
<h3 style="background-color:#ee82ee;">#ee82ee</h3>
(2)尺寸
我们可以用height
和width
设定元素内容占据的尺寸,常用单位有像素px
,百分比%
。如:
.example-1 {
width: 100%;
height: 200px;
background-color: powderblue;
text-align: center;
}
.example-2 {
height: 100px;
width: 500px;
background-color: rgb(73, 138, 60);
text-align: right;
}
(3)对齐
对于元素中的文本,我们可以简单的设置text-align
属性为left, center, right
即可(显然缺省的是左对齐)。
五、盒子模型
一个HTML元素可以看做一个盒子,由内容content,内边距padding,边框border,外边距margin
构成
HTML文件:
<html>
<head>
<link rel="stylesheet" href="./mycss.css">
</head>
<body>
<div class="box1">我是内容一,外面红色的是我的边框。注意边框的内外都有25px的距离。</div>
<div class="box2">我是内容二,外面蓝色的是我的边框。注意与上面元素的外边距,发生了叠加,不是50px而是25px。</div>
</body>
</html>
CSS文件:
.box1 {
height: 200px;
width: 200px;
background-color:#615200;
color: aliceblue;
border: 10px solid red;
padding: 25px;
margin: 25px;
}
.box2 {
height: 300px;
width: 300px;
background-color:#004d61;
color: aliceblue;
border: 10px solid blue;
padding: 25px;
margin: 25px;
}
效果:
在页面右键检查
可以看到下图所示
六、边框与边距
- 边框
无论边框、内边距还是外边距,它们都有上下左右四个方向。
.example-1 {
border: 1px dotted black; /* 上下左右都相同 */
}
.example-2 {
border-bottom: 1px solid blue; /* 只设置底部边框 */
}
.example-3 {
border: 1px solid grey;
border-radius: 15px; /* 边框圆角 */
}
.example-4 {
border-left: 5px solid purple;
}
- 边距
padding: 20px; /* 上下左右都相同 */
padding-top: 20px;
padding-bottom: 100px;
padding-right: 50px;
padding-left: 80px;
padding: 25px 50px 75px 100px; /* 简写形式,按上,右,下,左顺序设置 */
padding: 25px 10px; /* 简写形式,上下为25px,左右为10px */
简写时的顺序为上,右,下,左,或者记为顺时针方向。
七、定位
position属性用于对元素进行定位。该属性有以下一些值:
static
静态
这是元素的默认定位方式,无论设置与否,元素都将按正常的页面布局进行relative
相对
将元素相对于它的静态位置进行偏移fixed
固定
将元素固定不动(即使上下左右拖动滚动条),元素固定的位置由top,bottom,left,right属性决定absolute
绝对
将使元素相对于其最近设置了定位属性(非static)的父元素进行偏移。
如果该元素的所有父元素都没有设置定位属性,那么就相对于<body>
这个父元素。
八、溢出
当元素内容超过其指定的区域时,我们通过溢出overflow属性来处理这些溢出的部分。
溢出属性有一下几个值:
visible
默认值,溢出部分不被裁剪,在区域外面显示hidden
裁剪溢出部分且不可见scroll
裁剪溢出部分,但提供上下和左右滚动条供显示auto
裁剪溢出部分,视情况提供滚动条
九、浮动
在一个区域或容器内,我们可以设置float
属性让某元素水平方向上向左或右进行移动,其周围的元素也会重新排列。
让图片向右浮动:
<html>
<head>
<style>
.example-float-right {
float: right;
}
</style>
</head>
<body>
<img src="https://mdbootstrap.com/img/Photos/Others/placeholder1.jpg" class="example-float-right" alt="">
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quidem, architecto officiis, repellendus
corporis obcaecati, et commodi quam vitae vel laudantium omnis incidunt repellat qui eveniet fugiat totam
modi nam vero!</p>
</body>
</html
效果:
十、不透明度
我们可以用opacity对任何元素(常用于图片)设置不透明度。
值在[0.0~1.0]之间,值越低,透明度越高
十一、组合选择器
- 后代选择器
以空格作为分隔,如:.haha p 代表在div元素内有.haha这种类的所有元素。
参见如下代码:
<html>
<head>
<style>
.haha p {
background-color: yellow;
}
</style>
</head>
<body>
<div class="haha">
<p>Paragraph 1 in the div .haha.</p>
<p>Paragraph 2 in the div .haha>.</p>
<span>
<p>Paragraph 3 in the div .haha.</p>
</span>
</div>
<p>Paragraph 4. Not in a div .haha.</p>
<p>Paragraph 5. Not in a div .haha.</p>
</body>
</html>
效果:
- 子选择器(直接后代选择器)
以>作为分隔
<html>
<head>
<style>
.haha > p {
background-color: yellow;
}
</style>
</head>
<body>
<div class="haha">
<p>Paragraph 1 in the div .haha.</p>
<p>Paragraph 2 in the div .haha.</p>
<span>
<p>Paragraph 3 in the div .haha - it is descendant but not immediate child.</p>
</span> <!-- not Child but Descendant -->
</div>
<p>Paragraph 4. Not in a div .haha.</p>
<p>Paragraph 5. Not in a div .haha.</p>
</body>
</html>
效果:
虽然段落3在.haha类中,但它的直接父元素是span,不是.haha的直接后代,所以不能选择。只有段落1、2有黄色背景。