导航栏元素不在单独的行上

时间:2017-10-29 15:36:31

标签: javascript html css

我正在尝试构建导航栏并遇到一些问题:

  1. 我无法打造品牌&口号课分开单行
  2. 导航栏中的“下拉ini”和“提示”标签之间存在微小的烦恼(当宽度大于768px时)
  3. 使用break标记甚至不能修复问题1. none的Flex-wrap也会失败。问题2不是空白问题,因此没有理由存在未知差距。非常感谢任何建议,谢谢。

    function myFunction() {
                var x = document.getElementsByClassName("dropdown-content");
                if (x.className === "dropdown-content") {
                    x.className += " responsive";
                } else {
                    x.className = "dropdown-content";
                }
            }
    body { margin: 0; }
    
    #navbar {
        width: 100%;
        height: 100px;
        background: green;
        padding: 0% 5%;
        box-sizing: border-box;
    }
    #navbar img {
        width: auto;
        height: 80%;
        float: left;
    }
    #navbar a:hover {
        background: red;
    }
    #branding {
        float: left;
        background: yellow;
        height: 100%;
        width: auto;
        display: flex;
    	justify-content: center;
    	align-items: center;
    }
    .brand {}
    .slogan {}
    .navlinks {
        float: right;
        height: 100%;
        background: pink;
        display: flex;
    	justify-content: center;
    	align-items: center;
        padding: 0 15px;
    }
    .dropdown {
        float: right;
        overflow: hidden;
    }
    .dropdown .dropbtn {
        font-size: 16px;    
        border: none;
        outline: none;
        color: white;
        padding: 14px 15px;
        background-color: red;
        height: 100px; /* PREFER 100% BUT DOESNT WORK */
        display: block;
    }
    .dropdown:hover .dropbtn {
        background: yellow;
    }
    .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;
    }
    .dropdown-content a {
        float: none;
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        text-align: left;
    }
    .dropdown-content a:hover {
        background-color: #ddd;
    }
    .dropdown:hover .dropdown-content {
        display: block;
    }
    
    
    
    
    #menu {
        display: none;
    }
    .responsive {
        display: block;
    }
    
    @media handheld, screen and (max-width: 768px) {
        .navlinks, .dropdown { display: none; }
        #menu {
        display: grid;
    }
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    
    <div id="navbar">
            <a href="#"><img src="assets/img/logo.png"></a>
            <div id="branding">
                <a href="#" class="brand">brand name</a>
                <a href="#" class="slogan">text slogan goes here</a>
            </div>
            <a href="javascript:void(0);" class="navlinks" id="menu" onclick="myFunction()">&#9776;</a>
            <a href="#" class="navlinks">Tips</a>
            <div class="dropdown">
                <button class="dropbtn">dropdown ini 
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">first choice</a>
                    <a href="#">second choice</a>
                </div>
            </div>
        </div>

1 个答案:

答案 0 :(得分:0)

1

 #branding {
    float: left;
    background: yellow;
    height: 100%;
    width: auto;
    display: flex;
    flex-direction: column; //Add Flex-Direction
    justify-content: center;
    align-items: center;
};
  1. 添加保证金:0到您的按钮。 Safari必须具有一些默认属性;