Avoiding overlapping with fixed element

marmistrz

I'm creating a fixed navigation at the top of the webpage. But then the wrapper content would overlap. So I'm setting the margin-top with jQuery:

$('#wrapper').css('margin-top', function() {return $('nav').height();});
nav {
  background-color: #cccccc;
  position: fixed;
  top: 0;
  width: 100%;
  line-height: 2.5;
}

@media screen and (min-width: 800px) {
    nav {
        line-height: 1.5;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<nav>
    NAV
</nav>
<div id="wrapper">
    <header id="header" class="alt">
        HEADER
    </header>
    <main>
        Lorem <br>
        ipsum <br>
        dolor <br>
        sit <br>
        amet <br>
    </main>     
</div>

But then, if I open the page in a small window and then maximize it, the nav will resize but the margin-top will stay the same.

As I understand, the css function is called only once. Is it possible to bind margin-top of the #wrapper to the height of nav?

yezzz

Set it on document ready and on window resize. For instance:

function setWrapper() {
    $('#wrapper').css('margin-top', $('nav').height() );
};

$(setWrapper);
$(window).resize(setWrapper);

You may also want to throttle or debounce the call to the function.

An alternative would be to use "regular" css and media queries to set the margin-top.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

position fixed element is overlapping into footer

From Dev

position: fixed overlapping page

From Dev

fixed position header overlapping

From Dev

Overlapping dynamic fixed header

From Dev

Position fixed overlapping issue?

From Dev

Fixed notifications overlapping

From Dev

Avoiding overlapping tuples in python while appending to a list

From Dev

Regex for overlapping fixed width matches

From Dev

position relative overlapping position fixed

From Dev

Fixed image overlapping another div

From Dev

Bootstrap Fixed Navbar Overlapping Content

From Dev

A fixed size row with overlapping elements

From Dev

Listview Element onItemClickListeners overlapping

From Dev

Avoiding overlapping of labels with direct labels and ggplot2

From Java

twitter bootstrap navbar fixed top overlapping site

From Dev

Keep Fixed Menu-bar from overlapping

From Dev

fixed div overlapping adjacent div on horizontal scroll

From Dev

Keeping fixed footer and header from overlapping

From Dev

Prevent images from overlapping a fixed header

From Dev

DIV Position Fixed, inner content overlapping

From Dev

Fixed element inside draggable fixed element

From Dev

Fixed element below fixed element without JS

From Dev

Position a fixed element below another fixed element

From Dev

Thymeleaf: append to html element / avoiding repetitive iteration

From Dev

Nested loop through list avoiding same element

From Dev

Image overlapping DOM element border

From Dev

CSS ::after element text overlapping

From Dev

Stick Element to Bottom of Fixed Element

From Dev

Absolute element on top of fixed element?