CSS Sticky position does not work with Bootstrap class=Row

Asked

Viewed 310 times

2

Hail, hail.

I’m having trouble fixing a menu on a website. When page content has a DIV with class bootstarp ROW, this DIV overwrites the menu at scrolling time.

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;}

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

Does anyone have an idea to help?

Thank you.

1 answer

2


This is a z-index problem, along with the positio:sticky you have to declare a z-indez > 1, this will solve the problem. Type as the code below

.main-header{position:-webkit-sticky; position:sticky; top:0; z-index:10}

To see working run the code below

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" />
<style>
    .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;
        z-index: 10;
    }

</style>
</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>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</body>

</html>

  • Perfect! Thank you very much, Hugo. Hug.

  • @Walterrobynson cool that solve there, if you believe that the question has been solved consider mark it with a click on that is just below the arrows next to the answer. It was worth the strength, success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.