可以通过 <div> 和 <span> 将 HTML 元素组合起来。
1、HTML 块元素
大多数 HTML 元素被定义为块级元素或内联元素。
编者注:“块级元素”译为 block level element,“内联元素”译为 inline element。
块级元素在浏览器显示时,通常会以新行来开始(和结束)。
例子:<h1>, <p>, <ul>, <table>
2、HTML 内联元素
内联元素在显示时通常不会以新行开始。
例子:<b>, <td>, <a>, <img>
3、HTML <div> 元素
HTML <div> 元素是块级元素,它是可用于组合其他 HTML 元素的容器。
<div> 元素没有特定的含义。除此之外,由于它属于块级元素,浏览器会在其前后显示折行。
如果与 CSS 一同使用,<div> 元素可用于对大的内容块设置样式属性。
<div> 元素的另一个常见的用途是文档布局。它取代了使用表格定义布局的老式方法。使用 <table> 元素进行文档布局不是表格的正确用法。<table> 元素的作用是显示表格化的数据。
4、HTML <span> 元素
HTML <span> 元素是内联元素,可用作文本的容器。
<span> 元素也没有特定的含义。
当与 CSS 一同使用时,<span> 元素可用于为部分文本设置样式属性。
5、HTML 分组标签
标签 | 描述 |
---|---|
<div> | 定义文档中的分区或节(division/section)。 |
<span> | 定义 span,用来组合文档中的行内元素。 |
6、HTML 类
对 HTML 进行分类(设置类),使我们能够为元素的类定义 CSS 样式。
为相同的类设置相同的样式,或者为不同的类设置不同的样式。
举例:
1 <!DOCTYPE html> 2 <html> 3 ????<head> 4 ????????<meta charset="utf-8"> 5 ????????<title>My test page</title> 6 ?????????7 ????????<style>.cities { 8 ????????????background-color:black; 9 ????????????color:white;10 ????????????margin:20px;11 ????????????padding:20px;12 ????????}13 ????????</style>14 ????</head>15 ????16 ????<body>17 ????????<!--<div>容器,设置class属性是为了在<head>中配置css样式-->18 ????????<div class="cities">19 ????????????<h2>London</h2>20 ????????????<p>21 ????????????????London is the capital city of England. 22 ????????????????It is the most populous city in the United Kingdom, 23 ????????????????with a metropolitan area of over 13 million inhabitants.24 ????????????</p>25 ????????</div>26 27 ????</body>28 </html>
输出结果:
6.1 分类块级元素
HTML <div> 元素是块级元素。它能够用作其他 HTML 元素的容器。
设置 <div> 元素的类,使我们能够为相同的 <div> 元素设置相同的类:
1 <!DOCTYPE html> 2 <html> 3 ????<head> 4 ????????<meta charset="utf-8"> 5 ????????<title>My test page</title> 6 ?????????7 ????????<style>.cities { 8 ????????????background-color:black; 9 ????????????color:white;10 ????????????margin:20px;11 ????????????padding:20px;12 ????????}13 ????????</style>14 ????</head>15 ????16 ????<body>17 ????????<!--<div>容器,设置class属性是为了在<head>中配置css样式-->18 ????????<div class="cities">19 ????????????<h2>London</h2>20 ????????????<p>21 ????????????????London is the capital city of England. 22 ????????????????It is the most populous city in the United Kingdom, 23 ????????????????with a metropolitan area of over 13 million inhabitants.24 ????????????</p>25 ????????</div>26 ????????????27 ????????<div class="cities">28 ????????????<h2>Paris</h2>29 ????????????<p>Paris is the capital and most populous city of France.</p>30 ????????</div>31 32 ????????<div class="cities">33 ????????????<h2>Tokyo</h2>34 ????????????<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,35 ????????????and the most populous metropolitan area in the world.</p>36 ????????</div>37 38 ????</body>39 </html>
输出结果:
6.2 分类行内元素
HTML <span> 元素是行内元素,能够用作文本的容器。
设置 <span> 元素的类,能够为相同的 <span> 元素设置相同的样式。
1 <!DOCTYPE html> 2 <html> 3 ????<head> 4 ????????<meta charset="utf-8"> 5 ????????<title>My test page</title> 6 ?????????7 ????????<style> 8 ????????????span.red{ 9 ????????????????color: red10 ????????????}11 ????????</style>12 ????</head>13 ????14 ????<body>15 16 ????????<h1>My <span class="red">Important</span> Heading</h1>17 18 ????</body>19 </html>
输出结果:
Web开发——HTML基础(HTML块 <div>/<span> )
原文地址:https://www.cnblogs.com/zyjhandsome/p/9783547.html