html - 为什么我的 div 框在我向左设置 float 时向下堆叠?

标签 html css

我在一家网上商店工作,我想要一个产品字段,里面有产品,我希望它们向左浮动,所以当我添加一个产品时,它会与其他产品一起向左浮动。

问题

当我添加一个 float: left 时,它向下堆叠而不是向左浮动,我不明白为什么?

示例

Example picture

代码:

.product_gallery {
  margin: 0 auto;
  width: 1000px;
  list-style: none;
  float: right;
  position: relative;
}

.product_gallery li {
  float: right;
  width: 300px;
  margin-right: 50;
  border: 1px solid #999;
}

.product_gallery li img {
  width: 300px;
  height: 200px;
  float: right;
}

.product_description {
  position: absolute;
  width: 300px;
  height: 200px;
  background: rgba(0, 0, 0, 0.6);
  opacity: 0;
  transition: 0.5s ease-in;
}

.product_gallery li:hover .product_description {
  opacity: 0.7;
}

.product_description p {
  padding: 10px;
  color: #fff;
  font-size: 18px;
}

.product_information {
  height: 40px;
  background: blue;
  width: 100%;
  float: left;
}

.product_information h4 {
  float: left;
  color: #fff;
  width: 70%;
  margin: 10px;
}

.product_information span {
  float: right;
  color: #fff;
  margin: 5px;
  font-size: 20px;
}

.buy_now {
  background: blue;
  color: #fff;
  width: 85px;
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  text-decoration: none;
  float: right;
  height: 18px;
}
<ul class="product_gallery">
  <li>
    <img src="images/test.png">
    <div class="product_information">
      <h4>AWP ASIIMOV</h4>
      <span>230:-</span>
    </div>
    <div class="product_description">
      <p>Awp asiimov är ett fint skin om man gillar att spela awp :)</p>
      <a href="#" class="buy_now">BUY NOW</a>
    </div>
  </li>
</ul>

最佳答案

如果我真的理解你,你希望你的 div 是并排的。现在不是了,因为第一个 div 有 width: 100%,所以第二个 div 到下一行。

使用 div width: 300px 将其宽度减小到可以包含在一行中的值。

关于html - 为什么我的 div 框在我向左设置 float 时向下堆叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43034160/

相关文章:

javascript - javascript警报中的html实体?

html - 简单的单页应用程序模板 Bootstrap

javascript - 有垂直或水平滚动条吗?

html - 占位符 z-index firefox 问题

HTML/CSS : Float Left

css - 表格行上的背景图案重复

html - 当内容溢出页面时使非环绕 div 展开

javascript - 将文件中的图像添加到 HTML5 Canvas

html - CSS 3 - 缩放过渡在谷歌浏览器中快速返回

javascript - 单击按钮消失并显示隐藏字段