<body> 标签
<body> 标签定义了 HTML 文档的主体部分。它包含了所有在浏览器窗口中可见的内容,如文本、图像、链接、表格、列表等。
概述
<body> 标签是 HTML 文档的必需元素,它包含了所有在网页中显示给用户的内容。这是用户实际看到的部分,与包含元数据的 <head> 标签相对。
语法
html
<body>
<!-- 页面内容 -->
</body>常用属性
虽然 HTML5 中很少使用 <body> 标签的属性,但以下是一些可用的属性:
bgcolor 属性(已废弃)
设置背景颜色:
html
<!-- 已废弃,应使用 CSS -->
<body bgcolor="#f0f0f0">background 属性(已废弃)
设置背景图像:
html
<!-- 已废弃,应使用 CSS -->
<body background="bgimage.jpg">text 属性(已废弃)
设置文本颜色:
html
<!-- 已废弃,应使用 CSS -->
<body text="#333333">link、vlink、alink 属性(已废弃)
设置链接颜色:
html
<!-- 已废弃,应使用 CSS -->
<body link="#0000ff" vlink="#800080" alink="#ff0000">基本用法
简单页面结构
html
<!DOCTYPE html>
<html>
<head>
<title>页面标题</title>
</head>
<body>
<h1>主标题</h1>
<p>这是一个段落。</p>
</body>
</html>完整页面结构
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>完整页面示例</title>
</head>
<body>
<header>
<h1>网站标题</h1>
<nav>
<ul>
<li><a href="#home">首页</a></li>
<li><a href="#about">关于</a></li>
<li><a href="#contact">联系</a></li>
</ul>
</nav>
</header>
<main>
<section>
<h2>主要内容</h2>
<p>这里是页面的主要内容。</p>
</section>
<aside>
<h3>侧边栏</h3>
<p>这里是侧边栏内容。</p>
</aside>
</main>
<footer>
<p>© 2023 我的网站. 保留所有权利。</p>
</footer>
</body>
</html>内容组织
文本内容
html
<body>
<h1>主标题</h1>
<h2>副标题</h2>
<p>这是一个段落,包含一些<strong>粗体文本</strong>和<em>斜体文本</em>。</p>
<p>这是另一个段落。</p>
</body>列表
html
<body>
<h2>无序列表</h2>
<ul>
<li>列表项 1</li>
<li>列表项 2</li>
<li>列表项 3</li>
</ul>
<h2>有序列表</h2>
<ol>
<li>第一项</li>
<li>第二项</li>
<li>第三项</li>
</ol>
</body>链接和图像
html
<body>
<h2>链接示例</h2>
<p>访问 <a href="https://www.example.com">示例网站</a>。</p>
<h2>图像示例</h2>
<img src="image.jpg" alt="图像描述" width="300" height="200">
</body>表格
html
<body>
<h2>表格示例</h2>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>北京</td>
</tr>
<tr>
<td>李四</td>
<td>30</td>
<td>上海</td>
</tr>
</tbody>
</table>
</body>使用 CSS 样式
内联样式
html
<body style="background-color: #f0f0f0; font-family: Arial, sans-serif;">
<h1 style="color: #333;">标题</h1>
<p style="color: #666; line-height: 1.6;">段落内容</p>
</body>内部样式表
html
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}
p {
color: #666;
line-height: 1.6;
}
</style>
</head>
<body>
<h1>标题</h1>
<p>段落内容</p>
</body>
</html>外部样式表
html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>标题</h1>
<p>段落内容</p>
</body>
</html>事件处理
页面加载事件
html
<body onload="initPage()" onunload="cleanup()">
<h1>页面内容</h1>
<script>
function initPage() {
console.log('页面加载完成');
}
function cleanup() {
console.log('页面即将卸载');
}
</script>
</body>用户交互事件
html
<body onresize="handleResize()" onscroll="handleScroll()">
<div style="height: 2000px;">
<h1>可滚动内容</h1>
<p>向下滚动查看效果。</p>
</div>
<script>
function handleResize() {
console.log('窗口大小改变');
}
function handleScroll() {
console.log('页面滚动');
}
</script>
</body>完整示例
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>完整网页示例</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: white;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
header {
background-color: #333;
color: white;
padding: 1rem;
text-align: center;
}
nav ul {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
background-color: #444;
}
nav li {
margin: 0 15px;
}
nav a {
color: white;
text-decoration: none;
padding: 0.5rem;
}
main {
padding: 2rem 0;
}
section {
margin-bottom: 2rem;
}
footer {
background-color: #333;
color: white;
text-align: center;
padding: 1rem;
margin-top: 2rem;
}
@media (max-width: 768px) {
nav ul {
flex-direction: column;
align-items: center;
}
nav li {
margin: 5px 0;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>我的网站</h1>
<p>欢迎访问我的网站</p>
</header>
<nav>
<ul>
<li><a href="#home">首页</a></li>
<li><a href="#about">关于</a></li>
<li><a href="#services">服务</a></li>
<li><a href="#contact">联系</a></li>
</ul>
</nav>
<main>
<section id="home">
<h2>首页</h2>
<p>欢迎来到我的网站首页。这里有一些关于网站的内容。</p>
<img src="https://via.placeholder.com/300x200" alt="占位图像">
</section>
<section id="about">
<h2>关于我们</h2>
<p>我们是一家专注于网页开发的公司。</p>
<ul>
<li>专业团队</li>
<li>优质服务</li>
<li>客户满意</li>
</ul>
</section>
<section id="services">
<h2>我们的服务</h2>
<ol>
<li>网页设计</li>
<li>前端开发</li>
<li>响应式布局</li>
</ol>
</section>
<section id="contact">
<h2>联系我们</h2>
<p>如有任何问题,请通过以下方式联系我们:</p>
<p>邮箱: <a href="mailto:info@example.com">info@example.com</a></p>
<p>电话: <a href="tel:+1234567890">+1 234 567 890</a></p>
</section>
</main>
<footer>
<p>© 2023 我的网站. 保留所有权利。</p>
</footer>
</div>
<script>
// 简单的导航功能
document.querySelectorAll('nav a').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
});
});
console.log('页面加载完成');
</script>
</body>
</html>最佳实践
- 语义化标签:使用适当的 HTML5 语义化标签组织内容
- 可访问性:确保内容对所有用户都可访问
- 响应式设计:使用 CSS 媒体查询实现响应式布局
- 性能优化:优化图像和资源加载
- SEO 友好:使用适当的标题和结构化内容
- 渐进增强:确保基本功能在没有 JavaScript 的情况下也能工作
浏览器兼容性
<body> 标签在所有浏览器中都得到支持:
- Chrome
- Firefox
- Safari
- Edge
- Internet Explorer
注意事项
- 每个 HTML 文档只能有一个
<body>标签 <body>标签必须是<html>标签的直接子元素<body>标签应该在<head>标签之后- 避免使用已废弃的属性,应使用 CSS 来控制样式
- 确保页面内容在没有 CSS 和 JavaScript 的情况下仍然可读