1

I am trying to create a header that when you scroll it will stick to the top of the page.

I have tried positon: fixed but it moved behind the the div containing the image. I tried sticky it's the same story. Anh as you can see I tried position: fixed and sticky on some placed to see i it would work but it doesn't.

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@100&display=swap');
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  /* padding-top: 100px; */
}

.section1 {
  /* position: sticky; */
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
  text-align: center;
  color: blueviolet;
}

.section1 img {
  /* position: sticky; */
  flex-shrink: 0;
  min-width: 100%;
  min-height: 100%;
  position: relative;
  text-align: center;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

li,
a,
button {
  font-family: "Montserrat", sans-serif;
  font-weight: 500;
  font-size: 16px;
  color: #edf0f1;
}

header {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30px 10%;
  background-color: #24252a;
}

.header_logo {
  cursor: pointer;
  margin-left: -70px;
}

.nav_links {
  list-style-type: none;
}

.nav_links li {
  display: inline-block;
  padding: 0px 20px;
}

.nav_links li a {
  transition: all 0.3s ease 0s;
}

.nav_links li a:hover {
  color: #0088a9;
}

button {
  padding: 9px 25px;
  background-color: rgba(0, 139, 169, 1);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s ease 0s;
}

button:hover {
  background-color: rgba(0, 139, 169, 0.8);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <header>
    <img class="header_logo" src="./youtube_logo.png" alt="youtube's logo" width="5%" height="5%">
    <nav=c lass="navbar">
      <ul class="nav_links">
        <li><a href="#">SERVICE</a></li>
        <li><a href="#">STORE</a></li>
        <li><a href="#">CONTACT</a></li>
        <li><a href="#">MORE</a></li>
      </ul>
      </nav>
      <a class="social_buttion" href="#"><button>SOCIAL</button></a>
  </header>
  <section class="section1">
    <img class="page_logo1" src="./2f606ad14bf9171e5f41b42a01b4441f.jpg">
    <div class="centered">
      this is center
  </section>
  </div>
  <div class="section2">
    <img class="page_logo2" src="./2f606ad14bf9171e5f41b42a01b4441f.jpg">
  </div>


</body>

</html>
CosmosLee
  • 57
  • 3
  • 7

2 Answers2

1

Just add z-index:100; to your header and it remains on top when scrolling and the image goes behind.

Reference for z-index

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@100&display=swap');
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  /* padding-top: 100px; */
}

.section1 {
  /* position: sticky; */
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
  text-align: center;
  color: blueviolet;
}

.section1 img {
  /* position: sticky; */
  flex-shrink: 0;
  min-width: 100%;
  min-height: 100%;
  position: relative;
  text-align: center;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

li,
a,
button {
  font-family: "Montserrat", sans-serif;
  font-weight: 500;
  font-size: 16px;
  color: #edf0f1;
}

header {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30px 10%;
  background-color: #24252a;
  z-index:100;/* add this */
}

.header_logo {
  cursor: pointer;
  margin-left: -70px;
}

.nav_links {
  list-style-type: none;
}

.nav_links li {
  display: inline-block;
  padding: 0px 20px;
}

.nav_links li a {
  transition: all 0.3s ease 0s;
}

.nav_links li a:hover {
  color: #0088a9;
}

button {
  padding: 9px 25px;
  background-color: rgba(0, 139, 169, 1);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s ease 0s;
}

button:hover {
  background-color: rgba(0, 139, 169, 0.8);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <header>
    <img class="header_logo" src="./youtube_logo.png" alt="youtube's logo" width="5%" height="5%">
    <nav=c lass="navbar">
      <ul class="nav_links">
        <li><a href="#">SERVICE</a></li>
        <li><a href="#">STORE</a></li>
        <li><a href="#">CONTACT</a></li>
        <li><a href="#">MORE</a></li>
      </ul>
      </nav>
      <a class="social_buttion" href="#"><button>SOCIAL</button></a>
  </header>
  <section class="section1">
    <img class="page_logo1" src="./2f606ad14bf9171e5f41b42a01b4441f.jpg">
    <div class="centered">
      this is center
  </section>
  </div>
  <div class="section2">
    <img class="page_logo2" src="./2f606ad14bf9171e5f41b42a01b4441f.jpg">
  </div>


</body>

</html>
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
1

Use a positive z-index value for header

body {
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  /* padding-top: 100px; */
}

.section1 {
  /* position: sticky; */
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
  text-align: center;
  color: blueviolet;
}

.section1 img {
  /* position: sticky; */
  flex-shrink: 0;
  min-width: 100%;
  min-height: 100%;
  position: relative;
  text-align: center;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

li,
a,
button {
  font-family: "Montserrat", sans-serif;
  font-weight: 500;
  font-size: 16px;
  color: #edf0f1;
}

header {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30px 10%;
  background-color: #24252a;
  z-index: 1;
}

.header_logo {
  cursor: pointer;
  margin-left: -70px;
}

.nav_links {
  list-style-type: none;
}

.nav_links li {
  display: inline-block;
  padding: 0px 20px;
}

.nav_links li a {
  transition: all 0.3s ease 0s;
}

.nav_links li a:hover {
  color: #0088a9;
}

button {
  padding: 9px 25px;
  background-color: rgba(0, 139, 169, 1);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s ease 0s;
}

button:hover {
  background-color: rgba(0, 139, 169, 0.8);
}
<header>
  <img class="header_logo" src="https://via.placeholder.com/200" alt="youtube's logo" width="5%" height="5%">
  <nav=c lass="navbar">
    <ul class="nav_links">
      <li><a href="#">SERVICE</a></li>
      <li><a href="#">STORE</a></li>
      <li><a href="#">CONTACT</a></li>
      <li><a href="#">MORE</a></li>
    </ul>
    </nav>
    <a class="social_buttion" href="#"><button>SOCIAL</button></a>
</header>
<section class="section1">
  <img class="page_logo1" src="https://via.placeholder.com/200">
  <div class="centered">
    this is center
</section>
</div>
<div class="section2">
  <img class="page_logo2" src="https://via.placeholder.com/200">
</div>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49