Skip to content Skip to sidebar Skip to footer

Content Below Should Not Scroll When Fixed Div Is Visible

What's expected? I don't want the content below scrollable when loader is loading. I tried z-index, position(relative to other elements). But they aren't working.

Solution 1:

add overflow: hidden; to html selector

body,
html {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.fixed {
  display: flex;
  color: white;
  align-items: center;
  justify-content: center;
  position: fixed;
  background: rgba(0, 0, 0, 0.5);
  height: 100vh;
  width: 100vw;
  z-index: 9999
}

.content {
  height: 1300px;
  background: red;
  z-index: 0;
}


/* Please ignore the below code, as it has nothing to do with the problem */.loader {
  color: #ffffff;
  font-size: 90px;
  text-indent: -9999em;
  overflow: hidden;
  width: 1em;
  height: 1em;
  border-radius: 50%;
  margin: 72px auto;
  position: relative;
  -webkit-transform: translateZ(0);
  -ms-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-animation: load6 1.7s infinite ease, round 1.7s infinite ease;
  animation: load6 1.7s infinite ease, round 1.7s infinite ease;
}

@-webkit-keyframes load6 {
  0% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  5%,
  95% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  10%,
  59% {
    box-shadow: 0 -0.83em0 -0.4em, -0.087em -0.825em0 -0.42em, -0.173em -0.812em0 -0.44em, -0.256em -0.789em0 -0.46em, -0.297em -0.775em0 -0.477em;
  }
  20% {
    box-shadow: 0 -0.83em0 -0.4em, -0.338em -0.758em0 -0.42em, -0.555em -0.617em0 -0.44em, -0.671em -0.488em0 -0.46em, -0.749em -0.34em0 -0.477em;
  }
  38% {
    box-shadow: 0 -0.83em0 -0.4em, -0.377em -0.74em0 -0.42em, -0.645em -0.522em0 -0.44em, -0.775em -0.297em0 -0.46em, -0.82em -0.09em0 -0.477em;
  }
  100% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
}

@keyframes load6 {
  0% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  5%,
  95% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  10%,
  59% {
    box-shadow: 0 -0.83em0 -0.4em, -0.087em -0.825em0 -0.42em, -0.173em -0.812em0 -0.44em, -0.256em -0.789em0 -0.46em, -0.297em -0.775em0 -0.477em;
  }
  20% {
    box-shadow: 0 -0.83em0 -0.4em, -0.338em -0.758em0 -0.42em, -0.555em -0.617em0 -0.44em, -0.671em -0.488em0 -0.46em, -0.749em -0.34em0 -0.477em;
  }
  38% {
    box-shadow: 0 -0.83em0 -0.4em, -0.377em -0.74em0 -0.42em, -0.645em -0.522em0 -0.44em, -0.775em -0.297em0 -0.46em, -0.82em -0.09em0 -0.477em;
  }
  100% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
}

@-webkit-keyframes round {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@keyframes round {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
<div><divclass="fixed"><divclass="loader">Loading...</div></div><divclass="content"><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div></div></div>

Solution 2:

Move the overflow to another wrapper. Actually it's on the viewport and the fixed element is also positionned relatively to the viewport thus it cannot hide the scroll

body,
html {
  margin: 0;
  padding: 0;
}

.fixed {
  display: flex;
  color: white;
  align-items: center;
  justify-content: center;
  position: fixed;
  background: rgba(0, 0, 0, 0.5);
  height: 100vh;
  width: 100vw;
  z-index: 9999;
}

.content {
  height: 1300px;
  background: red;
  z-index: 0;
}

/*added*/.wrapper {
  height:100vh;
  overflow:auto;
}
/**//* Please ignore the below code, as it has nothing to do with the problem */.loader {
  color: #ffffff;
  font-size: 90px;
  text-indent: -9999em;
  overflow: hidden;
  width: 1em;
  height: 1em;
  border-radius: 50%;
  margin: 72px auto;
  position: relative;
  transform: translateZ(0);
  -webkit-animation: load6 1.7s infinite ease, round 1.7s infinite ease;
  animation: load6 1.7s infinite ease, round 1.7s infinite ease;
}


@keyframes load6 {
  0% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  5%,
  95% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
  10%,
  59% {
    box-shadow: 0 -0.83em0 -0.4em, -0.087em -0.825em0 -0.42em, -0.173em -0.812em0 -0.44em, -0.256em -0.789em0 -0.46em, -0.297em -0.775em0 -0.477em;
  }
  20% {
    box-shadow: 0 -0.83em0 -0.4em, -0.338em -0.758em0 -0.42em, -0.555em -0.617em0 -0.44em, -0.671em -0.488em0 -0.46em, -0.749em -0.34em0 -0.477em;
  }
  38% {
    box-shadow: 0 -0.83em0 -0.4em, -0.377em -0.74em0 -0.42em, -0.645em -0.522em0 -0.44em, -0.775em -0.297em0 -0.46em, -0.82em -0.09em0 -0.477em;
  }
  100% {
    box-shadow: 0 -0.83em0 -0.4em, 0 -0.83em0 -0.42em, 0 -0.83em0 -0.44em, 0 -0.83em0 -0.46em, 0 -0.83em0 -0.477em;
  }
}
@keyframes round {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
<divclass="wrapper"><divclass="fixed"><divclass="loader">Loading...</div></div><divclass="content"><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div><div>Hello</div></div></div>

Solution 3:

setting the position of .content to fixed will also fix the problem. you may also need to set the width in that case:

.content {
      height: 1300px;
      background: red;
      z-index: 0;
      position:fixed;
      width:100%;
    }

Post a Comment for "Content Below Should Not Scroll When Fixed Div Is Visible"