gpt4 book ai didi

html - 调整下拉菜单大小以匹配链接宽度

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:42 24 4
gpt4 key购买 nike

我想调整下拉菜单的大小以匹配链接宽度。这是我目前拥有的图片: enter image description here

以下是我的 HTML 页面的相关部分:

    <body>
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
<a href="why_writing.html">Why Write?</a>
<a href="dollops.html">Dollops</a>
<a href="longforms.html">Longforms</a>
<a href="technical_science.html">Technical/Science</a>
<a href="quotes.html">Quotes</a>
<a href="words.html">Words</a>
<a href="notes.html">Notes</a>
</div>
</div>
</nav>
</div>

以下是我的 CSS 样式表中最相关的部分:

/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
overflow: hidden;
background-color: #272424;
font-family: Arial;
}

/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}

/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
width: 25%;
}

/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}

/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}

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

/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}

/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}

我尝试将 width: 100% 添加到 .dropdown-content 但宽度太大并且超出了右侧的书写链接。

编辑:作为一项额外功能,我希望我的链接显示为“框”,它们之间没有任何空格连接,而不是让它们显示为一条连续的黑色带。我该怎么做?

最佳答案

我对css做了一些修改。

/* Navbar container */
*{
box-sizing: border-box;
}
.navbar {
background-color: #272424;
font-family: Arial;
height:50px
}

/* Links inside the navbar */
.navbar a {
float: left;
font: Arial;
font-size: 20px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width: 25%;
}

/* The dropdown container */
.dropdown {
float: left;
width: 25%;
position: relative;
}

/* Dropdown button */
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit; /* Important for vertical align on mobile phones */
margin: 0; /* Important for vertical align on mobile phones */
width: 100%;
}

/* Add a red background color to navbar links on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #81A3A7;
}

/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 100%;
}

/* Links inside the dropdown */
.navbar .dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width:auto;
}

/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navbar">
<nav>
<a href=index.html>About</a>
<a href=projects.html>Projects</a>
<a href=publications.html>Publications</a>
<div class="dropdown">
<button class="dropbtn">Writing
</button>
<div class="dropdown-content">
<a href="why_writing.html">Why Write?</a>
<a href="dollops.html">Dollops</a>
<a href="longforms.html">Longforms</a>
<a href="technical_science.html">Technical/Science</a>
<a href="quotes.html">Quotes</a>
<a href="words.html">Words</a>
<a href="notes.html">Notes</a>
</div>
</div>
</nav>
</div>

关于html - 调整下拉菜单大小以匹配链接宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270572/

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