首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下拉菜单不对齐(纯CSS)

问下拉菜单不对齐(纯CSS)
EN

Stack Overflow用户
提问于 2020-10-09 13:40:03
回答 3查看 39关注 0票数 1

无法将下拉菜单与“服务”链接对齐。每当我悬停到“服务”链接时,它就会停留在左侧。我曾尝试在nav ul ul上使用函数"left“,它在某种程度上起到了作用,但每当我最小化窗口时,它都没有响应。它只适用于我更改的窗口大小。在这一点上,我对这一点感到困惑。

​

​

代码语言:javascript
复制
/* Basic */
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
     body{  
      font-family: 'Acme', sans-serif;
      background: white;  
      line-height: 1.6;
     }
     ul{   
       list-style: none;
     }
    
     a{
       text-decoration: none;
     }
    
     /* Navbar */
     .navbar{
         display: flex;
         background: #004C66;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         position: fixed;
    
         padding: 0 30px;
     }
    /* To Keep Control of the Logo */
     .navbar .logo img{
         width: 100%;
         margin-top: 10px;
     }
    
     /* To Make the Desktop Nav Buttons Inline */
     .navbar ul li{
        display: inline;
     }
    
     .navbar ul li .nav-link{
         padding: 10px 25px;
         color: #fff;
         font-size: 30px;
         transition-duration: 500ms;
     }
    
     .navbar .nav-link:hover{
         color: black;
     }
    
     /* NAVBAR DROP DOWNS */
    
        /* Hide Dropdowns By Default */
        nav ul ul {
            display: none;
            position: absolute; 
            top: auto; /* the height of the main nav */
            text-align: center;
            justify-items: center;
            border-radius: 10%;
        }
        
        /* Display Dropdowns on Hover */
        nav ul li:hover > ul {
            display:flex;
            background-color: teal;
            flex-direction: column;
        }
    
        /* Fisrt Tier Dropdown */
        nav ul ul li {
            width:auto;
            top: 0px;
            float:none;
            display:list-item;
            position: relative;
        }
    
        /* Second, Third and more Tiers */
        nav ul ul ul li {
            position: relative; 
        }
代码语言:javascript
复制
<header class="navbar">
    <a href="" class="logo"><img src="/img/logoS.png" alt=""></a>

        <nav class="desktop-view">
            <ul>
                <li><a class="nav-link" href="#">Home</a></li>
                
                <li><a class="nav-link services" href="#">Services</a>
                <!-- First Tier Drop Down -->
                    <ul>
                        <li><a class="nav-link" href="#">Tile</a></li>
                        <li><a class="nav-link"href="#">Mexican Tile</a></li>
                        <li><a class="nav-link"href="#">Marble Floor</a></li>
                        <li><a class="nav-link"href="#">Shower Regrout</a></li>
                    </ul>
                </li>

                <li><a class="nav-link" href="#">Contact</a></li>
            </ul>
        </nav>
</header>

​

EN

回答 3

Stack Overflow用户

发布于 2020-10-09 15:00:24

将.navbar ul li position设置为relative

然后使用left: 50%和transform: translateX(-50%)将下拉菜单移到中间

​

代码语言:javascript
复制
/* Basic */
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
     body{  
      font-family: 'Acme', sans-serif;
      background: white;  
      line-height: 1.6;
     }
     ul{   
       list-style: none;
     }
    
     a{
       text-decoration: none;
     }
    
     /* Navbar */
     .navbar{
         display: flex;
         background: #004C66;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         position: fixed;
    
         padding: 0 30px;
     }
    /* To Keep Control of the Logo */
     .navbar .logo img{
         width: 100%;
         margin-top: 10px;
     }
    
     /* To Make the Desktop Nav Buttons Inline */
     .navbar ul li{
        display: inline;
        position: relative;
     }
    
     .navbar ul li .nav-link{
         padding: 10px 25px;
         color: #fff;
         font-size: 30px;
         transition-duration: 500ms;
     }
    
     .navbar .nav-link:hover{
         color: black;
     }
    
     /* NAVBAR DROP DOWNS */
    
        /* Hide Dropdowns By Default */
        nav ul ul {
            display: none;
            position: absolute; 
            text-align: center;
            border-radius: 10%;
            left: 50%;
            width: 300px;
            transform: translateX(-50%);
        }
        
        /* Display Dropdowns on Hover */
        nav ul li:hover > ul {
            display:flex;
            background-color: teal;
            flex-direction: column;
        }
    
        /* Fisrt Tier Dropdown */
        nav ul ul li {
            width:auto;
            top: 0px;
            float:none;
            display:list-item;
            position: relative;
        }
    
        /* Second, Third and more Tiers */
        nav ul ul ul li {
            position: relative; 
        }
代码语言:javascript
复制
<header class="navbar">
    <a href="" class="logo"><img src="/img/logoS.png" alt=""></a>

        <nav class="desktop-view">
            <ul>
                <li><a class="nav-link" href="#">Home</a></li>
                
                <li><a class="nav-link services" href="#">Services</a>
                <!-- First Tier Drop Down -->
                    <ul>
                        <li><a class="nav-link" href="#">Tile</a></li>
                        <li><a class="nav-link"href="#">Mexican Tile</a></li>
                        <li><a class="nav-link"href="#">Marble Floor</a></li>
                        <li><a class="nav-link"href="#">Shower Regrout</a></li>
                    </ul>
                </li>

                <li><a class="nav-link" href="#">Contact</a></li>
            </ul>
        </nav>
</header>

​

票数 2
EN

Stack Overflow用户

发布于 2020-10-09 14:36:47

绝对定位的元素相对于第一个定位的父元素进行定位,因此具有position: absolute的导航ul ul将相对于返回的具有位置设置的第一个元素进行定位。

在本例中,要求下拉列表与其父li对齐,因此将该元素设置为position: relative

票数 0
EN

Stack Overflow用户

发布于 2020-10-09 15:36:14

screenshot of menu bar

如果你想要下拉菜单的这种外观,那么在你的CSS代码中进行以下更改:

代码语言:javascript
复制
ul{   
       list-style: none;
       display: flex;
       position: relative;
       
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64274492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档