reddtoric reddtoric - 1 month ago 10
CSS Question

centering div fixed position -no solution worked

I just want to make a preloader that's positioned in the middle of the page.

Requirements:


  • position: fixed;

  • centered on the X-axis



It's just a div with class 'preloader' in body. Body is this div's direct parent, no other wrapper in between.

.preloader {
position: fixed;
display: inline-block;
z-index: 99;
top: 45%;
left: 50%;
width: 60px;
height: 60px;
-ms-transform: translateX(100px); /*100px just to test if it moved at all, initially had -50%, see list below*/
-webkit-transform: translateX(100px);
transform: translateX(100px);
//=====rest is just animation and aesthetics======
border: 3px solid #8A2EE6;
border-radius: 50%;
border-bottom: 3px solid black;
box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
animation-name: rotatePreloader;
animation-duration: 0.65s;
animation-direction: normal;
animation-iteration-count: infinite;
animation-timing-function: linear;
transition: opacity 1s;
}


I've done:


  • display: block;

  • display: inline-block;

  • translateX(-50%);

  • translateX(30px);

  • translate(-50%, 0);

  • translate(30px, 0);

  • translate(-50%, -50%);

  • rearranging the transforms.. lol

  • took off -o & -mos

  • margin: 0 auto (worked with position: relative when I didn't need it to be fixed)



https://jsfiddle.net/goa3v2ke/#

Answer

You need to do like this, where you set top/left to 50% and then move them back with translate using the same value, int this case -50%.

Sample 1 now center it both horizontal and vertical, and by change the top/left values, you can move it in the direction you want, sample 2 has 33% as top value.


Update based on question edit

The small X/Y-axis offset is caused by the rotate being executed before the translate, so change your @keyframes rule to this, showed in sample 3 and an update of your fiddle

@keyframes rotatePreloader {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

Sample 1

.preloader {
    position: fixed;
    display: inline-block;
    z-index: 99;
    top: 50%;
    left: 50%;
    width: 60px;
    height: 60px;
    transform: translate(-50%,-50%);
  
/*=====rest is just animation and aesthetics======*/
    border: 3px solid #8A2EE6;
    border-radius: 50%;
    border-bottom: 3px solid black;
    box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
    animation-name: rotatePreloader;
    animation-duration: 0.65s;
    animation-direction: normal;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    transition: opacity 1s;
}
<div class="preloader"></div>

Sample 2

.preloader {
    position: fixed;
    display: inline-block;
    z-index: 99;
    top: 33%;
    left: 50%;
    width: 60px;
    height: 60px;
    transform: translate(-50%,-50%);
  
/*=====rest is just animation and aesthetics======*/
    border: 3px solid #8A2EE6;
    border-radius: 50%;
    border-bottom: 3px solid black;
    box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
    animation-name: rotatePreloader;
    animation-duration: 0.65s;
    animation-direction: normal;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    transition: opacity 1s;
}
<div class="preloader"></div>

Sample 3

.leftPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  left: 0px;
  transition: width 1s;
}

.leftPreloaderBG.loaded {
  width: 0;
}

.rightPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  right: 0px;
  transition: width 1s;
}

.rightPreloaderBG.loaded {
  width: 0;
}

@keyframes rotatePreloader {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

.preloader {
  position: fixed;
  display: inline-block;
  z-index: 99;
  top: 33%;
  left: 50%;
  width: 60px;
  height: 60px;
  transform: translate(-50%, -50%);
  /*=====rest is just animation and aesthetics======*/
  border: 3px solid #8A2EE6;
  border-radius: 50%;
  border-bottom: 3px solid black;
  box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
  animation-name: rotatePreloader;
  animation-duration: 0.65s;
  animation-direction: normal;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  transition: opacity 1s;
}

.preloader.loaded {
  opacity: 0;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  color: white;
  font-size: 1em;
  background-color: gray;
  background-position: center;
  background-repeat: repeat;
}
<body>
  <div class="preloader"></div>
  <div class="leftPreloaderBG"></div>
  <div class="rightPreloaderBG"></div>
</body>

Comments