如何使用jquery水平居中操作jquery元素

时间:2017-05-19 21:50:53

标签: javascript jquery html css

我不知道是否与jquery有任何相关性可能会影响..我用jquery操纵我的html元素,我用锚点包装我的html并且我想在水平方向居中,无论我有多少个盒子或者不止一个它必须是我的div的中心

如下图enter image description here

enter image description here

$(".wrapMe").each(function() {
     $(this).wrapAll("<a href='" + $(this).find("a").attr("href") + "' />");
});
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.booking-form {
  width: 1004px;
  margin: 30px auto;
  background: lightblue;
  padding: 30px 0;
}
.booking-form:before, .booking-form:after {
  content: "";
  display: table;
  clear: both;
}

.historyForm {
  width: 950px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 10px;
}
.historyForm:before, .historyForm:after {
  content: "";
  display: table;
  clear: both;
}

.historyBox {
  position: relative;
  width: 32.33%;
  margin-right: 1%;
  float: left;
  padding: 16px 13px;
  background: rgba(0, 0, 0, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  pointer-events: visible;
}
.historyBox:hover {
  background: rgba(0, 0, 0, 0.9);
  border: 1px solid rgba(0, 0, 0, 0.1);
}
.historyBox a {
  color: #FFF;
}
.historyBox p {
  font-size: 12px;
  color: #ccd2d6;
  margin-top: -5px;
}

.historyName {
  font-size: 14px;
  font-weight: bold;
}

.history-icon {
  color: #FFF;
  position: absolute;
  top: 30%;
  right: 5%;
}
<div class="booking-form">
   <div class="historyForm">
                    <div class="historyBox wrapMe">
                        <a href="http://www.google.com" class="historyName">Huzur Royal Otel, Datça</a>
                        <p>20.04.2017 - 23.04.2017, 3 Gece</p><i class="ani-icon-magnifying-glass history-icon"></i>
                    </div>
                    <div class="historyBox wrapMe">
                        <a href="http://www.youtube.com" class="historyName">Huzur Royal Otel, Datça</a>
                        <p>20.04.2017 - 23.04.2017, 3 Gece</p><i class="ani-icon-magnifying-glass history-icon"></i>
                    </div>
                </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

codePen Demo

2 个答案:

答案 0 :(得分:1)

我通过将flexbox添加到.historyForm并从.historyBox

中删除浮动来修复它

相同https://codepen.io/srajagop/pen/xdQxKE

的Codepen
.booking-form{
  width:1004px;
  margin:30px auto;
  background:lightblue;
  padding:30px 0;
  &:before,&:after{
    content:"";
    display:table;
    clear:both;
  }
}
.historyForm{
    width:950px;
    margin-left:auto;
    margin-right: auto;
    margin-top: 10px;

  display: flex;
  flex-flow: row;
  align-items: center;
  justify-content: center;
  &:before,&:after{
    content:"";
    display:table;
    clear:both;
  }
}
.historyBox{
    position: relative;
    margin-right:1%;
    padding:16px 13px;
    background:rgba(0,0,0,.8);
    border:1px solid rgba(0,0,0,.2);
    border-radius:3px;
    pointer-events: visible;
    &:hover{
        background:rgba(0,0,0,.9);
        border:1px solid rgba(0,0,0,.1);
    }
    a{
        color:#FFF;        
    }
      p{
        font-size: 12px;
        color:#ccd2d6;
        margin-top: -5px;
    }
}

答案 1 :(得分:1)

您只能使用CSS来实现此目的。只需将其添加到CSS中即可。

.historyForm {
  display: flex;
  justify-content: center;
}

&#13;
&#13;
/* NEW CSS BEGIN */

.historyForm {
  display: flex;
  justify-content: center;
}

/* NEW CSS END */

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.booking-form {
  width: 1004px;
  margin: 30px auto;
  background: lightblue;
  padding: 30px 0;
}
.booking-form:before, .booking-form:after {
  content: "";
  display: table;
  clear: both;
}

.historyForm {
  width: 950px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 10px;
}
.historyForm:before, .historyForm:after {
  content: "";
  display: table;
  clear: both;
}

.historyBox {
  position: relative;
  width: 32.33%;
  margin-right: 1%;
  float: left;
  padding: 16px 13px;
  background: rgba(0, 0, 0, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  pointer-events: visible;
}
.historyBox:hover {
  background: rgba(0, 0, 0, 0.9);
  border: 1px solid rgba(0, 0, 0, 0.1);
}
.historyBox a {
  color: #FFF;
}
.historyBox p {
  font-size: 12px;
  color: #ccd2d6;
  margin-top: -5px;
}

.historyName {
  font-size: 14px;
  font-weight: bold;
}

.history-icon {
  color: #FFF;
  position: absolute;
  top: 30%;
  right: 5%;
}
&#13;
<div class="booking-form">
   <div class="historyForm">
                    <div class="historyBox wrapMe">
                        <a href="http://www.google.com" class="historyName">Huzur Royal Otel, Datça</a>
                        <p>20.04.2017 - 23.04.2017, 3 Gece</p><i class="ani-icon-magnifying-glass history-icon"></i>
                    </div>
                    <div class="historyBox wrapMe">
                        <a href="http://www.youtube.com" class="historyName">Huzur Royal Otel, Datça</a>
                        <p>20.04.2017 - 23.04.2017, 3 Gece</p><i class="ani-icon-magnifying-glass history-icon"></i>
                    </div>
                </div>
</div>
&#13;
&#13;
&#13;