- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
是否可以仅使用 CSS 将元素相对于一个元素水平放置,而相对于另一个元素垂直放置?我正在尝试向我的导航栏添加一个下拉菜单。但是,子菜单没有正确对齐。我想定位元素 A,使其左边缘与元素 B 的左边缘对齐,但我希望元素 A 的上边缘与元素 C 的下边缘对齐。
基本上,我希望它位于标题下,与列表元素对齐(图像已编辑):
我有以下代码:
#header{
padding: 0.125em;
background: #eee; /* Styling for clarity */
}
nav{
display: inline; /* Makes nav inline */
}
h1{
display: inline; /* Makes the main title inline */
}
#header ul{
padding:0; /* Clears the space around the list */
display: inline; /* Makes lists inline */
}
#header li{
display: inline; /* Makes list items inline */
list-style-type: none; /* Removes bullets */
}
#header ul>li>.submenu>li{
display: block; /* Makes submenu list items behave normally */
}
#header ul>li>.submenu{
display: none; /* Hides the submenu */
position: absolute; /* Enables positioning */
top: 100%; /* Position directly under - SHOULD BE UNDER HEADER */
left: 0; /* Align left edge - SHOULD BE ALIGNED WITH ANCESTOR LIST ITEM */
background: #ccc; /* Styling for clarity */
}
#header ul>li:hover>.submenu{
display: block; /* Shows the submenu */
}
<header id="header">
<nav>
<ul>
<li>
<a>
<h1>
<img>TITLE
</h1>
</a>
</li>
<li>
<a>PAGE</a>
</li>
<li>
<a>PAGE</a>
<ul class="submenu">
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
</ul>
</li>
<li>
<a>PAGE</a>
</li>
</ul>
</nav>
</header>
现在,要使定位有所作为,我必须选择要对其进行定位的元素。但是,如果我用列表元素来做,子菜单会太靠上,因为它位于列表元素下方,并且标题有一些填充。
#header li {
position: relative; /* Positioning will be relative to the ANCESTOR LIST ITEM */
}
#header{
padding: 0.125em;
background: #eee; /* Styling for clarity */
}
nav{
display: inline; /* Makes nav inline */
}
h1{
display: inline; /* Makes the main title inline */
}
#header ul{
padding:0; /* Clears the space around the list */
display: inline; /* Makes lists inline */
}
#header li{
display: inline; /* Makes list items inline */
list-style-type: none; /* Removes bullets */
position: relative; /* Positioning will be relative to the ANCESTOR LIST ITEM */
}
#header ul>li>.submenu>li{
display: block; /* Makes submenu list items behave normally */
}
#header ul>li>.submenu{
display: none; /* Hides the submenu */
position: absolute; /* Enables positioning */
top: 100%; /* Position directly under - SHOULD BE UNDER HEADER */
left: 0; /* Align left edge - SHOULD BE ALIGNED WITH ANCESTOR LIST ITEM */
background: #ccc; /* Styling for clarity */
}
#header ul>li:hover>.submenu{
display: block; /* Shows the submenu */
}
<header id="header">
<nav>
<ul>
<li>
<a>
<h1>
<img>TITLE
</h1>
</a>
</li>
<li>
<a>PAGE</a>
</li>
<li>
<a>PAGE</a>
<ul class="submenu">
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
</ul>
</li>
<li>
<a>PAGE</a>
</li>
</ul>
</nav>
</header>
这是结果:与列表元素对齐
如果我用标题来做,子菜单将与整个标题的左侧对齐,这不是我想要的。
#header {
position: relative; /* Positioning will be relative to the HEADER */
}
#header{
padding: 0.125em;
background: #eee; /* Styling for clarity */
position: relative; /* Positioning will be relative to the HEADER */
}
nav{
display: inline; /* Makes nav inline */
}
h1{
display: inline; /* Makes the main title inline */
}
#header ul{
padding:0; /* Clears the space around the list */
display: inline; /* Makes lists inline */
}
#header li{
display: inline; /* Makes list items inline */
list-style-type: none; /* Removes bullets */
}
#header ul>li>.submenu>li{
display: block; /* Makes submenu list items behave normally */
}
#header ul>li>.submenu{
display: none; /* Hides the submenu */
position: absolute; /* Enables positioning */
top: 100%; /* Position directly under - SHOULD BE UNDER HEADER */
left: 0; /* Align left edge - SHOULD BE ALIGNED WITH ANCESTOR LIST ITEM */
background: #ccc; /* Styling for clarity */
}
#header ul>li:hover>.submenu{
display: block; /* Shows the submenu */
}
<header id="header">
<nav>
<ul>
<li>
<a>
<h1>
<img>TITLE
</h1>
</a>
</li>
<li>
<a>PAGE</a>
</li>
<li>
<a>PAGE</a>
<ul class="submenu">
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
<li><a>PAGE</a></li>
</ul>
</li>
<li>
<a>PAGE</a>
</li>
</ul>
</nav>
</header>
这是它的样子:在标题下
所以,我想要的是“top: 100%;”与整个标题和“left: 0;”相关与子菜单是子菜单的列表元素相关。
我希望我的问题很清楚。我找不到以前问过这个问题的人。很抱歉,我无法在问题中放置图片或更多链接,因为这是我的第一个问题,所以我没有足够的声誉。
感谢阅读我的问题,希望能尽快得到解答!
最佳答案
这是 fiddle :http://jsfiddle.net/td6Las4x/
只需为“#header”添加“position:relative”,为“#header ul > li > .submenu”和“display: inline-block;”移除“left:0”对于“#header li”
#header li {
display: inline-block;
}
关于html - CSS Position 元素相对于其他两个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30547857/
我刚开始使用 html5 和 css,我对使用 css position 有点困惑。 我可以从堆栈溢出中找到这些链接, Difference between relative and absolute
我想知道是否有一种方法可以获得相对定位的元素的“绝对位置”。我想不出任何方法,但也许有人想出了一个方法...... 最佳答案 我不确定“绝对位置”是什么意思,但您可以使用 el.getBounding
基本上我的 HTML(使用 Bootstrap 类)中有一个页面,其中包含使用 thumbnail 和 caption 类展示的产品。在产品图片上方有一个 ON SELL(圆形红色标签)。我给这个红色
当我使用 BaseAdapter 的 ListView 离开屏幕时,每一行不再保持连续的位置。除了这个我不知道还能怎么解释。 如果我的 BA/LV 在屏幕上显示 4 个项目,并且我添加了一个显示每一行
此 HTML 无法在 IE8 中正确呈现。它适用于 Firefox、Opera、IE7、IE9 和 IE10。 (当然,它在 IE6 中也不起作用,因为它涉及固定位置的元素。) 实际上它在 Chrom
我正在使用Position Absolute的表单验证引擎。 我有一个特定的验证案例,希望可以为我解决。我需要能够根据另一个字段的值来使一个字段成为必需。 例如: If country.dropdow
我看过很多帖子,人们希望将相机位置调整到屏幕位置。我的问题是如何做相反的事情。 我目前想要实现的是将“门”位置设置为屏幕的百分比,此计算已准备就绪,并且我确实有最终屏幕的 X、Y (px) 位置。相机
谁能解释一下使用 position:relative、position:absolute 和 float 对正常文档流及其子项的影响的区别? 例如当我有三个元素 A、B、C 时,其中 A 是包含 B
我有以下代码: .tabs{ position: fixed; top:110px; left:0px; width:40px; z-inde
我不知道如何表达这个问题。 所以这是一个例子。 图层上的动画滑入 View ,然后稍微弹回。 十分简单。 位置关键帧看起来像这样: 关键帧 0:(-100,200) 关键帧 10:(100,200)
我是 XSLT 的新手,我正在使用 w3schools 的“自己尝试”页面进行一些测试。我正在使用以下演示: http://www.w3schools.com/xsl/tryxslt.asp?xmlf
我试图让一个对象 1 (SKSpriteNode) 在某个高度(y 坐标)处创建另一个对象 (object2)。我已经得到了我需要的所有代码,除了我不知道如何正确编写: object1.positio
.special p { display:none; } .special:hover p { display:block; } Things
当将一个绝对定位元素放置在一个位置固定包装器中时,在一个位置相对包装器中,绝对元素位置不应该相对于相对 parent 而不是固定 parent ? Content
我有一个 iframe,它在应用 css 后显示谷歌地图 position: relative; top: 48px; left: -233px; 但是当我向下滚动时,它刚好出现在显示 khaleej
我在一个网站上工作,有一个带有 position:relative 的父元素,它有一个带有 position:absolute 的子元素。令我感到奇怪的是,对于我提到的位置,子元素似乎仍然可以识别它们
我有一个 main 元素,我已将其设置为 position: relative。这包含两个 div,然后我在其上应用 position: absolute。然后,这会导致夹在 main 元素中间的页眉
HTML 代码由一个 div 组成,它具有两种类型的类:“隐藏”和“保留”。 Lorem ipsum dolor sit amet, consectetur adipis
我必须开发一个生成表格(有点……)的软件,并且必须提供对元素位置的完全控制。 到目前为止,我们有这样的构造: 还有一些javascript代码来放置#a , #b和 #c #co
我尝试将 z-index 设置为 body 下的某些位置为 fixed 的元素 示例如下: HTML menu content ....
我是一名优秀的程序员,十分优秀!