HTML 多列布局教程
32
0
0
本文将介绍如何使用不同方法创建多列布局网页。常见的布局方式包括使用 <div> 元素、HTML5 语义元素以及 <table> 元素。在现代网页设计中,提倡使用语义化标签来提升页面的可读性和维护性。
使用 <div> 元素的布局
<div> 元素是一个通用容器,可以通过 CSS 轻松定位和样式化。在早期网页设计中,常用 <div> 元素搭配 CSS 实现布局。
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>City Gallery</title>
<style>
#header {
background-color: black;
color: white;
text-align: center;
padding: 5px;
}
#nav {
line-height: 30px;
background-color: #eeeeee;
height: 300px;
width: 100px;
float: left;
padding: 5px;
}
#section {
width: 350px;
float: left;
padding: 10px;
}
#footer {
background-color: black;
color: white;
clear: both;
text-align: center;
padding: 5px;
}
</style>
</head>
<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>
<div id="footer">
Copyright W3School.com.cn
</div>
</body>
</html>使用 HTML5 语义元素的布局
HTML5 提供了许多语义化标签,如 <header>、<nav>、<section> 和 <footer>,用于更清晰地定义网页的各个部分。
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>City Gallery</title>
<style>
header {
background-color: black;
color: white;
text-align: center;
padding: 5px;
}
nav {
line-height: 30px;
background-color: #eeeeee;
height: 300px;
width: 100px;
float: left;
padding: 5px;
}
section {
width: 350px;
float: left;
padding: 10px;
}
footer {
background-color: black;
color: white;
clear: both;
text-align: center;
padding: 5px;
}
</style>
</head>
<body>
<header>
<h1>City Gallery</h1>
</header>
<nav>
London<br>
Paris<br>
Tokyo<br>
</nav>
<section>
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</section>
<footer>
Copyright W3School.com.cn
</footer>
</body>
</html> 使用 <table> 元素的布局
虽然 <table> 元素主要用于显示表格化数据,但也可以用于布局。不过,这种方式不推荐,因为它不符合语义化设计的原则。
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table Layout</title>
<style>
table.lamp {
width: 100%;
border: 1px solid #d4d4d4;
}
table.lamp th, td {
padding: 10px;
}
table.lamp td {
width: 40px;
}
</style>
</head>
<body>
<table class="lamp">
<tr>
<th>
<img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
</th>
<td>
The table element was not designed to be a layout tool.
</td>
</tr>
</table>
</body>
</html>总结
- 语义化标签:优先使用 HTML5 的语义标签,它们使代码更具可读性和维护性。
- CSS布局:通过 CSS 实现布局,确保页面的响应性和灵活性。
- 现代布局技术:考虑使用 Flexbox 和 Grid 等现代布局工具来实现复杂的布局。
选择合适的布局方式可以提升网页的可读性、可维护性以及用户体验。
0
快来点个赞吧
发表评论
评论列表