将ul与img对齐

时间:2017-03-27 16:24:03

标签: html css html-lists

我正在尝试将ul中的链接与图像和文字对齐,以便我可以拥有一个好的导航栏,但出于某种原因,它会低于一切。我该如何解决这个问题?



body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}

#tagline {
  font-style: italic;
}

nav {
  background-color: white;
  -webkit-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
}

#logo {
  padding: 10px 10px 10px 10px;
  vertical-align: middle;
}

li,
a {
  text-decoration: none;
  list-style-type: none;
  color: black;
  display: inline-block;
  float: right;
}

<nav>
  <img src="https://s1.postimg.org/qiu818fhr/lb_logo.png" id="logo" alt="logo">
  <span id="tagline">Live, 1-to-1, flexible and personalized</span>
  <ul id="nav-items">
    <li><a href="#">How it Works</a></li>
    <li><a href="#">Courses</a></li>
    <li><a href="#">Teachers</a></li>
    <li><a href="#">Enroll</a></li>
    <li><a href="#">Login</a></li>
  </ul>
</nav>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

display:flex应用于nav以及为什么不从float:right删除li

body {
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

#tagline {
  font-style: italic;
}

nav {
  background-color: white;
  -webkit-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
}

#logo {
  padding: 10px;
}

ul {
  margin-left: auto;
}

li,
a {
  text-decoration: none;
  list-style-type: none;
  color: black;
  display: inline-flex;
}
<nav>
  <img src="https://s1.postimg.org/qiu818fhr/lb_logo.png" id="logo" alt="logo">
  <span id="tagline">Live, 1-to-1, flexible and personalized</span>
  <ul id="nav-items">
    <li><a href="#">How it Works</a></li>
    <li><a href="#">Courses</a></li>
    <li><a href="#">Teachers</a></li>
    <li><a href="#">Enroll</a></li>
    <li><a href="#">Login</a></li>
  </ul>
</nav>

答案 1 :(得分:0)

您希望将#nav-items列表右移,因为它是一个块元素。然后display: inline-block上的licolor上的text-decoration / a

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}

#tagline {
  font-style: italic;
}

nav {
  background-color: white;
  -webkit-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
}

#logo {
  padding: 10px 10px 10px 10px;
  vertical-align: middle;
}

#nav-items {
  float: right;
}

li {
  display: inline-block;
}

a {
  text-decoration: none;
  color: black;
}
&#13;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>LanguageBird</title>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>

  <nav>
    <img src="img/lb_logo.png" id="logo" alt="logo">
    <span id="tagline">Live, 1-to-1, flexible and personalized</span>
    <ul id="nav-items">
      <li><a href="#">How it Works</a></li>
      <li><a href="#">Courses</a></li>
      <li><a href="#">Teachers</a></li>
      <li><a href="#">Enroll</a></li>
      <li><a href="#">Login</a></li>
    </ul>
  </nav>

  <script src="main.js"></script>
</body>

</html>
&#13;
&#13;
&#13;

如果要使用弹性布局,请将display: flex;应用于nav,然后使用align-items垂直居中。然后将margin-left: auto分配给#nav-items以将该菜单推向右侧。

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}

#tagline {
  font-style: italic;
}

nav {
  background-color: white;
  -webkit-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 6px 7px -2px rgba(0, 0, 0, 0.75);
}

#logo {
  padding: 10px 10px 10px 10px;
  vertical-align: middle;
}

li {
  display: inline-block;
}

a {
  color: black;
  text-decoration: none;
}

nav {
  display: flex;
  align-items: center;
}

#nav-items {
  margin-left: auto;
}
&#13;
<nav>
  <img src="https://s1.postimg.org/qiu818fhr/lb_logo.png" id="logo" alt="logo">
  <span id="tagline">Live, 1-to-1, flexible and personalized</span>
  <ul id="nav-items">
    <li><a href="#">How it Works</a></li>
    <li><a href="#">Courses</a></li>
    <li><a href="#">Teachers</a></li>
    <li><a href="#">Enroll</a></li>
    <li><a href="#">Login</a></li>
  </ul>
</nav>
&#13;
&#13;
&#13;