如何在菜单中移动小图像?

时间:2016-03-21 14:18:02

标签: html css image nav

如何移动<li>内的小箭头图像?

以下是代码:

body {
  margin: 0;
  padding: 0;
}
nav {
  width: 100%;
  background: #f1f1f1;
  padding: 5px;
}
nav ul {
  list-style-type: none;
}
nav ul li {
  display: inline-block;
}
nav ul li a img {
  max-width: 20px;
  height: auto;
  margin-left: 4px;
}
nav ul li a {
  color: #636363;
  padding: 10px;
  text-decoration: none;
}
nav ul li a:hover {
  color: #333;
  text-decoration: underline;
}
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="css-1.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script type="text/javascript" src="javascript-1.js"></script>
  <title>Haircut</title>
</head>

<body>
  <header>
    <nav>
      <ul>
        <li><a href="#">News</a>
        </li>
        <li><a href="#">Delivery</a>
        </li>
        <li><a href="#">Destinations<img src="https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_keyboard_arrow_down_48px-128.png"></a>
        </li>
        <li><a href="#">Guaranties</a>
        </li>
        <li><a href="#">Contacts</a>
        </li>
      </ul>
    </nav>
  </header>



</body>

</html>

如果我只是在img元素中添加margin-top,则会将所有ul向下移动。那不是我想要的。我想稍微移动它以与li项目

对齐

2 个答案:

答案 0 :(得分:3)

vertical-align: middle;可能是你需要的。

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
}
nav {
  width: 100%;
  background: #f1f1f1;
  padding: 5px;
}
nav ul {
  list-style-type: none;
}
nav ul li {
  display: inline-block;
}
nav ul li a img {
  max-width: 20px;
  height: auto;
  margin-left: 4px;
  vertical-align: middle;
}
nav ul li a {
  color: #636363;
  padding: 10px;
  text-decoration: none;
}
nav ul li a:hover {
  color: #333;
  text-decoration: underline;
}
&#13;
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="css-1.css">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script type="text/javascript" src="javascript-1.js"></script>
  <title>Haircut</title>
</head>

<body>
  <header>
    <nav>
      <ul>
        <li><a href="#">News</a>
        </li>
        <li><a href="#">Delivery</a>
        </li>
        <li><a href="#">Destinations<img src="https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_keyboard_arrow_down_48px-128.png"></a>
        </li>
        <li><a href="#">Guaranties</a>
        </li>
        <li><a href="#">Contacts</a>
        </li>
      </ul>
    </nav>
  </header>



</body>

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

答案 1 :(得分:0)

您可以将图像设置为绝对位置。

nav ul li a img {
  position:absolute;
  max-width: 20px;
  height: auto;
  margin-left: 4px;
}

然后你可以使用top属性来定位它,不是很漂亮但是有效

http://jsbin.com/goyozo/edit?html,css