div内的中心装载机

时间:2017-08-21 10:17:03

标签: html css

我有这个装载机:

.loader_ajax_small {
  border: 2px solid #f3f3f3 !important;
  border-radius: 50%;
  border-top: 2px solid #2D2D2D !important;
  width: 29px;
  height: 29px;
  -webkit-animation: spin_loader_ajax_small 2s linear infinite;
  animation: spin_loader_ajax_small 2s linear infinite;
}

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

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

但我无法理解如何在div中水平居中,例如:

enter image description here

正如你所看到的那样在左边但是我希望它居中,我怎么能这样做?

3 个答案:

答案 0 :(得分:6)

margin: 0 auto;中为父级div中心添加了.loader_ajax_small

.loader {
  border: 1px solid #ccc;
  padding: 20px;
}
.loader_ajax_small {
  border: 2px solid #f3f3f3 !important;
  border-radius: 50%;
  border-top: 2px solid #2D2D2D !important;
  width: 29px;
  height: 29px;
  margin: 0 auto;
  -webkit-animation: spin_loader_ajax_small 2s linear infinite;
  animation: spin_loader_ajax_small 2s linear infinite;
}

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

@keyframes spin_loader_ajax_small {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
<div class="loader">
  <div class="loader_ajax_small"></div>
</div>

display: flex;

.loader {
  border: 1px solid #ccc;
  padding: 20px;
  display: flex;
  justify-content: center;  
}
.loader_ajax_small {
  border: 2px solid #f3f3f3 !important;
  border-radius: 50%;
  border-top: 2px solid #2D2D2D !important;
  width: 29px;
  height: 29px;
  -webkit-animation: spin_loader_ajax_small 2s linear infinite;
  animation: spin_loader_ajax_small 2s linear infinite;
}

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

@keyframes spin_loader_ajax_small {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
<div class="loader">
  <div class="loader_ajax_small"></div>
</div>

答案 1 :(得分:1)

margin:0 auto;属性中添加.loader_ajax_small

&#13;
&#13;
.loader_ajax_small {
  border: 2px solid #f3f3f3 !important;
  border-radius: 50%;
  border-top: 2px solid #2D2D2D !important;
  width: 29px;
  height: 29px;
  margin:0 auto;
  -webkit-animation: spin_loader_ajax_small 2s linear infinite;
  animation: spin_loader_ajax_small 2s linear infinite;
}

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

@keyframes spin_loader_ajax_small {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
&#13;
<div class="loader_ajax_small"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

将您的加载器margin设置为auto 像这样的东西

margin:10px auto;