阿里云主机折上折
  • 微信号
Current Site:Index > <body> - the main content of the document

<body> - the main content of the document

Author:Chuan Chen 阅读数:3302人阅读 分类: HTML

Basic Concepts of the <body> Tag

The <body> tag is one of the most important parts of an HTML document, containing all the visible content of a webpage. When a browser loads an HTML document, the content within the <body> tag is rendered and displayed to the user. This tag is a direct child of the <html> element and typically follows the <head> tag.

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <!-- Place page content here -->
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph of text.</p>
</body>
</html>

Attributes of the <body> Tag

Although many <body> attributes have been deprecated in modern HTML5, understanding them is still helpful for grasping the evolution of HTML:

  • bgcolor: Sets the background color of the page (deprecated)
  • text: Sets the default text color (deprecated)
  • link: Sets the color of unvisited links (deprecated)
  • vlink: Sets the color of visited links (deprecated)
  • alink: Sets the color of active links (deprecated)
  • background: Sets a background image (deprecated)

In modern development, these styles should be implemented using CSS:

<body style="background-color: #f0f0f0; color: #333;">
    <!-- Page content -->
</body>

Content Model of the <body> Tag

The <body> tag can contain almost all HTML elements, including:

  • Text content
  • Headings (<h1> to <h6>)
  • Paragraphs (<p>)
  • Lists (<ul>, <ol>, <dl>)
  • Tables (<table>)
  • Forms (<form>)
  • Multimedia (<img>, <video>, <audio>)
  • Divisions (<div>, <section>, <article>, etc.)
<body>
    <header>
        <h1>Website Title</h1>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </nav>
    </header>
    
    <main>
        <article>
            <h2>Article Title</h2>
            <p>Article content...</p>
        </article>
    </main>
    
    <footer>
        <p>Copyright Information</p>
    </footer>
</body>

DOM Access to the <body> Tag

In JavaScript, the <body> element can be accessed in several ways:

// Direct access
const body = document.body;

// Via getElementsByTagName
const body = document.getElementsByTagName('body')[0];

// Via querySelector
const body = document.querySelector('body');

Event Handling for the <body> Tag

The <body> tag can listen for various events, commonly used for global event handling:

<body onload="init()" onresize="handleResize()" onclick="handleClick(event)">
    <!-- Page content -->
</body>

<script>
function init() {
    console.log('Page loaded');
}

function handleResize() {
    console.log('Window resized');
}

function handleClick(event) {
    console.log('Click event', event.target);
}
</script>

Styling the <body> Tag

CSS can be used to fully control the styling of the <body>:

body {
    font-family: 'Arial', sans-serif;
    line-height: 1.6;
    margin: 0;
    padding: 0;
    background-color: #ffffff;
    color: #333333;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

Best Practices for the <body> Tag

  1. Semantic Structure: Use HTML5 semantic tags to organize content logically.
  2. Performance Optimization: Avoid placing large scripts at the beginning of <body>.
  3. Progressive Enhancement: Ensure basic content remains accessible without JavaScript.
  4. Accessibility: Set appropriate language attributes and text direction.
<body lang="en" dir="ltr">
    <a href="#maincontent" class="skip-link">Skip to main content</a>
    
    <!-- Navigation and other content -->
    
    <main id="maincontent">
        <!-- Main content -->
    </main>
</body>

Special Use Cases for the <body> Tag

In certain frameworks or specialized applications, the <body> tag may have unique uses:

Common Structure in Single-Page Applications (SPA):

<body>
    <div id="app">
        <!-- Dynamic content will be rendered by JavaScript -->
    </div>
    <script src="app.js"></script>
</body>

Print Style Optimization:

<body>
    <div class="content">
        <!-- Printable content -->
    </div>
    <div class="no-print">
        <!-- Non-printable content -->
    </div>
</body>

<style>
@media print {
    .no-print {
        display: none;
    }
}
</style>

Extended Knowledge About the <body> Tag

  1. Document Flow: The <body> is the starting point of the normal document flow.
  2. Viewport-Related Units: The dimensions of <body> affect calculations for units like vh/vw.
  3. Scroll Behavior: The scrolling behavior of <body> can be controlled via CSS.
/* Disable scrolling */
body {
    overflow: hidden;
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

Debugging Techniques for the <body> Tag

During development, temporarily modifying the <body> can aid in debugging:

// Temporarily add a border to visualize layout
document.body.style.border = '1px solid red';

// Inspect the body's box model
console.log(document.body.getBoundingClientRect());

// Monitor body size changes
const observer = new ResizeObserver(entries => {
    for (let entry of entries) {
        console.log('Body size changed:', entry.contentRect);
    }
});
observer.observe(document.body);

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

上一篇:-文档标题</a></p> <p>下一篇:<a href="/article/1812.html" title="<meta>-文档元数据" target="_blank"><meta>-文档元数据</a></p> </div> </article> <div class="designer"> <section><i><img src="https://theme.cccx.cn/favicon.jpg"></i> <h3>Front End Chuan</h3> <p>Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.</p> </section> <span><img src="https://theme.cccx.cn/img/wx.jpg"></span> </div> <div class="ad"><a href="https://www.aliyun.com/minisite/goods?userCode=sxqb5dun" target="_blank"><img src="https://theme.cccx.cn/images/ad3.jpg" alt=""></a></div> <div class="wz_love"> <h2 class="h_title">相关文章</h2> <ul> <li><a href="/article/161.html" target="_blank" title="'<mark>'标签的作用与使用场景"> <i><img src="https://theme.cccx.cn/img/cover/1.jpg" alt="'<mark>'标签的作用与使用场景"></i> <h2>'<mark>'标签的作用与使用场景</h2> <!-- <span>2023-09-23</span> --> </a> </li> <li><a href="/article/20.html" target="_blank" title="表格的列分组(colgroup, col)"> <i><img src="https://theme.cccx.cn/img/cover/119.jpg" alt="表格的列分组(colgroup, col)"></i> <h2>表格的列分组(colgroup, col)</h2> <!-- <span>2023-09-23</span> --> </a> </li> <li><a href="/article/78.html" target="_blank" title="复选框(input type="checkbox")"> <i><img src="https://theme.cccx.cn/img/cover/38.jpg" alt="复选框(input type="checkbox")"></i> <h2>复选框(input type="checkbox")</h2> <!-- <span>2023-09-23</span> --> </a> </li> <li><a href="/article/96.html" target="_blank" title="框架的优缺点分析"> <i><img src="https://theme.cccx.cn/img/cover/163.jpg" alt="框架的优缺点分析"></i> <h2>框架的优缺点分析</h2> <!-- <span>2023-09-23</span> --> </a> </li> <li><a href="/article/51.html" target="_blank" title="表格的边框设置"> <i><img src="https://theme.cccx.cn/img/cover/26.jpg" alt="表格的边框设置"></i> <h2>表格的边框设置</h2> <!-- <span>2023-09-23</span> --> </a> </li> <li><a href="/article/24.html" target="_blank" title="表单的目标窗口(target)"> <i><img src="https://theme.cccx.cn/img/cover/86.jpg" alt="表单的目标窗口(target)"></i> <h2>表单的目标窗口(target)</h2> <!-- <span>2023-09-23</span> --> </a> </li> </ul> </div> </div> <aside class="rbox"> <div class="isgood_news"> <h2 class="h_title">本栏推荐</h2> <ul> <li> <a href="/article/159.html" title="'<figure>'与'<figcaption>'标签的使用" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/56.jpg" alt="'<figure>'与'<figcaption>'标签的使用"></i><em>1</em> <p>'<figure>'与'<figcaption>'标签的使用</p> <!-- <span>2023-09-23</span> --> </a> </li> <li> <a href="/article/51.html" title="表格的边框设置" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/26.jpg" alt="表格的边框设置"></i><em>2</em> <p>表格的边框设置</p> <!-- <span>2023-09-23</span> --> </a> </li> <li> <a href="/article/46.html" title="背景图像的使用" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/95.jpg" alt="背景图像的使用"></i><em>3</em> <p>背景图像的使用</p> <!-- <span>2023-09-23</span> --> </a> </li> <li> <a href="/article/193.html" title="Fetch API与XMLHttpRequest的比较" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/126.jpg" alt="Fetch API与XMLHttpRequest的比较"></i><em>4</em> <p>Fetch API与XMLHttpRequest的比较</p> <!-- <span>2023-09-23</span> --> </a> </li> <li> <a href="/article/74.html" title="代码显示标签(code)" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/7.jpg" alt="代码显示标签(code)"></i><em>5</em> <p>代码显示标签(code)</p> <!-- <span>2023-09-23</span> --> </a> </li> <li> <a href="/article/2050.html" title="<nav>-导航链接" target="_blank"> <i><img src="https://theme.cccx.cn/img/cover/58.jpg" alt="<nav>-导航链接"></i><em>6</em> <p><nav>-导航链接</p> <!-- <span>2023-09-23</span> --> </a> </li> </ul> </div> <!-- <div class="ad_r"><a href="/"><img src="https://theme.cccx.cn/images/ads.png" alt="广告图"></a></div> --> <div class="hot_news"> <h2 class="h_title">人气点击</h2> <ol start="1"> <li> <a href="/article/159.html" title="'<figure>'与'<figcaption>'标签的使用" target="_blank"> <p>'<figure>'与'<figcaption>'标签的使用</p> </a> <span>65650</span> </li> <li> <a href="/article/7.html" title="首行缩进与字母间距" target="_blank"> <p>首行缩进与字母间距</p> </a> <span>65639</span> </li> <li> <a href="/article/1773.html" title="const关键字与常量声明" target="_blank"> <p>const关键字与常量声明</p> </a> <span>65519</span> </li> <li> <a href="/article/887.html" title="VueUse组合式工具库" target="_blank"> <p>VueUse组合式工具库</p> </a> <span>65496</span> </li> <li> <a href="/article/676.html" title="路由匹配器变化(path-to-regexp移除)" target="_blank"> <p>路由匹配器变化(path-to-regexp移除)</p> </a> <span>65449</span> </li> <li> <a href="/article/630.html" title="索引签名与映射类型" target="_blank"> <p>索引签名与映射类型</p> </a> <span>65446</span> </li> <li> <a href="/article/491.html" title="DOM属性操作" target="_blank"> <p>DOM属性操作</p> </a> <span>65423</span> </li> <li> <a href="/article/1150.html" title="可访问性要求" target="_blank"> <p>可访问性要求</p> </a> <span>65419</span> </li> <li> <a href="/article/51.html" title="表格的边框设置" target="_blank"> <p>表格的边框设置</p> </a> <span>65372</span> </li> <li> <a href="/article/1280.html" title="MiniCssExtractPlugin提取CSS" target="_blank"> <p>MiniCssExtractPlugin提取CSS</p> </a> <span>65339</span> </li> </ol> </div> <!-- <div class="ad_r"><a href="/"><img src="https://theme.cccx.cn/images/ads.png" alt="广告图"></a></div> --> <div class="hot_news"> <h2 class="h_title">猜你喜欢</h2> <ol start="1"> <li><a href="/article/2049.html" title="<footer>-页脚或区块尾" target="_blank"> <p><footer>-页脚或区块尾</p> </a> </li> <li><a href="/article/1568.html" title="缩小文件搜索范围配置" target="_blank"> <p>缩小文件搜索范围配置</p> </a> </li> <li><a href="/article/720.html" title="typeof类型操作符" target="_blank"> <p>typeof类型操作符</p> </a> </li> <li><a href="/article/2223.html" title="正则表达式反向断言" target="_blank"> <p>正则表达式反向断言</p> </a> </li> <li><a href="/article/1854.html" title="中间件机制的核心思想" target="_blank"> <p>中间件机制的核心思想</p> </a> </li> <li><a href="/article/401.html" title="连字符处理" target="_blank"> <p>连字符处理</p> </a> </li> <li><a href="/article/2067.html" title="责任链模式(Chain of Responsibility)的请求处理流程" target="_blank"> <p>责任链模式(Chain of Responsibility)的请求处理流程</p> </a> </li> <li><a href="/article/2119.html" title="嵌套文档与子文档操作" target="_blank"> <p>嵌套文档与子文档操作</p> </a> </li> </ol> </div> <!-- <div class="ad_r"><a href="/"><img src="https://theme.cccx.cn/images/ads2.png" alt="广告图"></a></div> --> <!-- <div class="tagsclous"> <h2 class="h_title">标签云</h2> <ul> <a href="/" target="_blank">女程序员</a> <a href="/" target="_blank">互联网</a> <a href="/" target="_blank">自由</a> <a href="/" target="_blank">底气</a> <a href="/" target="_blank">忙碌生活</a> <a href="/" target="_blank">建站流程</a> <a href="/" target="_blank">个人网站</a> <a href="/" target="_blank">建站初衷</a> <a href="/" target="_blank">个人博客</a> <a href="/" target="_blank">草根站长</a> <a href="/" target="_blank">618活动</a> <a href="/" target="_blank">心得</a> <a href="/" target="_blank">感受</a> <a href="/" target="_blank">直播</a> <a href="/" target="_blank">阿里云</a> </ul> </div> --> <!-- <div class="emial_box"> <p>您可以通过以下方式联系我</p> <p>(<b>提议</b>、<b>投诉</b>、<b>合作</b>推荐此方式)</p> <p>发送邮件Email到</p> <p><a href="mailto:cc@cccx.cn" target="_blank" title="cc@cccx.cn" rel="nofollow noopener noreferrer">cc@cccx.cn</a></p> </div> --> </aside> <div class="clear"></div> </div> <footer> <div class="box"> <ul class="footer_nav"> <li><a href="/" target="_blank">网站地图</a></li> <li><a href="all_list.html" target="_blank">所有文章</a></li> <li><a href="tags.html" target="_blank">标签合集</a></li> <li><a href="/" target="_blank">访问统计</a></li> </ul> <div class="guanzhu_img"><i><img src="https://theme.cccx.cn/img/wx.jpg" alt="微信号">微信号</i> <i><img src="https://theme.cccx.cn/img/wxgzh.jpg" alt="微信公众号:前端川">微信公众号</i></div> <div class="copyright"> <p>前端川 版权所有 </p> <p> <a href="http://qgzpdj.ccopyright.com.cn/registrationPublicity.html" target="_blank" title="Work Copyright Registration Number:川作登字-2025-F-00009030" rel="nofollow noopener noreferrer"> Work Copyright Registration Number:川作登字-2025-F-00009030 </a> </p> <p>Copyright © <a href="https://www.cccx.cn" target="_blank">www.cccx.cn</a> All Rights Reserved.</p> </div> </div> </footer> <a href="#" title="返回顶部" class="icon-top"></a> <script> var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6224afcfa97c1c194d265ef169772648"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script src="https://theme.cccx.cn/dep/highlight/highlight.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>