绝对位置与其父CSS无关

时间:2019-09-06 08:02:27

标签: html css

我已经创建了该加载器,我想将其放置在其父级的中心。所以在父母 我正在使用的元素

display: flex;
align-items: center;
justify-content:center;

但是这不起作用。有帮助吗?

.empty-container {
  height: 100px;
  border: 1px solid gray;
}

.container {
  height: calc(100% - 100px);
  border: 1px solid red;
  display: flex;
  align-items: center;
  justify-content: center;
}

.component-loader {
  position: relative;
  text-align: center;
  transition: opacity .7s;
  border: 1px solid gray;
  height: 90px;
  width: 90px;
}

.loader-spinner:before {
  content: "";
  height: 90px;
  width: 90px;
  margin: -15px auto auto -15px;
  position: absolute;
  top: 160px;
  left: calc(50% - 45px);
  border-width: 8px;
  border-style: solid;
  border-color: green #ccc #ccc;
  border-radius: 100%;
  animation: rotation .7s infinite linear;
}


/* Safari */

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

@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

<div style="height: 600px">
    <div class="empty-container"></div>
    <div class="container">
        <div class="component-loader">
            <div class="loader-spinner"></div>
        </div>
    </div>
</div>

3 个答案:

答案 0 :(得分:-2)

.empty-container {
  height: 100px;
  border: 1px solid gray;
}


.container {
  height: calc(100% - 100px);
  border: 1px solid red;      
  position: relative;

}

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

.loader-spinner:before {
  content: "";
  height: 90px;
  width: 90px;
  
  position: absolute;
  top: 0;
  left: 0;
  border-width: 8px;
  border-style: solid;
  border-color: green #ccc #ccc;
  border-radius: 100%;
  animation: rotation .7s infinite linear;
}


/* Safari */

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

@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div style="height: 600px">

  <div class="empty-container"></div>
  <div class="container">
    <div class="component-loader">
      <div class="loader-spinner"></div>
    </div>
  </div>

</div>

答案 1 :(得分:-3)

尝试使用此代码,您的.loader-spinner:before的父元素是.loader-spinner

.empty-container {
  height: 100px;
  border: 1px solid gray;
}

.container {
  height: calc(100% - 100px);
  border: 1px solid red;
  display: flex;
  align-items: centre;
  justify-content: centre;
}

.component-loader {
  position: relative;
}
.loader-spinner{
  position: relative;
   height: 90px;
  width: 90px;
  text-align: center;
  transition: opacity .7s;
  border: 1px solid gray;
}
.loader-spinner:before {
  content: "";
  height: 90px;
  width: 90px;
  
  position: absolute;
  top: 0;
  left: 0;
  border-width: 8px;
  border-style: solid;
  border-color: green #ccc #ccc;
  border-radius: 100%;
  animation: rotation .7s infinite linear;
}


/* Safari */

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

@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div style="height: 600px">

  <div class="empty-container"></div>
  <div class="container">
    <div class="component-loader">
      <div class="loader-spinner"></div>
    </div>
  </div>

</div>

答案 2 :(得分:-3)

*{
  box-sizing: border-box;
}
.empty-container {
  height: 100px;
  border: 1px solid gray;
}

.container {
  height: calc(100% - 100px);
  border: 1px solid red;
  display: flex;
  position: relative;
}
.component-loader{
  border: 1px solid gray;
  position: absolute;
  height: 90px;
  width: 90px;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}
.loader-spinner:before {
  content: "";
  margin: 0;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-width: 8px;
  border-style: solid;
  border-color: green #ccc #ccc;
  border-radius: 100%;
  animation: rotation .7s infinite linear;
}
@-webkit-keyframes rotation {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
@keyframes rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<div style="height: 600px">
  <div class="empty-container"></div>
  <div class="container">
    <div class="component-loader">
      <div class="loader-spinner"></div>
    </div>
  </div>
</div>