同一行上的CSS / HTML导航和徽标

时间:2015-06-03 15:39:33

标签: html css navigation

我无法弄清楚如何将它们放在同一条线上。

http://codepen.io/anon/pen/dovZdQ

<body>
    <div class="navigation-bar">
        <div id="navigation-container">
            <img src="logo.png"> 
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Projects</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Get in Touch</a></li>
            </ul>
        </div>
    </div>
</body>

6 个答案:

答案 0 :(得分:9)

默认情况下,<ul>是一个块元素,而是改为inline-block

.navigation-bar ul {
  padding: 0px;
  margin: 0px;
  text-align: center;
  display:inline-block;
  vertical-align:top;
}

CodePen Demo

答案 1 :(得分:3)

首先,让我们使用一些语义HTML。

<nav class="navigation-bar">
    <img class="logo" src="logo.png">
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Projects</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Get in Touch</a></li>
    </ul>
</nav>

事实上,你甚至可以摆脱更多极简主义者:

<nav class="navigation-bar">
    <img class="logo" src="logo.png">
    <a href="#">Home</a>
    <a href="#">Projects</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Get in Touch</a>
</nav>

然后添加一些CSS:

.navigation-bar {
    width: 100%;  /* i'm assuming full width */
    height: 80px; /* change it to desired width */
    background-color: red; /* change to desired color */
}
.logo {
    display: inline-block;
    vertical-align: top;
    width: 50px;
    height: 50px;
    margin-right: 20px;
    margin-top: 15px;    /* if you want it vertically middle of the navbar. */
}
.navigation-bar > a {
    display: inline-block;
    vertical-align: top;
    margin-right: 20px;
    height: 80px;        /* if you want it to take the full height of the bar */
    line-height: 80px;    /* if you want it vertically middle of the navbar */
}

显然,实际的边距,高度和线高等取决于您的设计。

其他选项是使用表格或浮点数进行布局,但这些通常不赞成。

最后但同样重要的是,我希望你能解决这个问题。

答案 2 :(得分:0)

您需要将logo类应用于图像...然后浮动ul

Codepen Demo

<强> HTML

<img class="logo" src="http://i.imgur.com/hCrQkJi.png">

<强> CSS

.navigation-bar ul {
  padding: 0px;
  margin: 0px;
  text-align: center;
  float: left;
  background: white;
}

答案 3 :(得分:0)

试试这个CSS:

body {
  margin: 0;
  padding: 0;
}

.logo {
  float: left;
}
/* ~~ Top Navigation Bar ~~ */

#navigation-container {
  width: 1200px;
  margin: 0 auto;
  height: 70px;
}

.navigation-bar {
  background-color: #352d2f;
  height: 70px;
  width: 100%;
}

#navigation-container img {
  float: left;
}

#navigation-container ul {
  padding: 0px;
  margin: 0px;
  text-align: center;
  display:inline-block;
}

#navigation-container li {
  list-style-type: none;
  padding: 0px;
  height: 24px;
  margin-top: 4px;
  margin-bottom: 4px;
  display: inline;
}

#navigation-container li a {
  color: white;
  font-size: 16px;
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  text-decoration: none;
  line-height: 70px;
  padding: 5px 15px;
  opacity: 0.7;
}

#menu {
  float: right;
}

答案 4 :(得分:0)

我建议你使用 CSS Flex。

body {
    margin: 0;
    padding: 0;
}

#navigation-container {
    position: relative;
    background-color: #352d2f;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}

ul {
    display: flex;
    flex-direction: row;
    list-style-type: none;
}

a {
    text-decoration: none;
    color: black;
    font-size: 16px;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    padding: 5px 15px;
    opacity: 0.7;
}

li {
    display: flex;
    flex-direction: row;
    align-items: center;
}
 <head>
        <link href="./answer.css" type="text/css" rel="stylesheet">
    </head>
    <body>

        <div class="navigation-bar">
      
      
          <div id="navigation-container">
      
            <img src="https://i.imgur.com/hCrQkJi.png">
      
            <ul>
              <li><a href="#">Home</a></li>
              <li><a href="#">Projects</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Services</a></li>
              <li><a href="#">Get in Touch</a></li>
            </ul>
          </div>
      
      </body>

答案 5 :(得分:-4)

1)您可以将图像浮动到左侧:

<img style="float:left" src="http://i.imgur.com/hCrQkJi.png">

2)您可以使用HTML表格将元素放在一行上。

以下代码

  <div class="navigation-bar">
    <div id="navigation-container">
      <table>
        <tr>
          <td><img src="http://i.imgur.com/hCrQkJi.png"></td>
          <td><ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Projects</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Get in Touch</a></li>
        </ul>
          </td></tr></table>
    </div>