0

Here is the code:

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>

The goal I am trying to achieve is having the text from the section tag and have it directly below my fixed navbar. I have attempted to put a z-index with the values of above 1 into header, nav and #nav-list, but it has not worked. What can be re-configured to make this work? I am sorry. I have tried all I know and searched this site but to no success.

  • Edited my post. – Degenerate Jan 04 '21 at 02:45
  • `position: fixed` and `absolute` puts the navbar out of the normal flow. Its a normal behavior. Either the content needs a top margin equal the navbar height or you need to use `position: sticky` – tacoshy Jan 04 '21 at 02:47

2 Answers2

1

I would just add a margin-top equivalent to the nav offsetHeight, which in this case is 106 pixels.

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
section{
  margin-top:106px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>
If you have a responsive navigation bar you could use JS to dynamically set the margin top.

document.getElementsByTagName("section")[0].style.marginTop = document.getElementsByTagName("nav")[0].offsetHeight+"px";
body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>
Spectric
  • 30,714
  • 6
  • 20
  • 43
0

position: fixed; and position: absolute; puts the header out of the normal flow. use position: sticky; instead:

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: sticky;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>

Alternatively, you can use a top-margin at the section:

body {
  margin: 0;
  padding: 0;
}

/* NAVIGATION */
nav {
  background: #222222;
  padding: 10px 40px 10px 70px;
  color: #ffffff;
}

header {
  position: fixed;
  width: 100%;
  top: 0;
}

#nav-list {
  display: flex;
  list-style: none;
  justify-content: space-around;
  align-items: center;
  padding-left: 0;
}

#search {
  height: 40px;
  width: 240px;
  display: flex;
  border-radius: 5px;
}
 
#searchBar {
  height: 100%;
  width: 200px;
  border: none;
  border-radius: 5px 0 0 5px;
  outline: none;
  padding: 0 10px;
  color: #000;
  font-size: 16px;
} 

#search #searchButton {
  height: 100%;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  background-color: rgb(81, 22, 219);
  color: #222222;
  transition: color 0.75s;
}

#search #searchButton:hover {
  color: #fff;
}

#page-title {
  font-size: 24px;
}

/* MAIN CONTENT */

#content {
  /* test to see if the color will appear */
  background: blue;
}

section {
  margin-top: 90px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Movie Vault</title>
  <link rel="stylesheet" href="test.css">
</head>
<body>

  <header>
    <nav>
      <ul id="nav-list">
        <li id="search">
          <input id="searchBar" type="search" placeholder="Search...">
          <label id="searchButton">
            <i class="fas fa-search"></i>
          </label>
        </li>
        <li id="page-title">Video Vault</li>
      </ul>
    </nav>
  </header>

  <section>
    test
  </section>

</body>
</html>
Spectric
  • 30,714
  • 6
  • 20
  • 43
tacoshy
  • 10,642
  • 5
  • 17
  • 34