使用CSS Flexbox 构建可靠实用的网站 Header,30岁学前端开发
warning:
这篇文章距离上次修改已过202天,其中的内容可能已经有所变动。
以下是使用CSS Flexbox创建一个简单Header的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Header</title>
<style>
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: #333;
color: #fff;
}
.header a {
color: #fff;
text-decoration: none;
}
.header a:hover {
text-decoration: underline;
}
.logo {
font-size: 20px;
font-weight: bold;
}
.nav {
display: flex;
gap: 20px;
}
.nav a {
padding: 5px 10px;
background-color: #555;
border-radius: 5px;
}
.nav a:hover {
background-color: #777;
}
</style>
</head>
<body>
<div class="header">
<div class="logo">
<a href="/">My Website</a>
</div>
<nav class="nav">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
</div>
</body>
</html>
这段代码创建了一个具有logo、导航链接的Header,使用Flexbox布局实现了Header内部元素的灵活排列和居中对齐。
评论已关闭