使用CSS中的属性“position”优先使用两个元素之一

时间:2016-04-29 07:24:46

标签: html css

我正在开展一个项目,但我遇到了一个我遇到问题的部分。 通常在普通屏幕上,当我将头像悬停在搜索栏旁边时,我的工作看起来像这样:

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>

请帮我查一下我的代码有什么问题。感谢

2 个答案:

答案 0 :(得分:1)

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

  

对于定位框,'z-index'属性指定:

     
      
  • 当前堆叠上下文中框的堆栈级别。
  •   
  • 框是否建立堆叠上下文。
  •   

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

z-index生效,因为.container-1.header-user-menuposition设置为relative。由于.container-1出现在标记.header-user-menu之后,因此z-index更高。

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

/*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>

答案 1 :(得分:0)

它出现但它是搜索栏的背面。将[Activity ( Label = "Failiem.lv", NoHistory = true, ExcludeFromRecents = true, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait ), IntentFilter ( new[]{Intent.ActionSend, Intent.ActionSendMultiple}, Categories = new[]{Intent.CategoryDefault, Intent.CategoryOpenable}, DataMimeType = "*/*" )] public class ShareActivity { ... } [Activity ( MainLauncher = true, Icon = "@mipmap/icon", WindowSoftInputMode = SoftInput.AdjustPan, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, LaunchMode = Android.Content.PM.LaunchMode.SingleTask )] public class MainActivity { ... } 添加到z-index

喜欢

.header-user-menu ul

这里我在你的代码片段上得到了这个结果

enter image description here