简体   繁体   English

我无法调整导航栏css / html中的下拉菜单的大小

[英]I cant resize dropdown menu in navigation bar css/html

im trying to resize my dropdown menu in my navigation bar to 65px high but it doesnt change no matter what height i put in, originally i had used ul and li tags for my nav bar but i had to change it all to divs because w3 validator said that it wasnt exactly correct so i had to change it all to divs, here is my html and source code: 我试图将导航栏中的下拉菜单的大小调整为65px高,但是无论我放什么高度都不会改变,最初我在导航栏中使用了ul和li标签,但由于w3验证程序,我不得不将其全部更改为divs说这不是完全正确,所以我不得不将其全部更改为div,这是我的html和源代码:

<div id="nav">
<div class="container">
<a href="404.html">404found</a>
<div class="dropdown">
<button class="dropbtn">Courses</button>
<div class="dropdown-content">
<a href="nonadvancedcourses.html">Non-Advanced</a>
<a href="advancedcourses.html">Advanced</a>
</div>
</div>
<a href="projects.html">Projects</a> 
<a href="contact.html">Contact</a>
</div>
</div>



.dropdown {
float:left;
overflow:hidden;
height:65px;
width:205px;
}

.dropdown .dropbtn {
font-size:16px;    
border:none;
outline:none;
background-color:#FFF;
width:205px;
height:65px;
font-family:"Calibri Light", Arial, Helvetica, sans-serif; 
font-weight:100;
color:#444;
background:#FFF;
}

.container a:hover, .dropdown:hover .dropbtn {
background:#E9E9E9;
}

.dropdown-content {
display:none;
position:absolute;
background-color:#f9f9f9;
width:205px;
height:65px;
box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);
z-index:1;
}

.dropdown-content a {
float:left;
font-size:16px;
background:#FFF;
text-align:center;
padding:0;
margin:0;
text-decoration:none;
width:205px;
height:65px;
font-family:"Calibri Light", Arial, Helvetica, sans-serif; 
font-weight:100;
color:#444;
line-height:23px;

}

.dropdown-content a:hover {
background-color:#E9E9E9;
}

.dropdown:hover .dropdown-content {
display:block;
}

.container {
overflow:hidden;
background-color:#FFF;
font-family:Arial;
height:65px;
}

.container a {
float:left;
font-size:16px;
background:#FFF;
text-align:center;
padding-top:23px;
text-decoration:none;
width:205px;
height:65px;
font-family:"Calibri Light", Arial, Helvetica, sans-serif; 
font-weight:100;
color:#444;
}

The problem here is the content is overflowing the height you are setting, here's a couple ways of solving this. 这里的问题是内容正在溢出您设置的高度,这是解决此问题的几种方法。

Note: I've moved .container a rules to the top of the style, so they don't overwrite anything in .dropdown-content a 注意:我已经将.container a规则移至.container a的顶部,因此它们不会覆盖.dropdown-content a任何.dropdown-content a

Make things smaller 缩小尺寸

The simplest way is to resize what is overflowing. 最简单的方法是调整溢出内容的大小。 In this case the following style can simply be applied: 在这种情况下,可以简单地应用以下样式:

.dropdown-content a {
  padding-top: 4px;
  height: 28.5px;
}

codepen codepen

But this isn't a nice way of doing it, and involves alot of fiddling to center things correctly, so my own suggestion would be... 但这不是一个好的方法,并且涉及到很多摆弄正确地使事情居中的事情,所以我自己的建议是...

flex it 弯曲它

I find flex very useful for aligning anything. 我发现flex对于对齐任何内容非常有用。 Removing the padding and height's set on .dropdown-content a you can apply/replace the following styles for perfectly fitting and centered text within the 65px height. 删除.dropdown-content a上的填充和高度设置,您可以应用/替换以下样式,以在65px高度内完美地适合居中的文本。 Read more about flex here. 在此处阅读有关flex的更多信息。

.dropdown-content {
  flex-direction: column;
}

.dropdown-content a {
  display: flex;
  align-items: center;
  justify-content: center;
}

.dropdown:hover .dropdown-content {
  display: flex;
}

This way is a lot more dynamic, the height of .dropdown-content can be adjusted however you like an everything will fit accordingly. 这种方式更具动态性,可以调整.dropdown-content的高度,但是您希望所有内容都适合。

codepen codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM