学习html语言需要掌握两个基本概念:html元素、html属性。 html由html元素来组成。html元素使用成对的tags,tags中间为内容(using a start tag and an end tag, and with the content in between),此外有一些单独出现的tags(如br, hr, img等)。 html属性用来修饰html元素,比如p标签可以使用align属性来标识对齐,比如font标签可以用size和color标识文本的大小与颜色。 一些html元素必须结合属性使用,如图像元素必须指定src属性。
paragraphs使用<p>来标识,使用<br />来强制换行(<br>也可,加上结束符是为了兼容XHTML,具体可参考stackoverflow的解释)。新的段落会自动加上一个空白行(所以才需要br来换行) 例子:
<html> <head> <title>hello world</title> </head> <body> <p>This is a paragraph</p> <p>This is another paragraph</p> <p>This is <br />a line break</p> </body> </html>HTML支持多种文本风格。 需要注意的是浏览器将em和i标签都显示为斜体,实际上em表示着重(不同的是em存在&emsp加上分号表示四个空格之类的用法);将stong和b标签都显示为粗体,实际上strong也表示着重。 示例如下:
<html> <head> <title>hello world</title> </head> <body> <p>regular text</p> <p>regular text<b>bold text</b></p> <p>regular text<strong>strong text</strong></p> <p>regular text<big>big text</big></p> <p>regular text<small>small text</small></p> <p>regular text<i>italic text</i></p> <p>regular text<sub>subscripted text</sub><p> <p>regular text<sup>superscripted text</sup><p> <p>regular text<ins>inserted text</ins><p> <p>regular text<del>delted text</del></p> </body> </html>标题的标签从h1-h6,分割线为hr,注释为!–your comment– 示例如下:
<html> <head> <title>hello world</title> </head> <body> <h1>first section</h1> <h2>first part of first section</h2> <h1>second section</h1> <!-- this is a row consisted of a horizontal line--> <hr> </body> </html>有序列表为ol标签,无序为ul标签;每列用li标签
<ul> <li>red</li> <li>blue</li> <li>green</li> </ul>效果为
redbluegreen表格由table标签开始和结束 表格按照行堆叠,行标签为tr;每个行标签下包括多个列标签,列标签为td。例如
<table> <tr><td>a</td><td>b</td><td>c</td></tr> <tr><td>d</td><td>e</td><td>f</td></tr> </table>效果为
abcdef对一些html元素如图片可以增加边缘即border属性,指定px值,例子为:
<table border="10px"> <tr><td>a</td><td>b</td><td>c</td></tr> <tr><td>d</td><td>e</td><td>f</td></tr> </table>效果为
abcdef表示表格跨多行和多列 例子:
<table> <tr><td>a</td><td>b</td><td>c</td></tr> <tr><td colspan="3">d</td> </table>效果为
abcdalign, bgcolor等也可结合表格使用 例子:
<table align="center" border="10px"> <tr><td bgcolor="yellow">a</td><td>b</td><td>c</td></tr> <tr><td colspan="3">d</td> </table>效果为
abcdsrc(source属性)用于指定媒体文件的url地址(或本地相对路径) 例子(插入图片)
<img src="https://bkimg.cdn.bcebos.com/pic/0b46f21fbe096b63df6a7b5b01338744ebf8ac4d">对于图片、音频视频等可能存在导入不成功的问题,增加一个alt属性在导入失败时显示提示文本。 例子
<img src="a_wrong_url" alt="load failed">例子
<p align="center">aligned to center</p>常用的就是居中、左对齐、右对齐
文字、线条、图片等html元素都有大小粗细等属性。
文字的大小颜色可以这样指定:
<font size="3" color="purple">example</font>regular text线条的长短指定可以使用
<hr width="20px"> <hr width="20%">使用height和width指定图片的高度和宽度,可以指定长度(px)或比例(%)
<img src="img_url" hight=50% width=50%>用img、audio、video标签指定,必须指定src属性(source属性)。 例子(插入图片)
<img src="https://bkimg.cdn.bcebos.com/pic/0b46f21fbe096b63df6a7b5b01338744ebf8ac4d">用a标签指定
a标签必须指定href属性(Hypertext REFerence属性)。 例子:
<a href="https://www.baidu.com">www.baidu.com</a>a标签可以指定target属性,这决定如何打开链接(如打开新标签页等方式) 属性值_blank标识链接在新标签页打开
<a href="https://www.baidu.com" target="_blank">www.baidu.com</a>