目录
一、创建一个CSS文件
在之前的文件夹中创建一个后缀为.css的文件:
创建好后在右侧代码区进行编写。
二、CSS的样式
CSS样式规则由两个主要的部分构成:选择器,以{}包裹的一条或多条声明:
说明:
选择器是您需要改变样式的对象。
每条声明由一个属性和一个值组成。(无论是一条或多条声明,都需要用{}包裹,且声明用;分割)
属性(property)是您希望设置的样式属性(style attribute)。每个属性有一个值。属性和值被冒号分开。
示例如下:
/* 这是CSS的注释 */
/* 建议每条申明占一行 */
p{
color:red;
text-align:center; /* 文本居中 */
}
选择器
一个页面上的元素众多,选择器就用于在页面中找到/选择需要应用这个样式的对象。
除我们前示的元素选择器外,还有id和class选择器。其中class选择器使用非常普遍。
id选择器
/* 注意:id选择器前有 # 号。 */
#sky{
color: blue;
}
这条规则表明,找到页面上id为sky的那个元素让它呈现蓝色
<p id="sky">蓝色的天空</p>
<p id="forest">绿色的森林</p>
将上述代码添加到HTML文件的body里面,且在head里面修改link:
<link rel="stylesheet" type="text/css" href="web1.css">
href为css文件。
结果如下:
class选择器
css文件代码:
.center{
text-align: center;
}
.large{
font-size: 30px;
}
.red{
color: red;
}
html代码如下:
<p class="center">我会居中显示的</p>
<p class="red">我是红色的</p>
<p class="center large red">我又红又大还居中</p>
<p class="red">我也可以是红的</p>
结果如下:
元素的class值可以多个,也可以重复。因此,在实际应用中,class 选择器应用非常普遍。
三、CSS的三种样式
三种方法:外部样式表,内部样式表,内联样式
1.外部样式表
新建如下内容的一个 HTML文件(后缀为.html)也可在上次创建的进行修改:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 注意下面这个语句,将导入外部的web1.css 样式表文件 -->
<link rel="stylesheet" type="text/css" href="web1.css">
<title>页面标题</title>
</head>
<body>
<h1>我有样式!</h1>
<hr>
<p class="haha">挺酷的</p>
</body>
</html>
打开之前创建的web1.css:
body {
background-color: linen;
text-align: center;
}
h1 {
color: yellow;
}
.haha {
margin-top: 100px;
color: chocolate;
font-size: 50px;
}
一般我们会在项目目录下建一个文件夹如css专门存放样式表文件,如此我们引入样式文件时路径就变为 ./css/web1.css之类的。
引入外部样式表是我们使用样式的主流方式,因为众多的样式规则单独放在一个文件中,与 HTML 内容分开,结构清晰。
结果:
2.内部样式表
我们也可以将样式放在 HTML 文件中,这是内部样式表。如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 注意下面这个语句,将导入外部的web1.css 样式表文件 -->
<link rel="stylesheet" type="text/css" href="mycss.css">
<title>页面标题</title>
<style>
body {
background-color: linen;
text-align: center;
}
h1 {
color: yellow;
}
.haha {
margin-top: 100px;
color: chocolate;
font-size: 50px;
}
</style>
</head>
<body>
<h1>我有样式!</h1>
<hr>
<p class="haha">挺酷的</p>
</body>
</html>
结果如下:
该例子与外部样式表一样的效果,但在<head>元素中引入了<style>标签,放入了样式。。
3.内联样式
所谓内联样式,就是直接把样式规则直接写到要应用的元素中,如:
<h3 style="color:green;">I am a heading</h3>
内联样式是最不灵活的一种方式,完全将内容和样式合在一起,实际应用中非常少见。
4.级联的优先级
前面我们学习了三种使用样式的方式,如果某元素如
在外部、内部及内联样式中都被设置color:red;、color:green;、color:blue;,那么到底是什么颜色,也即到底哪个有效呢?
这就涉及样式的优先级问题,从高到低分别是:
1.内联样式
2.内部样式表或外部样式表
3.浏览器缺省样式
哪个样式定义离元素的距离近,哪个就生效。
三、CSS相关内容
颜色
我们可以采用颜色名称也可以使用颜色RGB16进制值,来设定前景或背景的颜色。如:
<!-- 颜色名称 -->
<h3 style="background-color:Tomato;">Tomato</h3>
<h3 style="background-color:Orange;">Orange</h3>
<h3 style="background-color:DodgerBlue;">DodgerBlue</h3>
<h3 style="background-color:MediumSeaGreen;">MediumSeaGreen</h3>
<h3 style="background-color:Gray;">Gray</h3>
<h3 style="background-color:SlateBlue;">SlateBlue</h3>
<h3 style="background-color:Violet;">Violet</h3>
<h3 style="background-color:LightGray;">LightGray</h3>
<hr>
<!-- 颜色值,3个字节分别代表RGB(Red,Green,Blue)的0~255的值 -->
<h3 style="background-color:#ff0000;">#ff0000</h3>
<h3 style="background-color:#0000ff;">#0000ff</h3>
<h3 style="background-color:#3cb371;">#3cb371</h3>
<h3 style="background-color:#ee82ee;">#ee82ee</h3>
<h3 style="background-color:#ffa500;">#ffa500</h3>
<h3 style="background-color:#6a5acd;">#6a5acd</h3>
<!-- 文本颜色 -->
<h3 style="color:Tomato;">Hello World</h3>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
<p style="color:MediumSeaGreen;">Ad facilis est ducimus rem consectetur, corporis omnis, eveniet esse dolor molestiae numquam odio corrupti, sed obcaecati praesentium accusamus? Tempora, dolor a?</p>
结果如下:
尺寸
我们可以用 height 和 width 设定元素内容的高度和宽度尺寸。常见的尺寸单位有:像数 px,百分比 %等。
HTML文件如下:
<html>
<head>
<link rel="stylesheet" href="./web1.css">
</head>
<body>
<div class="example-1">
该元素高 100 pixels,占据全部宽度,背景色为蓝色
</div>
<div class="example-2">
这个元素宽400像素,高200像素,背景色为红色
</div>
</body>
</html>
CSS文件如下:
.example-1 {
width: 100%;
height: 100px;
background-color: blue;
text-align: center;
}
.example-2 {
height: 200px;
width: 400px;
background-color: red;
text-align: right;
}
得到结果如下:
对齐
对于元素中的文本,可以设置text-align属性为left, center, right即可(显然缺省的是左对齐)。
1.盒子模型
盒子模型指的是一个 HTML 元素可以看作一个盒子。从内到外,这个盒子是由内容 content, 内边距 padding, 边框 border, 外边距 margin构成的,如下图:
说明:
Content 盒子的内容,如文本、图片等
Padding 填充,也叫内边距,即内容和边框之间的区域
Border 边框,默认不显示
Margin 外边距,边框以外与其它元素的区域
HTML 文件如下:
<html>
<head>
<link rel="stylesheet" href="web1.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;
}
结果如下:
点击鼠标右键检查得到第一个元素盒子模型各个内容像素大小:
一个元素真正占据的宽度是:
左外边距 + 左边框宽度 + 左内边距 + 内容宽度 + 右内边距 + 右边框宽度 + 右外边距
因此,我们在用width属性设置元素的宽度时,实际上只设置了其内容的宽度。
2.边框和边距
无论边框、内边距还是外边距,它们都有上下左右四个方向。
边框
HTML文件代码:
<p class="example-1">I have black borders on all sides.</p>
<p class="example-2">I have a blue bottom border.</p>
<p class="example-3">I have rounded grey borders.</p>
<p class="example-4">I have a purple left border.</p>
css文件代码:
.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 */
在之前稍加修改后得到如下:
3.定位
position属性用于对元素进行定位。该属性有以下一些值:
static 静态
relative 相对
fixed 固定
absolute 绝对
设置了元素的position属性后,我们才能使用top, bottom, left, right属性,否则定位无效。
static
设置为静态定位position: static;,这是元素的默认定位方式,也即你设置与否,元素都将按正常的页面布局进行。
即:按照元素在 HTML出现的先后顺序从上到下,从左到右进行元素的安排。
relative
设置为相对定位position: relative;,这将把元素相对于他的静态(正常)位置进行偏移
<!-- HTML代码 -->
<div class="example-relative">我偏移了正常显示的位置。去掉我的样式对比看看?</div>
<!-- CSS代码 -->
.example-relative {
position: relative;
left: 60px;
top: 40px;
background-color: rgb(173, 241, 241);
}
对比图:
fixed
设置为固定定位position: fixed;,这将使得元素固定不动。
此时元素固定的位置仍由top, bottom, left, right属性确定,但相对的是视口(viewport,就是浏览器的屏幕可见区域)
如下的代码将会在浏览器的右下角放置一个固定的按钮元素:
<!-- HTML -->
<div class="broad">占位区域。请将浏览器窗口改变大小,看看右下角的按钮发生了什么?</div>
<div class="example-fixed">这个按钮是固定的</div>
<!-- CSS -->
.example-fixed {
position: fixed;
bottom: 40px;
right: 10px;
padding: 6px 24px;
border-radius: 4px;
color: #fff;
background-color: #9d0f0f;
cursor: pointer;
box-shadow: 0 3px 3px 0 rgba(0,0,0,0.3), 0 1px 5px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.2);
}
.broad {
height: 5000px;
width: 5000px;
padding: 20px;
background-color: darkkhaki;
}
结果如下:
改变窗口大小,按钮大小也会改变,但位置不变。
absolute
设置为绝对定位position: absolute;,将使元素相对于其最近设置了定位属性(非static)的父元素进行偏移。
如果该元素的所有父元素都没有设置定位属性,那么就相对于这个父元素。
注意: 绝对定位此处可能有些混淆,请留意其是仍是相对的,不过是相对最近的父元素
<!-- HTML -->
<div class="example-relative">这是父元素,有 relative 定位属性
<div class="example-absolute">
这是子元素,有 absolute 定位属性
</div>
</div>
<!-- CSS -->
.example-relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid rgb(87, 33, 173);
}
.example-absolute {
position: absolute;
top: 80px;
right: 5px;
width: 200px;
height: 100px;
border: 3px solid rgb(218, 73, 16);
}
4.溢出
当元素内容超过其指定的区域时,我们通过溢出overflow属性来处理这些溢出的部分。
溢出属性有一下几个值:
visible 默认值,溢出部分不被裁剪,在区域外面显示
hidden 裁剪溢出部分且不可见
scroll 裁剪溢出部分,但提供上下和左右滚动条供显示
auto 裁剪溢出部分,视情况提供滚动条
关于滚动,我们还可以单独对上下或左右方向进行,如下代码所示:
<!-- HTML -->
<div class="example-overflow-scroll-y">You can use the overflow property when you want to have better control of the
layout. The overflow property specifies what happens if content overflows an element's box.
</div>
<!-- CSS -->
.example-overflow-scroll-y {
width: 200px;
height: 100px;
background-color: #eee;
overflow-y: scroll;
}
结果如下:
可进行上下滚动。
5.浮动
在一个区域或容器内,我们可以设置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>
结果如下:
个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
浮动元素之后的元素将围绕它。
要将一系列图片从左往右浮动,在图片链接后加(如上):
class=“example-float-right”
6.不透明度
我们可以用opacity对任何元素(不过常用于图片)设置不透明度。
值在[0.0~1.0]之间,值越低,透明度越高。
在创建的.HTML文件中用<style>中定义一个opacity,然后赋值为0-1即可。