在我的CSS代码中,导航栏框会在屏幕宽度更改时调整大小,并且在屏幕小于600px像素数量时也会调整大小.下拉框和内容的大小也像导航栏一样,但是内容的大小无法正确调整.调整屏幕大小时,下拉内容比下拉框略小,但是,当屏幕小于600px时,下拉内容将比框大.
我曾尝试使下拉内容的宽度与框的宽度相同,但是这没有影响.我尝试了所有与内容相关的类.他们都没有影响任何事情.
注意:我更改了背景颜色,以便您可以看到内容与下拉框本身的宽度不同.
body {
background-color: blue;
}
p {
color: white;
font-size: 20px;
display: inline;
padding: 20px;
font-family: arial;
}
.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(10,10,10);
top: 0;
}
.navcont {
margin: 0;
position: relative;
float: left;
}
.navcont a,.dropbtn {
margin: 0;
position: relative;
display: block;
color: white;
font-size: 20px;
text-align: center;
padding: 5vh 7vw;
text-decoration: none;
border-right: 1px solid rgb(50,50,50);
border-left: 1px solid rgb(50,50);
}
.navcont a:hover {
transition-duration: 0.3s;
background-color: rgb(30,30,30);
}
.navcont a:active {
background-color: rgb(9,194,36);
}
.sticky {
position: sticky;
position: -webkit-sticky;
top: 0;
z-index:3
}
.dropdown:hover .dropbtn {
transition-duration: 0.3s;
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
Box-shadow: 0px 8px 16px 0px rgba(0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 5vh 7.4vw;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
@media screen and (max-width: 600px) {
.navcont a {
padding: 3vh 3vw;
}
.dropdown-content {
padding: 3vh 3vw;
}
a.dropdown-content {
padding: 3vh 3vw;
width: 100%;
}
.dropbtn {
padding: 3vh 3vw;
}
div.tr_about {
width: 60%;
}
div.tl_about {
width: 37.5%;
}
}
<!doctype html>
<html>
<head>
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>
<div>
<ul class="navbar">
<li class="navcont"><a href="index.htm">Home</a></li>
<li class="navcont"><a href="about.htm">About</a></li>
<li class="navcont"><a href="purchase.htm">Purchase</a></li>
<li class="dropdown" style="float: right;">
<a href="javascript:void(0)" class="dropbtn">Contact</a>
<div class="dropdown-content">
<a href="support.htm">Support</a>
<a href="faq.htm">FAQ</a>
</div>
</li>
</ul>
</div>
</body>
</html>
如上所述,我希望下拉框内容的宽度与调整屏幕大小时下拉框的宽度相同.但是,我得到的实际结果是内容比下拉框略窄,然后,当屏幕宽度小于600px时,它变得比框宽.
最佳答案
如下修改媒体查询
.dropdown-content {
padding: 1vh 1vw;
right:10px;
}
a.dropdown-content {
padding: 1vh 1vw;
width: 98%;
}
.dropdown-content a {
padding: 5vh 2.9vw;
}
body {
background-color: blue;
}
p {
color: white;
font-size: 20px;
display: inline;
padding: 20px;
font-family: arial;
}
.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(10,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 5vh 7.4vw;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
@media (max-width:650px) {
.navcont a {
padding: 3vh 3vw;
}
.dropdown-content {
padding: 1vh 1vw;
right:10px;
}
a.dropdown-content {
padding: 1vh 1vw;
width: 98%;
}
.dropdown-content a {
padding: 5vh 6.8vw;
}
}
@media screen and (max-width: 600px) {
.navcont a {
padding: 3vh 3vw;
}
.dropdown-content {
padding: 1vh 1vw;
right:10px;
}
a.dropdown-content {
padding: 1vh 1vw;
width: 98%;
}
.dropdown-content a {
padding: 5vh 2.9vw;
}
.dropbtn {
padding: 3vh 3vw;
}
div.tr_about {
width: 60%;
}
div.tl_about {
width: 37.5%;
}
}
<body>
<div>
<ul class="navbar">
<li class="navcont"><a href="index.htm">Home</a></li>
<li class="navcont"><a href="about.htm">About</a></li>
<li class="navcont"><a href="purchase.htm">Purchase</a></li>
<li class="dropdown" style="float: right;">
<a href="javascript:void(0)" class="dropbtn">Contact</a>
<div class="dropdown-content">
<a href="support.htm">Support</a>
<a href="faq.htm">FAQ</a>
</div>
</li>
</ul>
</div>
</body>