javascript - 将div对齐成一行并固定在底部

标签 javascript 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;
  background-color: #fff;
  transition: right 1s linear;
  right: -400px;
}

.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 .products-container {
  position: relative;
  height: 100%;
  width: 100;
  margin-top: 15px;
  overflow: auto;
}

.cart-preview .body .product {
  display: inline-block;
}

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

.cart-preview .body .products-container>.product-details {
  position: absolute;
  width: 50%;
  float: right;
}

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

.cart-preview .body .checkout {
  position: relative;
  height: 100%;
  width: 100%;
}

.cart-preview .body .checkout>button {
  position: absolute;
  background: black;
  text-align: center;
  color: white;
  bottom: 20px;
  margin-bottom: 50px;
  height: 20px;
  width: 205px;
  margin-left: 100px;
}

.taxes {
  position: absolute;
  bottom: 150px;
  left: 0;
}

.cart-total {
  position: absolute;
  bottom: 100px;
  width: 100%;
}

.taxes {
  position: absolute;
  bottom: 130px;
  width: 100%;
}

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

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

.taxes .value {
  float: right;
}

.taxes .label {
  float: left;
}

<div id="blockcart-wrapper">
  <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/250x100"></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="taxes">
          <span class="label">Taxes</span>
          <span class="value">0</span>
        </div>
        <div class="cart-total">
          <span class="label">Total</span>
          <span class="value">0</span>
        </div>
        <button><a href="#">Checkout</a></button>
      </div>
    </div>
  </div>
</div>





我想实现div'product-details'与图像显示在同一行,并且两者都应占可用位置的50%
另外,我想将checkout div放在整个div的底部,实际上甚至没有显示。

如您所见,我在产品div中使用了display: inline-block,但是它不起作用,我不知道为什么。

因此,我想基本实现:左侧图像,右侧细节。
CSSHTML还有很多,为了更好的可读性,我删除了它们。

整个正文为position: fixed,因为它应始终占据整个页面。

这是MVCE,应在jsfiddle或Codepen中工作。
有人能帮我吗?

最佳答案

首先,您的某些选择器似乎无法正常工作。其次,您将inline-block应用于父级,而实际上应该将其应用于子级。

无论哪种方式,我都认为flexbox是更好的解决方案。我还使图像缩小或扩展以填充可用空间。



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;
  background-color: #fff;
  transition: right 1s linear;
  right: -400px;
}

.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 .products-container {
  position: relative;
  height: 100%;
  width: 100;
  margin-top: 15px;
  overflow: auto;
}

.product {
  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: absolute;
  width: 50%;
  float: right;
}

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

.cart-preview .body .checkout {
  position: relative;
  height: 100%;
  width: 100%;
}

.cart-preview .body .checkout>button {
  position: absolute;
  background: black;
  text-align: center;
  color: white;
  bottom: 20px;
  margin-bottom: 50px;
  height: 20px;
  width: 205px;
  margin-left: 100px;
}

.taxes {
  position: absolute;
  bottom: 150px;
  left: 0;
}

.cart-total {
  position: absolute;
  bottom: 100px;
  width: 100%;
}

.taxes {
  position: absolute;
  bottom: 130px;
  width: 100%;
}

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

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

.taxes .value {
  float: right;
}

.taxes .label {
  float: left;
}

<div id="blockcart-wrapper">
  <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/250x100"></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="taxes">
          <span class="label">Taxes</span>
          <span class="value">0</span>
        </div>
        <div class="cart-total">
          <span class="label">Total</span>
          <span class="value">0</span>
        </div>
        <button><a href="#">Checkout</a></button>
      </div>
    </div>
  </div>
</div>

关于javascript - 将div对齐成一行并固定在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017071/

相关文章:

javascript - _blank 目标表单在 chrome 和 IE 中循环提交

css - 在div中垂直居中放置文本

html - 视差功能 - Html/Css

javascript - 如何在div内放置D3工具提示?

javascript - 清除嵌套的setInterval?

Javascript 函数 "return this"不起作用

javascript - CKEditor在两次Enter键上插入<br/>两次

html - 将div定位在另一个CSS上

html - 如何居中 Font Awesome 图标?

Webkit 按钮 : background color overflows round corners