div对齐/对齐内容未按预期工作

时间:2018-08-30 05:29:51

标签: html css

我有以下HTML/CSS/JS

function toggleClass() {
  document.getElementById('shopping-cart-body').classList.toggle('open');
}
.cart-preview {
  float: right;
  position: relative;
}

.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
  text-decoration: none;
  color: inherit;
}

.cart-preview .header {
  display: block;
  font-weight: bold;
  border: 1px solid #808080;
  padding: 5px;
  cursor: pointer;
  background-color: #fff;
}

.cart-preview .body {
  visibility: visible;
  position: fixed;
  height: 100%;
  top: 0;
  width: 400px;
  z-index: 100;
  background-color: #fff;
  transition: right 1s linear;
  right: -410px;
}

.cart-preview .body.open {
  visibility: visible;
  transition: right 1s linear;
  right: 0px;
}

.cart-preview .body .shooping-cart-body {
  font-family: 'sans-serif';
  width: 100%;
  text-align: center;
}

.cart-preview .body .close {
  float: left;
}

.cart-preview .body .shopping-cart-header {
  font-family: 'IBMPlexSans-Bold.woff' font-size: 45px;
  margin-top: 40px;
  text-align: center;
}

.cart-preview .body .products-container {
  height: calc(73vh - 20px);
  width: 100%;
  margin-top: 15px;
  overflow: auto;
}

.product {
  position: relative;
  display: flex;
}

.product>div {
  width: 50%;
}

.product .prodcut-image {
  margin-right: 20px;
}

.product img {
  width: 100%;
  height: auto;
}

.cart-preview .body .products-container>.product-image {
  position: absolute;
  width: 50%;
  left: 0;
}

.cart-preview .body .products-container>.product-details {
  position: relative;
  width: 50%;
  float: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
}

.name-header {
  position: absolute;
  top: 0;
}

.cart-preview .body .products-container .color-circle:before {
  color: blue;
  content: ' \25CF';
  font-size: 30px;
}

.cart-preview .body .checkout {
  position: relative;
  bottom: 0;
  height: 20vh;
  width: 100%;
  background: rgba(0, 0, 0, 0.08);
}

.cart-preview .product {
  margin-top: 10px;
}

.product-quantity-details {
  position: absolute;
  align-items: center;
  display: inline-flex;
}

.product-quantity-details {
  position: absolute;
  top: 25%;
}

.product-quantity-details .quantity {
  border: 2px solid black;
  margin-right: 10px;
  background: white;
  width: 36px;
  height: 36px;
  font-size: 15px;
  line-height: 35px;
  color: black;
}

.name-header {
  position: absolute;
  text-align: left;
  top: 0;
}

.price-open {
  position: absolute;
  bottom: 0;
}

.product-quantity-details .quantity:after {
  content: 'x'
}

.cart-preview .body .checkout>button {
  position: absolute;
  background: black;
  text-align: center;
  vertical-align: middle;
  color: white;
  line-height: 14px;
  bottom: 30px;
  height: 20px;
  width: 205px;
  left: 25%;
}

.meta-data-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.meta-data-wrap>hr {
  margin-left: 20px;
  margin-right: 20px;
  background: black;
  height: 1px;
}

.cart-total .value {
  float: right;
}

.cart-total .value:after {
  content: '€'
}

.cart-total .label {
  float: left;
}

.taxes {
  margin-left: 20px;
  margin-right: 20px;
  margin-top: 10px;
}

.cart-total {
  margin-left: 20px;
  margin-right: 20px;
}

.taxes .value {
  float: right;
}

.taxes .label {
  float: left;
}

.taxes .value:after {
  content: '€';
}

.cart-preview.cart-overview {
  width: 100%;
  position: inherit;
}

.cart-preview.cart-overview .body {
  display: block;
  position: inherit;
  width: 100%;
}

.cart-preview .header> :first-child {
  float: left;
}

.cart-preview .header> :last-child {
  float: right;
}

.cart-preview .header::after,
.cart-preview .cart-totals>div::after {
  clear: both;
  content: "\A0";
}

.cart-preview .body {
  border: 1px solid #808080;
  padding: 2px;
}
<div class="blockcart cart-preview">
  <div class="header">
    <a rel="nofollow" href="#">
      <img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">

    </a>
  </div>
  <div class="body" id="shopping-cart-body">
    <div class="close"><a href="" onclick="toggleClass()">X</a></div>
    <ul>
    </ul>
    <div class="shopping-cart-header">CART</div>
    <div class="products-container">
      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>
      </div>


      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>
      </div>

      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>
      </div>

      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>
      </div>

      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>
      </div>

      <div class="product">
        <span class="prodcut-image"><img src="https://via.placeholder.com/80x80"></span>
        <div class="product-details">
          <div class="name-header">NAME</div>
          <div class="product-quantity-details">
            <span class="quantity">QTY</span>
            <span class="color-circle"></span>
            <span class="color">COLOR</span>
          </div>
          <div class="price-open">
            <span class="product-price">XX.XX</span>
            <span class="product-link"><a href="#">öffnen</a></span>
          </div>
        </div>

      </div>

    </div>
    <div class="checkout">
      <div class="meta-data-wrap">
        <div class="taxes">
          <span class="label">Taxes</span>
          <span class="value">0</span>
        </div>
        <hr>
        <div class="cart-total">
          <span class="label">Total</span>
          <span class="value">0</span>
        </div>
      </div>
      <button><a href="#">Checkout</a></button>
    </div>
  </div>
</div>

您可以在代码笔中看到预期的结果:
https://codepen.io/seppl2202/pen/WgQPvP
但是,当我将此CSS与真实的图像和字体一起使用时,结果如下:
enter image description here

真实示例中的图片为125x125,因此缺少“空格”应该不是问题。实际字体为IBMPlex及其derivates

我真的不知道为什么这个div在右边这么粘。 div如何像codepen中那样对齐?

有人可以帮我吗?

0 个答案:

没有答案