javascript - 有没有办法在一个 div 容器中放置 n 个 div

标签 javascript jquery html css

有没有办法在一个 div 容器中放置 n 个 div,这样每个 div 单元格就不会使用 css 和 js 相互碰撞。 我们需要在不改变 height 和 top 值的情况下设置 left 值。改变单元格宽度就可以了。

请检查此代码示例。

.container
	{
	  width : 400px;
	  height : 1000px;
	  background : #000;
	  position : relative;
	}

	.cell
	{
	  position  :absolute;
	  background : yellow;
	  border: 2px solid red;
	}
<div class="container">
   <div class="cell" style="top:50px;width:100%;height:100px"></div>  
   <div class="cell" style="top:150px;width:50%;height:50px"></div>
   <div class="cell" style="top:150px;width:50%;height:50px;"></div>
   <div class="cell" style="top:230px;width:33.33%;height:50px;"></div>
   <div class="cell" style="top:230px;width:33.33%;height:50px;"></div>
   <div class="cell" style="top:230px;width:33.33%;height:50px;"></div>
</div>

预期输出:

enter image description here

最佳答案

如果所有的单元格都应该按照它们提到的顺序一个接一个地堆叠,那么只需将它们的显示属性设置为“ block ”

.container {
  width: 400px;
  height: 1000px;
  background: #000;
  position: relative;
}

.cell {
  display: block;
  background: yellow;
  border: 2px solid red;
}
<div class="container">
  <div class="cell" style="width:100%;height:100px;"></div>
  <div class="cell" style="width:50%;height:50px;"></div>
  <div class="cell" style="width:50%;height:50px;"></div>
  <div class="cell" style="width:33.33%;height:50px;"></div>
  <div class="cell" style="width:33.33%;height:50px;"></div>
  <div class="cell" style="width:33.33%;height:50px;"></div>
</div>

关于javascript - 有没有办法在一个 div 容器中放置 n 个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43275571/

相关文章:

javascript - 带函数的setTimeout和不带函数的setTimeout有什么区别?

javascript - 使用Html.BeginForm/Ajax.BeginForm的FileUpload控件不起作用。(IE中拒绝html文件访问)。Css样式上传控件按钮

android - 如何在 jqm 中居中图像? (使用马赛克插件)

html - 绝对定位元素和响应式设计

javascript - 使用 Javascript 获取和设置动态创建的 HTML 输入的值

html - 之前和之后的 Css 形状

javascript - $window.on() 在 ES6 javascript 中不起作用

javascript - 如何在HTML中插入两个js文件?

javascript - 如何删除没有值(value)的选项选择?

javascript - 如何修复给定行以避免使用 .remove 函数删除