【问题标题】:Creating a nav bar with flexbox使用 flexbox 创建导航栏
【发布时间】:2020-10-23 01:30:26
【问题描述】:

我只学了几个星期的代码,所以我掌握了非常基础的知识。 我在尝试使用 flexbox 构建导航栏时遇到了困难。

由于某种原因,我无法获得导航按钮(

  • ) 以水平方式站立。 我一直在尝试并重写我的代码,但我无法弄清楚。

    这是我的代码的链接:https://codepen.io/kokazp/pen/xxORovj?editors=1100

    提前致谢。

    body{
        font-family: 'Yanone Kaffeesatz', sans-serif;
        background-color: lightGray;
    }
    
    navbar {
        background: white;
        box-shadow: 0px 0px 10px var(--clr-gray200);
        padding: 1rem 0;
        border-radius: var(--radius);
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        align-items: center;
      }
    
    #header-img{
        height: 70px;
        width: 70px;
        margin-right: auto;
        margin-left: 10px;
    }
    
    ul{
        list-style: none;
        margin: 0;
        padding: 0;
    }
        
    li, a{
        background-color:white;
        text-decoration: none;
        padding: 8px;
        color:black;
    }
            
    li a:hover{
        background-color: black;
        color: white;
    }
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
            <link href="https://fonts.googleapis.com/css2?family=Yanone+Kaffeesatz:wght@300&display=swap" rel="stylesheet">
            <link rel=StyleSheet HREF="estilo.css">
        </head>
        
        <body>
    
            <header id="header">
                <navbar id="nav-bar">
                    <ul>
                        <li><img src="https://blog.spoongraphics.co.uk/wp-content/uploads/2017/craft-brewery-logos/1.jpg" id="header-img" alt="Company logo"></li>
                        <li><a href="#form">Contact</a></li>
                        <li><a href="#beers">Beers</a></li>
                        <li><a href="#video">Video</a></li>
                    </ul>
                </navbar>
            </header>
        </body>
    </html>
  • 【问题讨论】:

      标签: html css frontend navbar nav


      【解决方案1】:

      让我们明确一点,当您使用 flex-box 时,它适用于子级而不是大子级,当您将 display:flex 应用于您的导航栏时,它将使 ul 成为 flex 子级,所以 li 标签 不是弹性的,其次你要让它成为列,所以它会以垂直的方式制作。 解决方法:你可以做ul

      ul{display:flex;}
      

      它会让li默认进入行方向

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        相关资源
        最近更新 更多