gpt4 book ai didi

html - 添加图像会破坏我的导航栏下拉菜单

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:03 26 4
gpt4 key购买 nike

我正在为某人创建网站。在顶部,我有一个带有链接和下拉菜单的栏,左侧有一个 Logo 。出于某种原因,图像不断弄乱下拉菜单。它功能齐全,但在视觉上是困惑的。要明白我的意思,请将鼠标悬停在下拉菜单上,按钮会扩展到栏外。

我试过将图像包装在一个 div 中,为图像设置边距和填充,并使用不同的图像,但都没有用。如果我将图像指向一个无效的 URL,它会起作用,但显然我想要一个真实的图像。

代码:

function hide(element) {
element.className += " hidden";
}

function unhide(element) {
element.className = element.className.replace(/\bhidden\b/, "");
}

function toggle(button) {
var list = document.getElementById(button.getAttribute("data-list"));
(list.className.search(/\bhidden\b/) > -1) ? unhide(list): hide(list);
window.onclick = function(e) {
if (!e.target.matches(".drop-button") && !e.target.matches(".drop-choices")) {
hide(list);
}
};
}
#back-top {
position: absolute;
left: 0;
right: 0;
top: 0;
height: 50px;
background-color: green;
}

#bar-top {
position: absolute;
display: table;
height: 50px;
top: 0;
left: 0;
right: 0;
padding: 0;
background-color: green;
}

.nav-link {
text-decoration: none;
}

.nav-option {
display: table-cell;
font-size: 16px;
top: 0;
color: white;
width: 200px;
cursor: pointer;
}

#nav-logo {
width: 40px;
height: 40px;
top: 2px;
padding: 0;
}

.drop-button {
margin: 0;
font-size: 16px;
color: white;
width: 100%;
height: 100%;
cursor: pointer;
border: none;
text-align: left;
background-color: inherit;
}

.nav-option:hover,
.nav-option:active,
.nav-option:focus,
.drop-button:hover,
.drop-button:active,
.drop-button:focus {
background-color: blue;
outline: none;
}

.drop-choices {
font-size: 16px;
overflow: auto;
max-height: calc(100vh - 50px);
display: block;
position: absolute;
background-color: lightgray;
min-width: 140px;
z-index: 5;
}

.drop-choices p {
color: black;
padding-top: 6px;
padding-bottom: 6px;
cursor: pointer;
margin: 0;
width: 188px;
text-align: left;
}

.drop-choices p:hover,
.drop-choices p:focus {
background-color: blue;
outline: none;
}

p.drop-title {
cursor: initial;
}

p.drop-title:hover {
background-color: #f9f9f9;
}

.hidden {
display: none;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Home - Jessica Keirns</title>
<link type="text/css" rel="stylesheet" href="topbar.css" />
</head>

<body>
<div id="back-top">
<div id="bar-top">
<a href="/main.xhtml" class="nav-link">
<img src="https://www.thsp.co.uk/wp-content/uploads/2016/11/Icon-Placeholder.png" alt="Home" width="40px" height="40px" />
</a>
<div class="nav-option drop-down" data-list="test" onclick="toggle(this)">
<button class="drop-button">Dropdown</button>
<div id="test" class="drop-choices hidden">
<p tabindex="0" id="choice1">choice1</p>
<p tabindex="0" id="choice2">choice2</p>
</div>
</div>
<a href="/about.xhtml" class="nav-option nav-link">About Me</a>
</div>
</div>
<script type="application/javascript" src="drop.js"></script>
</body>

</html>

最佳答案

添加vertical-align: middle;使居中对齐。

但是,我不建议您使用您曾经使用过的 css 样式编写代码。你应该study关于 tabletable-rowinline-blocktable-cell,然后再开始。因为表格应该采用适当的格式,例如 table-cell 应该只在 table-row 内,而 table-row 应该只在 table 内。如果您感到困惑,只需使用 inline-block 即可,这很容易理解

function hide(element) {
element.className += " hidden";
}

function unhide(element) {
element.className = element.className.replace(/\bhidden\b/, "");
}

function toggle(button) {
var list = document.getElementById(button.getAttribute("data-list"));
(list.className.search(/\bhidden\b/) > -1) ? unhide(list): hide(list);
window.onclick = function(e) {
if (!e.target.matches(".drop-button") && !e.target.matches(".drop-choices")) {
hide(list);
}
};
}
#back-top {
position: absolute;
left: 0;
right: 0;
top: 0;
height: 50px;
background-color: green;
}

#bar-top {
position: absolute;
display: table;
height: 50px;
top: 0;
left: 0;
right: 0;
padding: 0;
background-color: green;
}

.nav-link {
text-decoration: none;
}

.nav-option {
display: table-cell;
font-size: 16px;
top: 0;
color: white;
width: 200px;
cursor: pointer;
vertical-align: middle;
}

#nav-logo {
width: 40px;
height: 40px;
top: 2px;
padding: 0;
}

.drop-button {
margin: 0;
font-size: 16px;
color: white;
width: 100%;
height: 100%;
cursor: pointer;
border: none;
text-align: left;
background-color: inherit;
}

.nav-option:hover,
.nav-option:active,
.nav-option:focus,
.drop-button:hover,
.drop-button:active,
.drop-button:focus {
background-color: blue;
outline: none;
}

.drop-choices {
font-size: 16px;
overflow: auto;
max-height: calc(100vh - 50px);
display: block;
position: absolute;
background-color: lightgray;
min-width: 140px;
z-index: 5;
}

.drop-choices p {
color: black;
padding-top: 6px;
padding-bottom: 6px;
cursor: pointer;
margin: 0;
width: 188px;
text-align: left;
}

.drop-choices p:hover,
.drop-choices p:focus {
background-color: blue;
outline: none;
}

p.drop-title {
cursor: initial;
}

p.drop-title:hover {
background-color: #f9f9f9;
}

.hidden {
display: none;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Home - Jessica Keirns</title>
<link type="text/css" rel="stylesheet" href="topbar.css" />
</head>

<body>
<div id="back-top">
<div id="bar-top">
<a href="/main.xhtml" class="nav-link">
<img src="https://www.thsp.co.uk/wp-content/uploads/2016/11/Icon-Placeholder.png" alt="Home" width="40px" height="40px" />
</a>
<div class="nav-option drop-down" data-list="test" onclick="toggle(this)">
<button class="drop-button">Dropdown</button>
<div id="test" class="drop-choices hidden">
<p tabindex="0" id="choice1">choice1</p>
<p tabindex="0" id="choice2">choice2</p>
</div>
</div>
<a href="/about.xhtml" class="nav-option nav-link">About Me</a>
</div>
</div>
<script type="application/javascript" src="drop.js"></script>
</body>

</html>

关于html - 添加图像会破坏我的导航栏下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44511592/

26 4 0