英文:
Why is my menu button activating across the screen?
问题
我最近创建了一个带有左侧下拉菜单的网站。然而,每当我位于屏幕顶部,即下拉菜单应该出现在左侧的位置时,即使我位于屏幕的另一侧,下拉菜单也会激活。我原本期望它只在我悬停在按钮本身时才激活。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: block;
}
.dropdown-content a:hover {background-color: #79A373;}
.dropdown-content a:hover {border-radius: 5px;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
<div class="dropdown-content">
<a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">About me</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI List</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">Website Stuff</a>
</div>
</div>`
<!-- end snippet -->
英文:
I recently created a website with a dropdown menu on the left side. However, whenever I'm at the top of the screen, where the dropdown would be on the left, the dropdown activates even when I'm on the other side of the screen. I was expecting it to only activate when I hover over the button itself.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: block;
}
.dropdown-content a:hover {background-color: #79A373;}
.dropdown-content a:hover {border-radius: 5px;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
<div class="dropdown-content">
<a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">About me</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI List</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">Website Stuff</a>
</div>
</div>`
<!-- end snippet -->
答案1
得分: 0
Sure, here is the translated code portion:
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: inline-block;
}
.dropdown-content a:hover {
background-color: #79A373;
}
.dropdown-content a:hover {
border-radius: 5px;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: rgb(191, 255, 181);
}
Please note that this is the translated CSS code.
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: inline-block;
}
.dropdown-content a:hover {background-color: #79A373;}
.dropdown-content a:hover {border-radius: 5px;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}
<!-- end snippet -->
try setting the display of the a tags to inline block. The problem is that block display will stretch over the entire width of the container on default.
alternatively you can set a width to the dropdown-content
答案2
得分: 0
根据您在问题中提到的,您希望只有在鼠标悬停在按钮上时才显示dropdown-content
。但是为了在悬停dropdown-content
时仍然保持显示,必须修改您的代码。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: block;
}
.dropdown-content a:hover {background-color: #79A373;}
.dropdown-content a:hover {border-radius: 5px;}
.dropbtn:hover + .dropdown-content, .dropdown-content:hover {display: block;}
.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
<div class="dropdown-content">
<a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">关于我</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI 列表</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">网站内容</a>
</div>
</div>
请确保dropbtn
和dropdown-content
之间始终存在位置上的连续性。否则,将无法悬停在dropdown-content
上。
英文:
As you have mentioned in the question, you want the dropdown-content
to be displayed only when the button is hovered. But for hovering the dropdown-content
, it has to remain displayed even when dropdown-content
is hovered.
So I have modified your code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropbtn {
background-color: rgb(191, 255, 181);
padding: 0px;
font-size: 45px;
border: none;
width: 100px;
height: 100px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #98CC90;
min-width: 16px;
border-radius: 5px;
z-index: 0;
}
.dropdown {
text-align: left;
}
.dropdown-content a {
color: #182016;
padding: 12px 12px;
text-decoration: none;
font-size: 35px;
font-family: Arial, sans-serif;
display: block;
}
.dropdown-content a:hover {background-color: #79A373;}
.dropdown-content a:hover {border-radius: 5px;}
.dropbtn:hover + .dropdown-content, .dropdown-content:hover {display: block;}
.dropdown:hover .dropbtn {background-color: rgb(191, 255, 181);}
<!-- language: lang-html -->
<div class="dropdown">
<button class="dropbtn"><div class="menu"><img src="https://www.freeiconspng.com/thumbs/menu-icon/menu-icon-6.png"/></div></button>
<div class="dropdown-content">
<a onclick="window.location.href='https://ieatlargefrogs.com/about-me.html';">About me</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/dnilist.html';">DNI List</a>
<a onclick="window.location.href='https://ieatlargefrogs.com/website.html';">Website Stuff</a>
</div>
</div>`
<!-- end snippet -->
Please ensure that there is always positional continuity between dropbtn
and dropdown-content
. Otherwise, it will not be possible to hover the dropdown-content
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论