绝对定位元素 - 它们可以位于相对定位的兄弟元素或父元素下方吗
我对绝对定位的子菜单有疑问。你可以在这里看到它 -
我想要主菜单的黄色与子菜单的红色重叠。
理想情况下,我希望在不以任何方式更改 HTML 的情况下实现这一目标。我也许能够添加课程,但这意味着与一些我并不真正认识的人发生激烈的争论。
这可能吗?
I'm having an issue with an absolutely positioned sub-menu. You can see it here -
I'd like the Yellow of the main menu to overlap the red of the sub-menu.
Ideally, I'd like to achieve this without changing the HTML in any way. I may be able to add classes, but it will mean having a massive argument with some people I don't really know.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,绝对定位元素在页面容器内设置,除非其父元素具有
position
属性的显式值,无论该值是什么。因此,在您的示例中,您所需要做的就是将
position:relative;
添加到.main_nav
中,这应该就是全部了。更改后,
.main_nav
样式应如下所示:如果问题的其他部分尚未实现,请对此答案添加注释。
更新:
根据您的第一条评论,您希望重叠部分在红色上方是黄色,而不是在右上方是红色(如果不是,请纠正我)。
这可以通过相同的修复来实现,但应用于锚点本身
.main_nav a
。请注意下面的最后两个属性:和以前一样,请在评论中根据您的要求讨论这部分。
Absolutely positioned elements are set within the page container by default, unless their parent has an explicit value for
position
property, regardless of what value it is.So, in your example, all you need to do is add
position:relative;
to.main_nav
, and that should be all.After change, the
.main_nav
style should look like:Please add a comment to this answer if there is another part to the question not achieved yet.
Update:
As per your first comment, you want the overlapped part to be yellow on top of red instead of red on top of right (please correct me if otherwise).
This can be achieved with the same fix, but applied to the anchor itself
.main_nav a
. Note the last two properties below:As before, please discuss this part against your requirement in the comments.