简体   繁体   中英

Bootstrap menu covering page content

I'd like to use a menu bar with Bootstrap that always stays on top of the page. Therefore I used the class navbar-fixed-top . I used the code from the very end of this post and ran into a problem with visibility of my content

The main content is partly overlapped by the menue.

在此处输入图片说明

On http://getbootstrap.com/examples/starter-template/ the problem is "solved" by adding padding-top: 50px; to the body. But this does not work, when the menu is wrapping and becomes larger than 50px:

在此处输入图片说明

Is there a class in bootstrap that covers this problem? Or do I have to add a JavaScript that listens to changes of the window-size and adjusts the padding-top? I have no idea how to do that, but that's the only thing that came to my mind,...

CODE:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
    <style>
        h1 {
            font-size: 5em;
        }
        body {
            /*
            padding-top: 50px;
            */
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="">Project name</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li><a href="#item01">Item01</a></li>
                    <li><a href="#item02">Item02</a></li>
                    <li><a href="#item03">Item03</a></li>
                    <li><a href="#item04">Item04</a></li>
                    <li><a href="#item05">Item05</a></li>
                    <li><a href="#item06">Item06</a></li>
                    <li><a href="#item07">Item07</a></li>
                    <li><a href="#item08">Item08</a></li>
                    <li><a href="#item09">Item09</a></li>
                    <li><a href="#item10">Item10</a></li>
                    <li><a href="#item11">Item11</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </nav>

    <div class="container"">
        <div>
            <h1 id="about">My first template</h1>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
        </div>
        <div id="contact" style="margin: 100em 0;">Contact us</div>
    </div><!-- /.container -->

    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script>
        /* jQuery test. */
        $(function() {
            $('h1').html("Test");
        });
    </script>
</body>
</html>

I am unaware of a bootstrap class that fixes this problem. You could certainly add a listener to you window resize as you mentioned. It would solve the problem of your height not always being 50px. Should be something to the effect of:

$(window).on('resize', function(){
    var navHeight = $('nav').height();
    $('body').css('padding-top', navHeight + 'px');
});

This is untested, but should get you started. Hope this helps!

The thing is when you use "position: fixed" the element is taken out of the "flow" and is positioned relative to the window and not to the other elements. Therefore you must add a margin, like they have done in the bootstrap example, for the menu to not cover the underlying content. You can also add padding to body but I would prefer using margin on the element instead.

If you don't know the height of the menu on forehand I would recommend you use the answer @wrxsti wrote.

Give .navbar a margin-bottom within your CSS. That should fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM