html - 将图像添加到导航栏链接

标签 html css

我想将 PNG 图像添加到我的导航栏。我找不到要添加到我的背景图像中的正确位置:并且它看起来是正确的。图像不以文本为中心,我需要它。谢谢

@charset "utf-8";

/* CSS Document */

body {
  text-align: center
}
#navbar {
  position: relative;
  top: 15px;
  padding: 0;
  display: inline-block;
  background-repeat: repeat-x;
  border-radius: 15px;
}
#navbar li {
  list-style: none;
  float: left;
  position: relative;
  height: 50px;
  line-height: 50px;
  background-image: url(images/NAV.png);
}
#navbar li a {
  display: block;
  padding: 3px 30px;
  text-transform: uppercase;
  text-decoration: none;
  color: #999;
  font-weight: bold;
}
#navbar li a:hover {
  color: #000;
}
#navbar li ul {
  display: none;
  position: absolute;
  left: 0;
  width: 100%;
  margin: 0;
  padding: 0;
  text-align: center;
  margin-left: -100px;
}
#navbar li ul table {
  margin-top: -6px;
  width: 350px;
  background-image: url(images/NAV.png);
  background-repeat: repeat-x;
  border-radius: 15px;
}
#navbar li ul table tr {
  height: 25px;
}
#navbar li:hover ul {
  display: block;
}
#navbar li:hover li {
  float: center;
}
#navbar li:hover li a {
  color: inherit;
}
#navbar li li a:hover {
  color: inherit;
}
#bottomnav {
  position: fixed;
  z-index: 100;
  bottom: 15px;
  left: 0;
  width: 100%;
}
<body>
  <header>
    <img src="../images/logo.jpg" width="800" height="225" alt="This is our logo." />
  </header>
  <ul id="navbar">
    <li><a href="../index.html">Home</a> 
    </li>
    <li><a href="../services.html">Services</a>
      <ul>
        <table align="center">
          <tr>
            <td><a href="../spraying.html">Spraying</a>
            </td>
            <td><a href="../combine.html">Combining</a>
            </td>
          </tr>
          <tr>
            <td><a href="../planting.html">Planting</a>
            </td>
            <td><a href="../drilling.html">Drilling</a>
            </td>
          </tr>
          <tr>
            <td><a href="../strip_tillage.html">Strip Tillage</a>
            </td>
            <td><a href="../tillage.html">Tillage</a>
            </td>
          </tr>
        </table>
      </ul>
    </li>
    <li><a href="#">Packages</a>
    </li>
    <li><a href="#">About Us</a>
    </li>
    <li><a href="#">Contact Us</a>
    </li>
  </ul>
  <div id="bottomnav">
    Copyright 2016 Cole Rohr &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Site Map
  </div>
</body>

enter image description here

最佳答案

我想你想要这样的东西:

#navbar li ul table {
  margin-top: -6px;
  width: 350px;
  background-image: url('http://i.stack.imgur.com/RuH5Q.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  border-radius: 15px;
}

#navbar li {
  list-style: none;
  float: left;
  position: relative;
  height: 50px;
  line-height: 50px;
  background-image: url('http://i.stack.imgur.com/RuH5Q.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

Fiddle

或者像这样,为下拉菜单中 table 元素的每个 td 保留背景:

#navbar li ul table td {
  background-image: url('http://i.stack.imgur.com/RuH5Q.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

Fiddle

关于html - 将图像添加到导航栏链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36779477/

相关文章:

html - 如何为我的 CSS 进度条制作动画?

html - 选择框的样式颜色(带有自定义包装器)下拉文本与 css

css - 在线聊天中 Web 共享的图像预览

php - 如何在下拉列表中显示选中的项目名称?

html - float 与绝对和相对 div 定位有何关系?

javascript - 如何从提示框中获取数值?

html - 样式 ="clear:both"有什么用?

html - 在 Internet Explorer 11 中,页脚显示在屏幕中间而不是底部

html - jquery ui 组合框下拉对齐在扩展时关闭

html - Bootstrap 响应式表单 - 标签放置