在导航栏中居中显示下拉菜单
P粉285587590
P粉285587590 2024-04-04 18:24:52
0
1
345

我制作了这个 CSS/HTML。

    
.navbar {
  width: 100%;
  background-color: rgb(151, 44, 44);
  overflow: auto;
}

.navbar a {
  border-right:1px solid #f5b7b7;
  float: left;
  padding: 8px;
  color: white;
  text-decoration: none;
  font-size: 1.5vh;
  width: 25%; /* Four links of equal widths */
  text-align: center;
}

.navbar a:hover {
  background-color: rgb(92, 25, 25);
}

.navbar a.active {
  background-color: #04AA6D;
}

/* ************************************************************** */

/* DROPDOWN */


.dropdown {
  float: left; 
  overflow: hidden;
  
}


.dropdown .dropbtn {
  font-size: 1.5vh;  
  border: none;
  outline: none;
  color: white;
  padding: 8px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


.dropdown:hover .dropbtn {
  background-color:  rgb(92, 25, 25);
}

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

.dropdown-content a {
  float: none;
  color: rgb(0, 0, 0);
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
  width: 100%;
}

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

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

    
<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    
</head>

<body>

    <div class="navbar" id="tree">
        <a  href= "/">Home1</a> 
        <a  href= "/">Home2</a> 
        <a  href= "/">Home3</a> 
        
        <div class="dropdown">
          <button class="dropbtn" >&nbsp;TOPICS &nbsp; ▼&nbsp;</button>
          <div class="dropdown-content">
            <a href="#">t1</a>
            <a href="#">t2</a>
            <a href="#">t3</a>
          </div>
      </div>
</body>

</html>

但是有两个问题。

问题 1 - 无法将下拉菜单居中。使用下面的代码它居中,但在不同的分辨率和设备等上创建滚动条。

.dropdown {
  float: left; 
  overflow: hidden;
  position: relative;
  transform: translate(150%,0%)
}

问题 2 - 下拉按钮不会扩展到其整个 25% 区域(其他按钮也是如此)。即.. 悬停仅在文本“TOPIC V”上有效

任何想法为什么会发生..自 2 天以来无法解决;[

P粉285587590
P粉285587590

全部回复(1)
P粉460377540

对于第一个问题,使用 flexbox 来对齐项目。

.navbar {
  ...
  display: flex;
}
.dropdown {
  ...
  width: 25%;
  text-align: center;
}

对于第二个问题,需要将宽度设置为100%。检查下面的代码片段以了解更多详细信息。

.dropdown .dropbtn {
  ...
  width: 100%;
}

    
.navbar {
  width: 100%;
  background-color: rgb(151, 44, 44);
  overflow: auto;
  display: flex;
}

.navbar a {
  border-right:1px solid #f5b7b7;
  float: left;
  padding: 8px;
  color: white;
  text-decoration: none;
  font-size: 1.5vh;
  width: 25%; /* Four links of equal widths */
  text-align: center;
}

.navbar a:hover {
  background-color: rgb(92, 25, 25);
}

.navbar a.active {
  background-color: #04AA6D;
}

/* ************************************************************** */

/* DROPDOWN */


.dropdown {
  float: left; 
  overflow: hidden;
  width: 25%;
  text-align: center;
}


.dropdown .dropbtn {
  font-size: 1.5vh;  
  border: none;
  outline: none;
  color: white;
  padding: 8px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  width: 100%;
}


.dropdown:hover .dropbtn {
  background-color:  rgb(92, 25, 25);
}

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

.dropdown-content a {
  float: none;
  color: rgb(0, 0, 0);
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
  width: 100%;
}

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

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

    



    
    




    
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!