首页 > 解决方案 > 内容 div 位于导航栏上方

问题描述

所以我有一个固定在顶部的导航栏,为此我已经使用过position: fixed,但由于某种原因,我的内容覆盖了它。我想要这样的东西:

在此处输入图像描述

html {
  position: relative;
  min-height: 100%;
}

.header {
  top: 0;
  position: fixed;
}

.footer {
  position: fixed;
  bottom: 0
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="styles.css" media="screen" />
  <title>Static Template</title>
</head>

<body>
  <div class="header">
    <h2>Header</h2>
  </div>
  <div class="row">
    <div class="column side" style="background-color: #aaa;">Column</div>
    <div class="column middle" style="background-color: #bbb;">Column</div>
    <div class="column side" style="background-color: #ccc;">Column</div>
  </div>

  <div class="footer">
    <p>Footer</p>
  </div>
</body>

Codesandbox 演示: https ://codesandbox.io/s/laughing-dew-2g2r6?file=/index.html

标签: css

解决方案


margin-top您必须在课程中添加一些row内容。

html {
  position: relative;
  min-height: 100%;
}

.header {
  top: 0;
  position: fixed;
}

.row {
  margin-top: 50px;
}

.footer {
  position: fixed;
  bottom: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="styles.css" media="screen" />
  <title>Static Template</title>
</head>

<body>
  <div class="header">
    <h2>Header</h2>
  </div>
  <div class="row">
    <div class="column side" style="background-color: #aaa;">Column</div>
    <div class="column middle" style="background-color: #bbb;">Column</div>
    <div class="column side" style="background-color: #ccc;">Column</div>
  </div>

  <div class="footer">
    <p>Footer</p>
  </div>
</body>


推荐阅读