gpt4 book ai didi

html - 对齐元素的导航菜单栏问题

转载 作者:行者123 更新时间:2023-11-28 05:07:22 25 4
gpt4 key购买 nike

我正在测试网页顶部的导航栏,下拉菜单在右侧对齐。我在从菜单到栏背景的空间以及对齐图标与文本方面有很多问题。

我的代码是这样的:

function dropdownClick() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}

  @charset "UTF-8";
/* CSS Document */

html
{
position: relative;
min-height: 100%;
font-family: Gotham-Light;

-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}

body {
margin: 0;
}


/* Def. Navigation Bar */

ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #dedee1;
}

li {
float: left;
}

li a, .dropbtn {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.show {display:block;}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link href="assets/css-styles/styles.css" rel="stylesheet">

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<title>navigation</title>
</head>

<body>

<!-- NAVIGATION -->
<nav>
<ul>
<li><a href="#page1">Page1</a></li>
<li><a href="#page2">Page2</a></li>

<li style="float:right" class="dropdown">
<a class="dropbtn" onclick="dropdownClick()"><i class="material-icons">face</i>Username</a>
<div class="dropdown-content" id="myDropdown">
<a href="#settings"><i class="material-icons">settings</i> preferències</a>
<a href="#preferences"><i class="material-icons">lock</i> canviar password</a>
<a href="#logout"><i class="material-icons">power_settings_new</i> log out</a>
</div>
</li>
</ul>

</nav>


</body>
</html>

事件链接的框未与导航栏顶部对齐并且带有文本“用户名”的图标彼此未对齐。

抱歉,我是 CSS 样式的初学者。

提前致谢。

最佳答案

li a, .dropbtn 显示更改为 block 而不是 inline-block(在这种情况下会导致不需要的边距)。

另一个原因是i,给它一个float: left;

最后一个问题是子菜单出现在屏幕外,给它添加一个right: 0

function dropdownClick() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
@charset "UTF-8";
/* CSS Document */

html
{
position: relative;
min-height: 100%;
font-family: Gotham-Light;

-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}

body {
margin: 0;
}


/* Def. Navigation Bar */

ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #dedee1;
}

li {
float: left;
}

li a, .dropbtn {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
right: 0;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.show {display:block;}

.dropbtn i { float: left; }
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link href="assets/css-styles/styles.css" rel="stylesheet">

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<title>navigation</title>
</head>

<body>

<!-- NAVIGATION -->
<nav>
<ul>
<li><a href="#page1">Page1</a></li>
<li><a href="#page2">Page2</a></li>

<li style="float:right" class="dropdown">
<a class="dropbtn" onclick="dropdownClick()"><i class="material-icons">face</i>Username</a>
<div class="dropdown-content" id="myDropdown">
<a href="#settings"><i class="material-icons">settings</i> preferències</a>
<a href="#preferences"><i class="material-icons">lock</i> canviar password</a>
<a href="#logout"><i class="material-icons">power_settings_new</i> log out</a>
</div>
</li>
</ul>

</nav>


</body>
</html>

关于html - 对齐元素的导航菜单栏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39772163/

25 4 0