【问题标题】:Navigation Bar Gap On Hover悬停时导航栏间隙
【发布时间】:2017-12-15 10:25:39
【问题描述】:

我正在开发cordova android 应用程序,我正在尝试制作导航栏,我已经完成了它的创建,但它有一个差距。我不知道这个差距是从哪里来的,我想消除差距。

这是我的 HTML:

<nav class="fixed-nav">
        <ul>
            <li><a href="#"><img src="image/cofee.png" height="30" class="menu-icon"/>Cofee</a></li>
            <li><a href="#"><img src="image/koki.png" height="30" class="menu-icon"/>Restaurant</a></li>
            <li><a href="#"><img src="image/beer.png" height="30" class="menu-icon"/>Drinks & Nightlife</a></li>
        </ul>
    </nav>

这是我的 CSS:

.fixed-nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #ddd;
    white-space: nowrap;
    height: 50px;
    box-sizing: border-box;
    padding: 10px;
    box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16), 0px 3px 6px rgba(0, 0, 0, 0.23);
}

.fixed-nav ul,
.fixed-nav li {
    display: inline;
}

.fixed-nav a {
    text-decoration: none;
    text-transform: uppercase;
    padding: 17px 10px;
    color: #333;
    font-family: arial;
}

.fixed-nav a:hover {
    background-color: #000;
    color: #eee;
}

.fixed-nav ul {
    padding: 0;
}

.fixed-nav img {
    vertical-align: middle;
}

.menu-icon{
    margin-right: 5px;
}

main {
    margin-top: 55px;
}

【问题讨论】:

  • 您为 .fixed-nav 定义了 10 像素的内边距
  • 听起来不错,但不起作用。试过了,差距还是有的

标签: android html css cordova


【解决方案1】:

.fixed-nav 中更改 padding 值 From

padding: 10px;

padding: 10px 0px;

试试下面的代码,

.fixed-nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #ddd;
    white-space: nowrap;
    height: 50px;
    box-sizing: border-box;
    padding: 10px 0px;
    box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16), 0px 3px 6px rgba(0, 0, 0, 0.23);
}

.fixed-nav ul,
.fixed-nav li {
    display: inline;
}

.fixed-nav a {
    text-decoration: none;
    text-transform: uppercase;
    padding: 17px 10px;
    color: #333;
    font-family: arial;
}

.fixed-nav a:hover {
    background-color: #000;
    color: #eee;
}

.fixed-nav ul {
    padding: 0;
}

.fixed-nav img {
    vertical-align: middle;
}

.menu-icon{
    margin-right: 5px;
}

main {
    margin-top: 55px;
}
<nav class="fixed-nav">
        <ul>
            <li><a href="#"><img src="image/cofee.png" height="30" class="menu-icon"/>Cofee</a></li>
            <li><a href="#"><img src="image/koki.png" height="30" class="menu-icon"/>Restaurant</a></li>
            <li><a href="#"><img src="image/beer.png" height="30" class="menu-icon"/>Drinks & Nightlife</a></li>
        </ul>
    </nav>

【讨论】:

    【解决方案2】:

    您已为.fixed-nav 提供了10px 的填充。删除左侧的填充以解决问题。

    使用flexbox的另一种布局方法...

    .fixed-nav {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      background-color: #ddd;
      white-space: nowrap;
      height: 50px;
      box-sizing: border-box;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16), 0px 3px 6px rgba(0, 0, 0, 0.23);
    }
    
    .fixed-nav ul,
    .fixed-nav li,
    .fixed-nav a {
      display: flex;
    }
    
    .fixed-nav ul {
      height: 50px;
      margin: 0;
    }
    
    .fixed-nav a {
      text-decoration: none;
      text-transform: uppercase;
      color: #333;
      font-family: arial;
      align-items: center;
      padding: 0 10px;
    }
    
    .fixed-nav a:hover {
      background-color: #000;
      color: #eee;
    }
    
    .fixed-nav ul {
      padding: 0;
    }
    
    .fixed-nav img {
      vertical-align: middle;
    }
    
    .menu-icon {
      margin-right: 5px;
    }
    
    main {
      margin-top: 55px;
    }
    <nav class="fixed-nav">
      <ul>
        <li>
          <a href="#"><img src="image/cofee.png" height="30" class="menu-icon" />Cofee</a>
        </li>
        <li>
          <a href="#"><img src="image/koki.png" height="30" class="menu-icon" />Restaurant</a>
        </li>
        <li>
          <a href="#"><img src="image/beer.png" height="30" class="menu-icon" />Drinks & Nightlife</a>
        </li>
      </ul>
    </nav>

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 2017-06-11
      • 2020-02-07
      • 2017-09-02
      • 1970-01-01
      相关资源
      最近更新 更多