代码如下:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="../test.css">
<title>Test title</title>
</head>
<body>
<nav id="user-nav">
<div id="logo-container">
<a href="#index.php" class="link-dec"><img src="img/logo.svg" alt="Loading logo"></a>
<p>This is a site</p>
</div>
<div id="form-container">
<i id="search-icon"></i>
<input type="text" name="search" id="form-search" autocomplete="off" placeholder="Quick search...">
</div>
<div class="links">
<a href="#index.php" class="nav-link">Home</a>
<a href="#Browse" class="nav-link">Services</a>
<a href="#request" class="nav-link">About</a>
</div>
<div class="profile-link">
<a href="#profile" class="user-link">Profile name</a>
<div class="drop-menu">
<a href="#buy">Switch</a>
<a href="#logout">Log out</a>
</div>
</div>
</nav>
</body>
</html>
和CSS:
* {
margin: 0;
padding: 0;
}
body {
background-color: chocolate;
}
#user-nav {
display: flex;
height: 62px;
position: fixed;
background-color: #333;
width: 100%;
border-bottom: 2px solid #fff;
}
#logo-container img {
width: 96px;
height: 60px;
}
#logo-container {
display: flex;
flex-grow: 2;
}
.profile-link {
flex-grow: 1;
text-align: center;
display: flex;
flex-direction: column;
position: relative;
border-left: 1px solid #fff;
height: 100%;
}
.drop-menu {
display: flex;
flex-direction: column;
position: absolute;
top: 64px;
background: #333;
border-left: 1px solid #fff;
width: 100%;
}
我从导航栏中的其他元素中删除了所有多余的CSS,因为我认为它会影响最后的div,但不会影响未对齐。我为div使用了不同的显示属性,即使使用list而不是div作为下拉菜单,它也保留了配置文件名称,我总是得到1px的偏移量。我正在使用flex设置导航栏样式,因为我想使其更具响应性,我不确定这是否会以某种方式影响问题。