html - 如何将一个元素定位在另一个元素之上?

标签 html css position z-index

我有一个菜单和一个搜索框。我想将搜索框与菜单项一起放置。但是我的菜单是在一个名为“custommenu”的 div 中的另一个文件中构建的,它使用以下 css:

#custommenu {
  position:relative;
  z-index:999;
  font-size: 14px;
  margin: 0 auto;
  padding: 10px 16px;
  width: 918px;
  background-color: #FB0A51; 
  border-top-left-radius: 10px 10px; 
  -moz-border-top-left-radius: 10px 10px;
  border-top-right-radius: 10px 10px; 
  -moz-border-top-right-radius: 10px 10px;
}

而我的搜索框位于一个单独的文件中,如下所示:

 <div class="header">
   some code
   <div class="quick-access">
      some code
   <php echo $this->getChildHtml('topSearch') ?>;
   </div>
 </div>

我尝试将以下内容添加到 css 文件中,以便搜索框位于菜单顶部,但它不起作用

 .header .form-search { 
   position:absolute; 
   right:29px;  
   z-index:1000; 
   top: 80px;  
   width:315px;  
   height:30px;  
   padding:1px 0 0 16px; 
 }

搜索框仍然隐藏在菜单后面。我想在菜单中添加搜索框。我该怎么做?

编辑:这是包含搜索框的 div 的 css,

 .header { width:930px; margin:0 auto; padding:10px; text-align:right; position:relative; z-index:10; border-top:3px solid #3C3C42;}
 .header .quick-access { float:right; width:600px;margin-top:-125px; padding:28px 10px 0 0; }
 .header .form-search { position:relative; top: 100px;left: 300px; z-index:9999; width:315px; height:30px; padding:1px 0 0 16px; }

这就是它现在的样子,(紫色链接 - 快速访问,白色框是搜索,它位于粉红色的“自定义菜单”区域后面。我希望白色框位于粉红色区域。所有这是在“标题”内)

z-index issue o/p

最佳答案

@所有

很抱歉这么晚才回复。但是经过一些摆弄,我找到了解决方案。我将标题的 z-index 设置为比自定义菜单更高的值。由于我的标题包含搜索框,因此搜索框需要具有更高的值才能出现在菜单上。

现在的代码是这样的

.header{ position: relative; z-index: 4000; }
 .header search { position: relative; z-index: 99999; }
.custommenu { position: relative; z-index: 1000 ;}

这完美地让我的搜索框在我的菜单顶部对齐。再次感谢所有提供帮助的人。欣赏它。

关于html - 如何将一个元素定位在另一个元素之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927295/

相关文章:

html - 我不想将 CSS 类的属性应用于列表中的元素

css - 使用和不使用 shiny 运行时,使用 rmarkdown ioslides 解决不同的 CSS 行为

jquery 在重新加载网站后恢复可拖动位置

javascript - 在 PHP 中使用 header 函数时页面不会更改,但可以使用 Javascript

javascript - 滑动切换 [Jquery 函数] 不工作

javascript - 云Firestore : Query does not find a document but document exists

jQuery animate() 不动画背景颜色 rgba

html - 自动化测试CSS和HTML前端编码

css - 滚动时使具有相对位置的 Div 位于页脚 div 下方

html - 更改 2 个绝对元素之间的 Z 索引?