目录
通过学习《web应用基础》课程,我们知道,要学会web应用开发就必须学会HTML,CSS,JavaScript 三大巨头。本文章重点介绍HTML和CSS的基础学习。(基于vscode编写程序)
一、什么是HTML
HTML(HyperText Markup Language)是超文本标记语言,我们用 HTML 来构建 Web 页面即所谓的网页。网页上的内容展示都编写在HTML文本中。
请注意:HTML 不是一门编程语言,而是一种用于定义内容结构的标记语言。
二、什么是CSS
CSS(Cascading Style Sheets)是级联样式表,一种样式规则语言。CSS 将决定HTML上的内容该如何在屏幕上呈现。
网页的内容是由 HTML的元素构建的,这些元素如何呈现,涉及许多方面,如整个页面的布局,元素的位置、距离、颜色、大小、是否显示、是否浮动、透明度等等。
三、HTML的基础使用
在创建.html文本之后,生成HTML初始格式快捷键可以输入:!+tab
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<title>页面标题</title>
</head>
<body>
<h1>主题/标题</h1>
<p>内容</p>
</body>
</html>
HTML文档的结构内容说明如下: 1).<!DOCTYPE html>: 声明文档类型。出于历史原因需要,现在可有可无。
2).<html></html>: <html>元素包裹了整个完整的页面,是一个根元素,其它元素都嵌套到其中。 3).<head></head>: <head>元素是一个容器,它包含了所有你想包含在HTML页面中但不想在HTML页面中显示的内容。这些内容包括你想在搜索结果中出现的关键字和页面描述,CSS样式,字符集声明等等。
4).<meta charset="utf-8">: 这个元素设置文档使用utf-8字符集编码,utf-8字符集包含了人类大部分的文字。基本上他能识别你放上去的所有文本内容。毫无疑问要使用它,并且它能在以后避免很多其他问题。
5).<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">: 指定页面的图标,出现在浏览器标签上。
6).<title></title>: 设置页面标题,出现在浏览器标签上,当你标记/收藏页面时它可用来描述页面。
7).<body></body>: <body>元素。 包含你能在页面看到的所有内容,包括文本,图片,音频,游戏等等。
HTML文档中的一些基础应用:
1.元素的属性:
<!-- 带属性的段落输入框 -->
<p title="这是个title属性">鼠标移上来试试!</p>
<!-- 带属性的输入框 -->
<input type="text">
<input type="password">
说明: 1).一个空格,在属性和元素名称之间。(如果已经有一个或多个属性,就与前一个属性之间有一个空格。)
2).属性名称,后面跟着一个 = 号。
3).一个属性值,由一对引号 "" 引起来。
2.标题大小:
<h1>~<h6>
<h1>This is heading 1</h1>
<p>This is some text.</p>
<hr>
<h2>This is heading 2</h2>
<p>This is some other text.</p>
<hr>
3.文本格式:
<p>You can use the mark tag to <mark>highlight</mark> text.</p>
<p><del>This line of text is meant to be treated as deleted text.</del></p>
<p><s>This line of text is meant to be treated as no longer accurate.</s></p>
<p><ins>This line of text is meant to be treated as an addition to the document.</ins></p>
<p><u>This line of text will render as underlined</u></p>
<p><small>This line of text is meant to be treated as fine print.</small></p>
<p><strong>This line rendered as bold text.</strong></p>
<p><em>This line rendered as italicized text.</em></p>
4.超链接语法:
<a href="https://www.baidu.com/" target="_blank">百度一下</a>
说明:
1).href为 Hypertext Reference 的缩写,表示要跳转去的地址 URL(Uniform Resorce Locator)
2).target属性为_blank表示在新的页面打开超链接(默认是在当前页面打开即_self)
3).超链接标签包含的内容(当前为文字"百度一下")即为显示在页面上供用户点击的
5.图片及文件路径:
<img src="https://mdbootstrap.com/img/logo/mdb192x192.jpg" alt="MDB Logo" width="200" height="200">
说明:
1).src属性为要显示图片文件的位置 URL,即图片文件的路径
2).alt属性当获取图片出现问题时显示的文字(占位符)
3).可为图片指定高宽度,但不建议(可能导致图片变形)
以上为一些基础的HTML相关元素使用,更多学习可以访问 菜鸟教程
四、CSS的基础使用
我们一般有三种方法:外部样式表,内部样式表,内联样式
外部样式表导入:
<!-- 注意下面这个语句,将导入外部的 mycss.css 样式表文件 -->
<link rel="stylesheet" type="text/css" href="mycss.css">
内部样式表:
<!-- 下面内容放在head元素中 -->
<style>
body {
background-color: linen;
text-align: center;
}
h1 {
color: red;
}
.haha {
margin-top: 100px;
color: chocolate;
font-size: 50px;
}
</style>
内联样式:
<h3 style="color:green;">I am a heading</h3>
级联的优先级:
1.内联样式
2.内部样式表或外部样式表
3.浏览器缺省样式
常见的CSS样式使用:
1.前景或背景的颜色设置(使用颜色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>
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;
4.盒子模型:
从内到外,这个盒子是由内容 content, 内边距 padding, 边框 border, 外边距 margin构成的
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;
}
5.定位 position:
static 静态
relative 相对
fixed 固定
absolute 绝对
设置了元素的position属性后,我们才能使用top, bottom, left, right属性,否则定位无效
设置为静态定位position: static;,这是元素的默认定位方式,也即你设置与否,元素都将按正常的页面布局进行
设置为相对定位position: relative;,这将把元素相对于他的静态(正常)位置进行偏移
设置为固定定位position: fixed;,这将使得元素固定不动(即使你上下左右拖动浏览器的滚动条)
设置为绝对定位position: absolute;,将使元素相对于其最近设置了定位属性(非static)的父元素进行偏移
6.不透明度:
我们可以用opacity对任何元素(不过常用于图片)设置不透明度。
值在[0.0~1.0]之间,值越低,透明度越高
<html>
<head>
<style>
img {
width: 25%;
border-radius: 10px;
float: left;
margin: 10px;
}
.opacity-2 {
opacity: 0.2;
}
.opacity-5 {
opacity: 0.5;
}
.opacity-10 {
opacity: 1;
}
</style>
</head>
<body>
<img class="opacity-2" src="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
<img class="opacity-5" src="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
<img class="opacity-10" src="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
</body>
</html>
7.伪类和伪元素:
伪类(pseudo-class)或伪元素(pseudo-element)用于定义元素的某种特定的状态或位置等。
比如我们可能有这样的需求:
1).鼠标移到某元素上变换背景颜色
2).超链接访问前后访问后样式不同
3).离开必须填写的输入框时出现红色的外框进行警示
4).保证段落的第一行加粗,其它正常
使用语法:
/* 选择器后使用 : 号,再跟上某个伪类/伪元素 */
selector:pseudo-class/pseudo-element {
property:value;
}
常用方法:
a:link {color:#FF0000;} /* 未访问的链接 */
a:visited {color:#00FF00;} /* 已访问的链接 */
a:hover {color:#FF00FF;} /* 鼠标划过链接 */
/* 鼠标移到段落则改变背景颜色 */
p:hover {background-color: rgb(226, 43, 144);}
p:first-line{color:blue;} /* 段落的第一行显示蓝色 */
p:first-letter{font-size: xx-large;} /* 段落的第一个字超大 */
h1:before { content:url(smiley.gif); } /* 在每个一级标题前插入该图片 */
h1:after { content:url(smiley.gif); } /* 在每个一级标题后插入该图片 */
以上为一些基础的CSS使用方法,更多学习可以 前往链接
五、CSS框架介绍
相对于基础的CSS 需要自己编写框架和格式内容,让我们认识一下Bootstrap(CSS框架),框架的使用让Web开发更迅速、简单。了解 Bootstrap 下面我们学习来学习MDBootstrap
教程一:公司网站首页 公司网站首页
教程二:单页应用(SPA) 单页应用
教程三:全屏作品网站 全屏作品网站
六、总结
在web基础应用中,css的书写是多样复杂的我们要学会使用Bootstrap框架以及开源,这样会有更多的时间和精力去美化内容。