gpt4 book ai didi

html - 使用 CSS 中的属性 'position' 优先考虑两个元素之一

转载 作者:行者123 更新时间:2023-11-28 16:20:32 25 4
gpt4 key购买 nike

我正在做一个元素,我遇到了一个问题。通常在普通屏幕上,当我将头像悬停在搜索栏旁边时,我的工作看起来像这样:

On a normal screen

问题发生在我使用小屏幕时,包含“我的帐户”、“我的心愿单”和“注销”按钮的“下拉菜单”在我点击或点击头像时不会出现:

On a small screen

HTML/CSS 代码看起来是这样的(它已被简化以检测我容易犯的错误):

/*Avatar - Dropdown menu*/
.header-user-menu {
float: right;
position: relative;
background-color: #3a3c3e;
width: 75px;
height: 40px;
border-radius: 2px;
}
.header-user-menu:before {
content: '';
width: 0;
height: 0;
top: 18px;
left: 15px;
position: absolute;
border: 5px solid transparent;
border-top-color: #fff;
}
.header-user-menu ul {
display: none;
font: bold 13px Arial, Helvetica, sans-serif;
background-color: inherit;
list-style: none;
position: absolute;
text-align: center;
width: 125px;
top: 25px;
right: 0;
padding: 10px;
border-radius: 2px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
}
.header-user-menu:active ul,
.header-user-menu:hover ul,
.header-user-menu.show ul {
display: block;
}
.header-user-menu img {
border-radius: 50%;
position: absolute;
top: 6px;
max-width: 28px;
right: 10px;
}
/*Search Bar*/
.container-1 {
width: 250px;
vertical-align: middle;
white-space: nowrap;
position: relative;
float: right;
margin-right: 10px;
}
.container-1 input#search {
width: 250px;
height: 40px;
background: #3a3c3e;
border: none;
font-size: 10pt;
float: left;
color: #63717f;
padding-left: 45px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
<div class="header-first-bar">
<div class="header-limiter">
<div class="header-user-menu">
<img src="images/avatar.jpg" alt="User Image" />
<ul>
<li><a href="account.php">My account</a>
</li>
<li><a href="#">My wishlist</a>
</li>
<li><a href="logout.php" class="highlight">Logout</a>
</li>
</ul>
</div>
<div class="container-1">
<span class="icon"><i class="fa fa-search"></i></span>
<input type="search" id="search" placeholder="Search..." />
</div>
</div>
</div>

请帮我找出我的代码有什么问题。谢谢

最佳答案

这是由于 .container-1.header-user-menu 元素的 z-indexz-index 适用于定位元素并指定元素在 z 轴上的位置。

For a positioned box, the 'z-index' property specifies:

  • The stack level of the box in the current stacking context.
  • Whether the box establishes a stacking context.

指定堆栈级别:“z-index”属性 https://www.w3.org/TR/CSS2/visuren.html#z-index

z-index 生效是因为 .container-1.header-user-menuposition 设置为相对。因为 .container-1 在标记中出现在 .header-user-menu 之后,所以 z-index 更高。

要解决这个问题,您可以从 .container-1 中删除 position: relative;,因为它似乎不需要。

/*Avatar - Dropdown menu*/
.header-user-menu {
float: right;
position: relative;
background-color: #3a3c3e;
width: 75px;
height: 40px;
border-radius: 2px;
}
.header-user-menu:before {
content: '';
width: 0;
height: 0;
top: 18px;
left: 15px;
position: absolute;
border: 5px solid transparent;
border-top-color: #fff;
}
.header-user-menu ul {
display: none;
font: bold 13px Arial, Helvetica, sans-serif;
background-color: inherit;
list-style: none;
position: absolute;
text-align: center;
width: 125px;
top: 25px;
right: 0;
padding: 10px;
border-radius: 2px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
}
.header-user-menu:active ul,
.header-user-menu:hover ul,
.header-user-menu.show ul {
display: block;
}
.header-user-menu img {
border-radius: 50%;
position: absolute;
top: 6px;
max-width: 28px;
right: 10px;
}
/*Search Bar*/
.container-1 {
width: 250px;
vertical-align: middle;
white-space: nowrap;
float: right;
margin-right: 10px;
}
.container-1 input#search {
width: 250px;
height: 40px;
background: #3a3c3e;
border: none;
font-size: 10pt;
float: left;
color: #63717f;
padding-left: 45px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
<div class="header-first-bar">
<div class="header-limiter">
<div class="header-user-menu">
<img src="images/avatar.jpg" alt="User Image" />
<ul>
<li><a href="account.php">My account</a>
</li>
<li><a href="#">My wishlist</a>
</li>
<li><a href="logout.php" class="highlight">Logout</a>
</li>
</ul>
</div>
<div class="container-1">
<span class="icon"><i class="fa fa-search"></i></span>
<input type="search" id="search" placeholder="Search..." />
</div>
</div>
</div>

关于html - 使用 CSS 中的属性 'position' 优先考虑两个元素之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com