gpt4 book ai didi

html - 具有导航栏全宽的导航栏子菜单

转载 作者:行者123 更新时间:2023-11-28 12:35:16 24 4
gpt4 key购买 nike

我正在尝试实现类似于下图的效果:

enter image description here

但是,我正在努力使子菜单对齐,并使子菜单的宽度与完整导航栏的宽度相同:

        body {
width: 1000px;
margin: 0 auto;
overflow-x: hidden;
}

.item {
float: left;
position: relative;
}

.item a {
padding: 10px;
border: 1px black solid;
margin: 1px;
display: block;
width: 100px;
}

.item .all-subs {
display: none;
position: absolute;
background: red;
width:100%;
}

.item .sub-items {
float: left;
margin: 0 1%;
width: 30%;
}
.item:hover {
background-color: green;
}

.item:hover > .all-subs {
display: block;
}
<body>

<div class="nav">
<div class="item">
<a href="#">Item 1</a>
<div class="all-subs">
<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
</div>
<div class="item">
<a href="#">Item 2</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
<div class="item">
<a href="#">Item 3</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>

</div>
</div>
<div class="item">
<a href="#">Item 4</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
</div>

</div>
<div class="item">
<a href="#">Item 4</a>

<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
<div class="item">
<a href="#">Item 5</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
</body>

子菜单的宽度设置为 100%,它继承了父 div 的宽度(如预期的那样)。此外,子菜单的对齐方式与父级 div 对齐。有什么方法可以用 CSS 改变这种行为吗?

我希望这将使我的导航栏响应(在一定程度上 - 平板电脑和手机需要改变)

最佳答案

相对定位你的 .nav 并给它一个 width,从 .item 中删除 position: relative 所以.all-subs 占据了 .nav 的完整 width

Demo on CodePen

body {
width: 1000px;
margin: 0 auto;
overflow-x: hidden;
}
.nav {
position: relative;
width: 744px;
}
.item {
float: left;
}
.item a {
padding: 10px;
border: 1px black solid;
margin: 1px;
display: block;
width: 100px;
}
.item .all-subs {
display: none;
position: absolute;
background: red;
width: 100%;
left: 0;
}
.item .sub-items {
float: left;
margin: 0 1%;
width: 30%;
}
.item:hover {
background-color: green;
}
.item:hover > .all-subs {
display: block;
}
<body>
<div class="nav">
<div class="item">
<a href="#">Item 1</a>
<div class="all-subs">
<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
<div class="item">
<a href="#">Item 2</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
<div class="item">
<a href="#">Item 3</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>

</div>
</div>
<div class="item">
<a href="#">Item 4</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
</div>

</div>
<div class="item">
<a href="#">Item 4</a>

<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>

</div>
</div>
<div class="item">
<a href="#">Item 5</a>
<div class="all-subs">

<div class="sub-items">
<h2>Sub Item List 1</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
<div class="sub-item">
Sub 3
</div>
<div class="sub-item">
Sub 4
</div>
<div class="sub-item">
Sub 5
</div>
<div class="sub-item">
Sub 6
</div>
</div>
<div class="sub-items">
<h2>Sub Item List 2</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>

</div>
<div class="sub-items">
<h2>Sub Item List 3</h2>
<div class="sub-item">
Sub 1
</div>
<div class="sub-item">
Sub 2
</div>
</div>
</div>
</div>
</div>
</body>

关于html - 具有导航栏全宽的导航栏子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818527/

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