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

时间:2018-08-25 11:55:30

标签: 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的底部,实际上甚至没有显示。

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

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

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

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

2 个答案:

答案 0 :(得分:1)

首先,您的某些选择器似乎无法正常工作。其次,您将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>

答案 1 :(得分:0)

尝试一下

function toggleClass() {
  document.getElementById('shopping-cart-body').classList.toggle('open');
}
.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;
  width:100%;
}

.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;
}

.prodcut-image{
  display:inline-block;
}
.product-details{
  display:inline-block;
  vertical-align:top;
}
.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="img.svg" 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://fakeimg.pl/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>   

尽管html结构应进行更新和完善。