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%。 此外,我想将结帐 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 - 使用 Javascript 从按钮获取自定义属性

html - 为什么我的 html canvas 游戏不能在 firefox 上运行?

javascript - 文本溢出多个单元格 : Howto ignore text with Hover etc. 。?

html - 减少内容为 "transform:rotated"的 TH 的宽度以避免浪费空间

javascript - 回文程序 - 意外的标识符错误

javascript - 正则表达式拆分字符串并保留分隔符

javascript - 计算旋转线段的 x、y 坐标以在 Canvas 上绘制

javascript - 如何在 Controller 中格式化 JSON 数据

jquery - 仅当 div 在视口(viewport)中时如何启动 svg 动画

jquery:当div达到一定高度时改变背景颜色