html - 带有视口(viewport)的移动 View 中导航栏上方的间隙

标签 html css

今天晚上我开始深入研究 html/css,但偶然发现了一个障碍。我正在尝试为我的 GitHub 页面制作一个导航栏,它在桌面 View 上显示得很好。导航栏将固定在桌面 View 中,但在移动 View 中我想保留在页面顶部。

这是当 width 小于 600px 时的显示方式。 enter image description here

我想移除导航栏上方的空隙,但不知道该怎么做。

html, body {
    height: 100%; margin: 0; padding: 0; padding-top: 40px;
}

body {
    font-family: sans-serif;
    background: #daf2ea
}

a {
    text-decoration: none;
}

.navbar {
    height: 40px;
    background: #333;
    color: #eee;
    padding-top: 7px;
    padding-bottom: 7px;
    box-shadow: 0 0 5px #777;
    position: fixed;
    top: 0;
    width: 100%;
}

.navbar h1 {
    font-weight: 300;
    font-size: 26px;
    margin-top: 4px;
    margin-left: 20px;
    padding-top: 0;
    text-shadow: 0 1px 2px #000;
    float: left;
}

.navbar a {color:white;}

.navbar a:hover {text-decoration: underline;}

.navbar ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

.navbar li {float: left;}

.navbar li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

@media screen and (max-width: 600px) {
    .navbar {
        position: static;
        top: auto;
        width: auto;
        padding-bottom: 14px;
    }

    .navbar h1 {
        float: none;
        text-align: center;
        margin-left: 0px;
    }

    .navbar li {float: none;}

    .navbar li a:hover {
        background-color: #1384c1;
    }
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lukasz Salitra - Home</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
      <div class="navbar">
          <h1><a href="index.html">Lukasz Salitra</a></h1>
          <ul>
              <li>
                  <a href="index.html">Home</a>
              </li>
              <li>
                  <a href="about.html">About</a>
              </li>
              <li>
                  <a href="contact.html">Contact</a>
              </li>
          </ul>
      </div>
  </body>
<html>

最佳答案

只需从 html 选择器中删除 padding-top: 40px; 行。

html, body {
    height: 100%; margin: 0; padding: 0;
}

关于html - 带有视口(viewport)的移动 View 中导航栏上方的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47685996/

相关文章:

html - 导航栏不与标题文本内联显示

css - 如何将列与 flexbox 对齐?

html - 带背景图片的 Internet Explorer 伪元素

html - 如何阻止 md-card 占据整个水平宽度?我如何并排放置它们?

html - 在CSS中计算高度

javascript - 如何在不使用 SVG 文件本身的 ID 的情况下更改 SVG 颜色?

html - 当父级更改大小时,图像容器不灵活

javascript - 从滚动站点的其余部分分离滚动导航栏

javascript - 将显示设置为可见状态后元素动画是否运行?

javascript - 子菜单关闭太快