Omar Dajani
Omar Dajani

Reputation: 378

Fixed nav-bar overlapping content (using margins makes page scrollable, I don't want that)

I have a quick question everyone. So I have added a fix nav-bar to the top of my page with a height of 44px. This part is fine. The issue is, because it has position: fixed it is not within the flow of the webpage. I tried adding margins and using paddings but it did not work. When I use margin-bottom: 44px; there is a gap on the bottom of the page when I scroll down.

Also... I do not want the page to be scrollable. In other words, I do not want the viewer to be able to scroll down the page because I have added paddings/margins.

Here is what I have tried so far:

<div class="wrapper">
      <nav>
        <div class="container">
          <ul>
            <li><a href="#" class="active"><i class="fab fa-houzz"></i></a></li>
            <li><a href="#">Ban</a></li>
            <li><a href="#">Warn</a></li>
            <li><a href="#">Gift</a></li>
            <li><a href="#">User</a></li>
            <li><a href="#">News</a></li>
            <li><a href="#">Reports</a></li>
            <li><a href="#">Omar</a></li>
          </ul>
        </div>
      </nav>
      <div class="content">

      </div>
    </div>

body {
  margin: 0;
  padding: 0;

  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrapper {
  height: 100vh;
}

nav {
  position: fixed;
  top: 0;
  width: 100%;
  height: 44px;
  overflow: scroll;

  background:   #323232;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
}

nav ul {
  display: flex;
  height: 44px;
  justify-content: space-around;
  align-items: center;

  padding: 0;
  margin: 0;
  list-style-type: none;
}

nav a {
  display: block;

  color: white;
  font-size: 15px;
  font-weight: lighter;
  text-decoration: none;
  transition: 0.3s;
}

nav a:hover {
  color: #B8B8B8;
}

ul li:last-child a {
  font-weight: normal;
}

nav ul li a.active {
  color: #B7B7B7;
}

.content {
  height: calc(100% - 44px);
  background: red;
  margin-top: 44px;
}

Upvotes: 0

Views: 59

Answers (1)

caiovisk
caiovisk

Reputation: 3809

box-sizing:border-box would work for you, so add:

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

See box-sizing docs: https://www.w3schools.com/css/css3_box-sizing.asp

Add padding top to your .wrapper as the same size of the nav, and remove calc() and margin-top from your .content

body {
  margin: 0;
  padding: 0;

  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
}
*, *:before, *:after {
	-moz-box-sizing: border-box;
	-webkit-box-sizing: border-box;
	box-sizing: border-box;
}
.wrapper {
  height: 100vh;
  padding-top: 44px;
}

nav {
  position: fixed;
  top: 0;
  width: 100%;
  height: 44px;
  overflow: scroll;

  background:   #323232;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
}

nav ul {
  display: flex;
  height: 44px;
  justify-content: space-around;
  align-items: center;

  padding: 0;
  margin: 0;
  list-style-type: none;
}

nav a {
  display: block;

  color: white;
  font-size: 15px;
  font-weight: lighter;
  text-decoration: none;
  transition: 0.3s;
}

nav a:hover {
  color: #B8B8B8;
}

ul li:last-child a {
  font-weight: normal;
}

nav ul li a.active {
  color: #B7B7B7;
}

.content {
  height: 100%;
  background: red;
}
<div class="wrapper">
    <nav>
      <div class="container">
        <ul>
          <li><a href="#" class="active"><i class="fab fa-houzz"></i></a></li>
          <li><a href="#">Ban</a></li>
          <li><a href="#">Warn</a></li>
          <li><a href="#">Gift</a></li>
          <li><a href="#">User</a></li>
          <li><a href="#">News</a></li>
          <li><a href="#">Reports</a></li>
          <li><a href="#">Omar</a></li>
        </ul>
      </div>
    </nav>
    <div class="content">

    </div>
  </div>

Upvotes: 1

Related Questions