假设我有这个代码。使用css的定位元素
<div class="big_div" >
<div class="small_div" id="small_div_1" ></div>
<div class="small_div" id="small_div_2" ></div>
<div class="small_div" id="small_div_3" ></div>
<div class="small_div" id="small_div_4" ></div>
</div>
.big_div{
width:1000px;
height:1000px;
background-color:green;
}
.small_div{
width:50px;
height:50px;
position:absolute;
top:100px;
background-color:red;
}
#small_div_1{
left:200px;
}
好了,我有4个绿色面红色正方形,第一方(#small_div_1
)是从表面的左侧200像素,并且所有都是从100像素表面的顶部上。我可以如何让其他3个正方形放在他们以前的兄弟(旁边#small_div_2
- 250px,左边#small_div_3
- 300px,左边#small_div_4
- 350px)旁边,而不需要分别定位每个正方形。因为当我有4个方格时这不是问题,但是当我有100个方格时,这是一个问题。有没有办法使用Sass或类似的东西,也许是JavaScript?
使用Flexbox的。太棒了。看看这个:** [Flexbox指南](https://css-tricks.com/snippets/css/a-guide-to-flexbox/)** – jherax
在任何情况下,绝对定位都在桌子外面如果您正在寻找具有未知数量元素的可扩展解决方案。 – Serlite
[用CSS定位另一个元素的元素]可能的重复(http://stackoverflow.com/questions/32830701/position-elements-around-another-with-css) – jherax