html盒子代码div

在HTML中,<div> 元素通常用于创建一个容器,用于包裹其他HTML元素,并且可以通过CSS样式进行布局和样式设置。

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Div示例</title> <style> /* 添加一些样式以便演示 */ .container { width: 300px; height: 200px; background-color: #f0f0f0; border: 1px solid #ccc; padding: 20px; box-sizing: border-box; } .inner-content { background-color: #e0e0e0; padding: 10px; } </style> </head> <body> <!-- 使用 div 元素创建一个容器 --> <div class="container"> <h1>这是一个示例容器</h1> <!-- 在容器中添加其他元素 --> <p class="inner-content">这是容器中的内容。</p> </div> </body> </html>

在上面的示例中,我们创建了一个 <div> 元素,给它一个类名为 "container",并在样式中定义了一些基本的外观属性。容器内部包含了一个 <h1> 标题和一个带有类名 "inner-content" 的段落。

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>网页布局示例</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #333; color: #fff; padding: 10px; text-align: center; } nav { background-color: #eee; padding: 10px; } nav a { text-decoration: none; color: #333; margin-right: 15px; } section { padding: 20px; } aside { float: left; width: 30%; background-color: #f0f0f0; padding: 20px; box-sizing: border-box; } article { float: left; width: 60%; padding: 20px; box-sizing: border-box; } footer { clear: both; background-color: #333; color: #fff; text-align: center; padding: 10px; } </style> </head> <body> <header> <h1>网页标题</h1> </header> <nav> <a href="#">首页</a> <a href="#">关于我们</a> <a href="#">联系我们</a> </nav> <section> <aside> <h2>侧边栏</h2> <p>这是侧边栏的内容。</p> </aside> <article> <h2>主要内容区域</h2> <p>这是主要内容区域的内容。</p> </article> </section> <footer> <p>&copy; 2024 版权所有</p> </footer> </body> </html>

在这个示例中,我们使用了多个 <div> 元素,分别代表页面的不同部分,如头部、导航栏、主要内容区域、侧边栏、文章和页脚。通过这些元素的组合和样式定义,可以创建一个基本的网页布局。