实现界面效果
1. HTML示例代码
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<title>示例主页</title>
</head>
<body>
<header>
<h1>示例页面</h1>
</header>
<div class="navbar">
<a href="#home">主页</a>
<div class="dropdown">
<button class="dropbtn">文件选择
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">打开文件</a>
<a href="#">保存文件</a>
<a href="#">另存为...</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">功能函数
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">计算</a>
<a href="#">排序</a>
<a href="#">过滤</a>
</div>
</div>
</div>
<div class="content">
<h2>欢迎来到示例主页</h2>
<p>这是一个带有下拉菜单的导航栏示例。</p>
</div>
</body>
</html>
2. CSS示例代码
css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #4CAF50;
color: white;
text-align: center;
padding: 1em 0;
}
.navbar {
background-color: #333;
overflow: hidden;
display: flex;
justify-content: center;
}
.navbar a, .dropdown .dropbtn {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
font-size: 16px; /* 设置字体大小 */
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #575757;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.content {
padding: 16px;
text-align: center;
}