我一直在努力解决这个问题,但无法弄清楚我错过了什么。我想下拉列表显示在其父级下方。列表显示但是所有列表都向左移动并强制剩余的导航项目下拉到另一行。我已经为目前的内容和what I want it to look like的图片添加了一个代码段。此外,我一直试图弄清楚如何使用下拉列表将fa fa-caret添加到导航项目。请帮忙!
.navbar {
background: linear-gradient(#9E0A0C, #EBEBEB);
padding: 10px;
border-top: solid 1px #000000;
overflow: hidden;
}
.navbar a {
text-decoration: none;
color: #ffffff;
font-weight: bolder;
font-size: 20px;
text-transform: uppercase;
padding: 3px 5px;
margin: auto;
/*float: left; may need to be removed to show borders*/
display: inline;
}
.navbar ul {
padding: 0;
margin-top: 0;
width: auto;
}
.navbar li {
border-left: solid 2px #000000;
display: inline;
list-style-type: none;
width: 800px;
padding: 0;
}
.navbar a:active {
background-color: #000000;
}
.navbar a:hover {
background-color: #ddd;
color: black;
font-size: 20px;
}
li:first-child {
border-left: none;
}
.dropdown {
display: block;
overflow: hidden;
/*float: left;*/
}
.list {
display: none;
min-width: 50px;
max-width: 150px;
z-index: 2;
box-shadow: 0 8px 16px 0 #e7e7e7;
background: #050243;
}
.list a {
color: #000000;
float: none;
font-size: 14px;
text-decoration: none;
display: block;
text-align: left;
background: #B6B6B6;
columns: 2;
}
.list a:hover {
background-color: #EEEEEE;
}
.dropdown:hover .list {
display: block;
}
form {
float: right;
padding: 0px;
margin: 0;
overflow: auto;
}
input {
border: solid 1px #000000;
background-image: url(pics/search.png);
background-repeat: no-repeat;
background-position: center right;
background-size: 15px;
border-radius: 5px;
padding: 5px 2px;
width: 250px;
}

<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li class="dropdown"><a href="#" class="dropbtn">Our Lodge</a>
<div class="list">
<a href="#">NEWS</a>
<a href="#">FACILITIES</a>
<a href="#">OFFICERS</a>
<a href="#">GUEST BOOK</a>
</div>
</li>
<li class="dropdown"><a href="#" class="dropbtn">Events</a>
<div class="list">
<a href="#">CALENDAR</a>
<a href="#">BINGO</a>
</div>
</li>
<li class="dropdown"><a href="#" class="dropbtn">Contact Us</a>
<div class="list">
<a href="#">BECOME AN ELK</a>
</div>
</li>
<form action="#">
<input type="text" placeholder="Search.." name="search">
</form>
</ul>
</div>
&#13;
答案 0 :(得分:0)
您可以通过首先将div.list元素设置为绝对定位来实现此目的。 'display:block'属性将所有其他内容推送到下一行,因为块元素占用了空间。
如您所见,列表不再向下推送内容,但现在它未与正确的导航项对齐。我们还要添加一个左:0,以便div.list位于父属性的左侧。
现在,父属性需要一个相对位置,以便子元素上的left:0相对于父元素定位。
.dropdown:hover .list {
display: block;
position: absolute;
left: 0;
}
.navbar li {
border-left: solid 2px #000000;
display: inline;
list-style-type: none;
width: 800px;
padding: 0;
position: relative;
}
答案 1 :(得分:0)
您的CSS代码存在问题,您错过了将position: relative
和position: absolute
添加到.dropdown
和.list
选择器,这是创建下拉列表必须的。试试这段代码。
.navbar {
background: linear-gradient(#9E0A0C, #EBEBEB);
padding: 10px;
border-top: solid 1px #000000;
}
.navbar a {
text-decoration: none;
color: #ffffff;
font-weight: bolder;
font-size: 20px;
text-transform: uppercase;
padding: 3px 5px;
margin: auto;
/*float: left; may need to be removed to show borders*/
display: inline;
}
.navbar ul {
padding: 0;
margin-top: 0;
width: auto;
}
.navbar li {
border-left: solid 2px #000000;
display: inline;
list-style-type: none;
width: 800px;
padding: 0;
}
.navbar a:active {
background-color: #000000;
}
.navbar li:hover > a {
background-color: #ddd;
color: black;
}
li:first-child {
border-left: none;
}
.dropdown {
display: block;
overflow: hidden;
position: relative;
/*float: left;*/
}
.list {
display: none;
min-width: 50px;
max-width: 150px;
z-index: 2;
box-shadow: 0 8px 16px 0 #e7e7e7;
background: #050243;
position: absolute;
left: 0;
top: 100%;
}
.list a {
color: #000000;
float: none;
font-size: 14px;
text-decoration: none;
display: block;
text-align: left;
background-color: #B6B6B6;
columns: 2;
}
.list a:hover {
background-color: #EEEEEE;
}
.dropdown:hover .list {
display: block;
}
form {
float: right;
padding: 0px;
margin: 0;
overflow: auto;
}
input {
border: solid 1px #000000;
background-image: url(pics/search.png);
background-repeat: no-repeat;
background-position: center right;
background-size: 15px;
border-radius: 5px;
padding: 5px 2px;
width: 250px;
}
答案 2 :(得分:0)
- &GT;请更新现有文件中的以下代码
.navbar {
background: linear-gradient(#9E0A0C, #EBEBEB);
padding: 10px;
border-top: solid 1px #000000;
}
.navbar a {
text-decoration: none;
color: #ffffff;
font-weight: bolder;
font-size: 20px;
text-transform: uppercase;
padding: 3px 5px;
margin: auto;
display: inline;
}
.navbar ul {
padding: 0;
margin-top: 0;
width: auto;
}
.navbar li {
border-left: solid 2px #000000;
display: inline;
list-style-type: none;
width: 800px;
padding: 0;
position: relative;
}
.navbar a:active {
background-color: #000000;
}
.navbar a:hover {
background-color: #ddd;
color: black;
}
li:first-child {
border-left: none;
}
.dropdown {
display: block;
overflow: hidden;
}
.list {
opacity: 0;
visibility: hidden;
min-width: 50px;
max-width: 150px;
z-index: 2;
box-shadow: 0 8px 16px 0 #e7e7e7;
background: #050243;
position: absolute;
top: 20px;
left: 0;
transition: 0.3s ease-in-out;
}
.list a {
color: #000000;
float: none;
font-size: 14px;
text-decoration: none;
display: block;
text-align: left;
background: #dddddd;
columns: 2;
}
.list a:hover {
background-color: #EEEEEE;
}
.dropdown:hover .list {
opacity: 1;
visibility: visible;
}
form {
float: right;
padding: 0px;
margin: 0;
overflow: auto;
}
input {
border: solid 1px #000000;
background-image: url(pics/search.png);
background-repeat: no-repeat;
background-position: center right;
background-size: 15px;
border-radius: 5px;
padding: 5px 2px;
width: 250px;
}
&#13;
<div class="navbar">
<ul>
<li><a href="#">Home</a></li>
<li class="dropdown"><a href="#" class="dropbtn">Our Lodge</a>
<div class="list">
<a href="#">NEWS</a>
<a href="#">FACILITIES</a>
<a href="#">OFFICERS</a>
<a href="#">GUEST BOOK</a>
</div>
</li>
<li class="dropdown"><a href="#" class="dropbtn">Events</a>
<div class="list">
<a href="#">CALENDAR</a>
<a href="#">BINGO</a>
</div>
</li>
<li class="dropdown"><a href="#" class="dropbtn">Contact Us</a>
<div class="list">
<a href="#">BECOME AN ELK</a>
</div>
</li>
<form action="#">
<input type="text" placeholder="Search.." name="search">
</form>
</ul>
</div>
&#13;