CSS位置粘性不适用于bootstrap 4类行

时间:2019-09-01 16:55:45

标签: css

我无法将菜单固定在网站上。 当页面内容具有具有Bootstrap 4 ROW类的DIV时,此DIV将覆盖菜单。 这是html代码:

<html>
<head>
<script type="text/javascript" 
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<main class="main-container">
  <h4>
  Scroll page down to see the HEADER fixed on top
  </h4>
  <header class="main-header">HEADER</header>
  <div class="main-content">
    <h4>MAIN CONTENT TITLE</h4>
    <div class="row"><!-- if delete this DIV, works fine -->
      <div class="col">
        left col
      </div>
      <div class="col">
        right col
      </div>
    </div><!-- row -->
  </div><!-- main-content -->
  <footer class="main-footer">FOOTER</footer>
</main>
</body>
</html>

这里是CSS代码:

.main-container{ max-width:600px; margin:0 auto; border:solid 10px green; padding:10px; margin-top:40px;}
.main-container *{padding:10px;background:#aaa; border:dashed 5px #000;}
.main-container * + *{margin-top:20px;}
.main-header{ height:50px; background:#aaa; border-color:red;}
.main-content{ min-height:1000px;}
.main-header{position:-webkit-sticky; position:sticky; top:0;}

https://jsfiddle.net/WRobynson/qhdfbjmv/10/

请,有人可以帮我吗?

谢谢

1 个答案:

答案 0 :(得分:1)

在主标题中添加z-index。

.main-container{ max-width:600px; margin:0 auto; border:solid 10px green; padding:10px; margin-top:40px;}
.main-container *{padding:10px;background:#aaa; border:dashed 5px #000;}
.main-container * + *{margin-top:20px;}
.main-header{ height:50px; background:#aaa; border-color:red; z-index:100;}
.main-content{ min-height:1000px;}
.main-header{position:-webkit-sticky; position:sticky; top:0;}
<html>
<head>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<main class="main-container">
  <header class="main-header">HEADER</header>
  <div class="main-content">
    <h4>MAIN CONTENT TITLE</h4>
    <div class="row">
      <div class="col">
        left col
      </div>
      <div class="col">
        right col
      </div>
    </div><!-- row -->
  </div><!-- main-content -->
  <footer class="main-footer">FOOTER</footer>
</main>
</body>
</html>