复习挑战
本课属于 Coddy HTML 学习旅程的“使用 CSS 添加样式”单元。第 60 课,共 76 课。
挑战
简单现在你已经学习了 CSS 中的各种定位技巧,包括 relative、absolute、fixed 和 z-index,是时候通过一个综合挑战来检验你的知识了。这个挑战将帮助你巩固对如何结合使用这些属性来创建复杂、动态布局的理解。你将运用所学的各种定位方式,重现一个特定的设计。
这里提供了一份披萨餐厅网站的 HTML 文档。你的任务是使用 CSS 定位属性,为以下元素设置样式,创建整洁有序的布局。
按照以下步骤操作:
- 设置页眉样式:
- 选中
<header>元素。 - 将 position 设置为
fixed,使页眉固定在页面顶部。 - 将 top 设置为
0,将 left 设置为0,并将 width 设置为100%,确保页眉横跨整个页面。 - 将 z-index 设置为
10,确保页眉位于页面上的其他元素之上。
- 选中
- 设置菜单单元样式:
- 选中
.menu类。 - 将 position 设置为
relative,使其可以相对于自身在文档流中的正常位置进行定位。
- 选中
- 设置促销框样式:
- 选中
.promotions类。 - 将 position 设置为
absolute,使其相对于.menu单元定位。 - 将 top 设置为
100px,将 right 设置为20px,使其位于菜单右上方。 - 将 z-index 设置为
5,确保它位于页眉下方,但在菜单中的其他内容上方。
- 选中
亲手试试
<html>
<head>
<title>Pizza Restaurant</title>
<style>
/* Fixed Header */
header {
background-color: #ff6f61;
padding: 20px;
text-align: center;
font-size: 24px;
color: white;
}
/* Menu Section (Relative) */
.menu {
margin-top: 70px; /* Space for fixed header */
padding: 20px;
background-color: #f8f8f8;
font-size: 18px;
}
/* Promotions Box (Absolute) */
.promotions {
background-color: #ffdf00;
padding: 10px;
font-size: 20px;
border-radius: 10px;
}
</style>
</head>
<body>
<!-- 固定页眉 -->
<header>
Pizza Restaurant
</header>
<!-- 菜单区域 -->
<div class="menu">
<h2>Our Menu</h2>
<ul>
<li>Margherita</li>
<li>Pepperoni</li>
<li>Veggie</li>
<li>Hawaiian</li>
</ul>
<p>Order now and enjoy!</p>
<!-- 促销信息框 -->
<div class="promotions">
<p>Get 20% off your first order!</p>
</div>
</div>
<div class="pizza-info">
<h2>About Pizza</h2>
<p>Pizza, a dish with origins in Italy, is one of the world's most beloved foods. It was first made in Naples during the 18th century, with the classic Margherita pizza being named after Queen Margherita of Savoy. The traditional pizza is known for its simple ingredients: a thin crust, tomato sauce, mozzarella cheese, and fresh basil.</p>
<p>Over the years, pizza has evolved into a variety of regional styles, from thin-crust New York-style pizza to deep-dish Chicago pizza. While the toppings have diversified, the tradition of sharing a pizza with family and friends remains an essential part of the pizza experience.</p>
<p>In Italy, pizza is often enjoyed casually, typically paired with a glass of wine or soda. It is a symbol of Italian culture, and many believe that a good pizza is a representation of the country's culinary expertise and passion for food.</p>
<p>Pizza's global reach has led to many variations and adaptations in different countries. In Japan, for example, toppings like teriyaki chicken and squid are popular, while in Brazil, green peas and corn are commonly found on pizzas. In the United States, pizza has become a staple of fast food, with different regions offering their unique twists, such as the deep-dish pizza from Chicago and the thin crust of New York City.</p>
</div>
</body>
</html>使用 CSS 添加样式中的所有课
自己练习: Web 在线编辑器