调整屏幕大小时,HTML导航栏下拉框内容无法正确调整大小

时间:2019-03-28 11:44:18

标签: html css

在我的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,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,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时,它变得比框宽。

2 个答案:

答案 0 :(得分:2)

如下所示修改媒体查询

.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,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,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,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 (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>

答案 1 :(得分:0)

符合您要求的PFB代码

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style type="text/css">
                        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;
                width: 24% !important;
            }

            .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,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,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%;
                }
            }
        </style>
    </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>