中心对齐浮动的导航栏和从左到右的下拉列表

时间:2014-09-02 07:08:00

标签: html css navbar

我想在导航栏中居中对齐(以便img是导航栏的中心。我知道我有浮动:离开;但如果我不这样做,(左边的东西,正确的东西)会掉落到了img的下边缘。所以我想要的是保持浮动,但是能够显示:内联块,或者相等的东西。我还想要(左边的)下拉栏从img开始#39;左侧,然后向右侧建造 demo fiddle

HTML

  <div id="nav">
    <div id="container">
        <ul>
            <li>
               <a href="#"> Left thing </a>

                <ul>
                    <li><a href="#"><----- want it to go this way</a>
                    </li>
                    <li><a href="#">i want this to start under left thing</a>
                    </li>
                </ul>
            </li>
            <li>
                <img src="http://www.jonathanjeter.com/images/Square_200x200.png" style="height:70px" />
            </li>
            <li>
              <a href="#"> Right thing </a>

                <ul>
                    <li><a href="#">This starts right</a>
                    </li>
                    <li><a href="#">And this is right</a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
</div>

CSS

* {
    padding: 0;
    margin: 0;
}
#body {
    padding: 0;
    margin: 0;
    font-family: Arial;
    font-size: 17px;
}
#nav {
    background-color: 72776A;
    width: 100%;
    position:fixed;
    height:50px;
}
#nav ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    position: relative;
    display:block;
}
#nav ul li {
    float:left;
}
#container {
    text-align:;
}
#nav ul li:hover {
    background-color: #333;
}
#nav ul li a, visited {
    color: ACD661;
    display: block;
    padding: 15px;
    text-decoration: none;
}
#nav ul li:hover ul {
    display: block;
}
#nav ul ul {
    display: none;
    position:absolute;
    color: red;
    border: 1px solid black;
}
#nav ul ul li {
    display: block;
}
#nav ul ul li a:hover {
    color: #699;
}

2 个答案:

答案 0 :(得分:0)

替换

#container {
  width: 270px;
  margin: 0px auto;
}

检查this小提琴

答案 1 :(得分:0)

在谈论解决方案之前,让我们谈谈你的CSS,我删除了margin:0, padding:0的重复,突出显示遗忘的选择器或错误输入。您需要为容器width精确div并添加margin-leftmargin-right自动来实现您的目标:

/* {
    padding: 0;
    margin: 0;
} not the best solution, everybody don't need this rule */

/* instead here a group of selectors */

body,
#nav ul {
    padding: 0;
    margin: 0;
}
/*#*/body { /* you mean just body right ? */
    font-family: Arial;
    font-size: 17px;
}

#nav {
    background-color: #72776A; /* you forgot # here */
    width: 100%;
    position:fixed;
    height:50px; /*it's smaller than your image*/
}
#nav ul {
    list-style-type: none;
    position: relative;
    /*display:block; don't need */
}

#nav li {
    float:left;
}
#container {
    /*text-align:; seems it's missing something here ? */
    margin: 0 auto;
    width: 300px;
}

#nav ul li:hover {
    background-color: #333;
}

#nav ul li a,
#nav ul li a:hover,
#nav ul li a:visited {/ { /* you mean a:visited ? */
    color: #ACD661; /* here again # forgot */
    display: block;
    padding: 15px;
    text-decoration: none;
}
#nav li:hover ul { /*ul don't need (i mean #nav ul li...) */
    display: block;
}
#nav ul ul {
    display: none;
    position:absolute;
    color: red;
    border: 1px solid black;
}
#nav li li { /* shorter then ul ul li */
    display: block;
}
#nav a li a:hover {
    color: #699;
}

您可以看到代码here