如何无缝对齐不同大小的块div? [英] How to seamlessly align block divs of different sizes?

查看:30
本文介绍了如何无缝对齐不同大小的块div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 div 框网格"可以这么说.

目前我的 div 框没有正确对齐.div 不同高度的盒子会导致一些 div 之间有很大的垂直空间.div 框也更靠右侧.

我希望 div 框彼此之间保持相同的边距,而不管框的高度如何,并让它们从左侧排成一行 >没错.

我希望 div 框像这样对齐:

这是正在发生的事情的例子:http://jsfiddle.net/P4S8z/

HTML:

.container {位置:相对;向左飘浮;边距:0;}.盒子 {位置:相对;显示:块;向左飘浮;宽度:250px;左边距:1.5em;底边距:0.5em;填充:0 10px 0;颜色:#666;背景:#fff;边框:1px 实心 #d2d2d2;边框半径:3px;}.box h3 {位置:相对;显示:块;高度:20px;行高:1.3em;宽度:260px;边距:0;填充:5px 10px;左:-15px;顶部:8px;颜色:#cfcfcf;文本阴影:0 1px 1px #111;边框顶部:1px 实心 #ccc;边框底部:1px 实心 #ccc;背景:#333;box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);}

<div class="box" style="height:225px;"><h3>胡说八道</h3>

<div class="box" style="height:160px;"><h3>胡说八道</h3>

<div class="box" style="height:200px;"><h3>胡说八道</h3>

<div class="box" style="height:180px;"><h3>胡说八道</h3>

<div class="box" style="height:150px;"><h3>胡说八道</h3>

<div class="box" style="height:170px;"><h3>胡说八道</h3>

解决方案

我能想到的使用纯 CSS 的最简单方法是指定列.

我用一个 div 包围了每个列",并将这些 div 向左浮动.

这是一个更新的小提琴:http://jsfiddle.net/Renson/P4S8z/4/

这应该像您想要的那样保持边距相同

新的 HTML

<div class="子容器"><div class="box" style="height:225px;"><h3>胡说八道</h3>

<div class="box" style="height:180px;"><h3>胡说八道</h3>

<div class="子容器"><div class="box" style="height:160px;"><h3>胡说八道</h3>

<div class="box" style="height:200px;"><h3>胡说八道</h3>

<div class="box" style="height:150px;"><h3>胡说八道</h3>

<div class="box" style="height:170px;"><h3>胡说八道</h3>

添加到 CSS

.subcontainer{向左飘浮;}

I want to create a div box 'grid' so to speak.

Currently my div boxes are not aligning correctly. div boxes of different height cause a large vertical space in between some divs. The div boxes are also going more to the right side.

I want the div boxes to keep the same margin from each other regardless of the height of the boxes and for them to line-up from the left > right.

I want the div boxes to align something like this:

Here's the example of what's happening: http://jsfiddle.net/P4S8z/

HTML:

.container {
  position: relative;
  float: left;
  margin: 0;
}

.box {
  position: relative;
  display: block;
  float: left;
  width: 250px;
  margin-left: 1.5em;
  margin-bottom: 0.5em;
  padding: 0 10px 0;
  color: #666;
  background: #fff;
  border: 1px solid #d2d2d2;
  border-radius: 3px;
}

.box h3 {
  position: relative;
  display: block;
  height: 20px;
  line-height: 1.3em;
  width: 260px;
  margin: 0;
  padding: 5px 10px;
  left: -15px;
  top: 8px;
  color: #cfcfcf;
  text-shadow: 0 1px 1px #111;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  background: #333;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

<div class="container">

  <div class="box" style="height:225px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:160px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:200px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:180px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:150px;">
    <h3>Blah blah</h3>
  </div>

  <div class="box" style="height:170px;">
    <h3>Blah blah</h3>
  </div>

</div>

解决方案

The simplest way I can think of using pure CSS would be to designate columns.

I surrounded each "column" with a div, and floated those divs left.

Here's an updated fiddle: http://jsfiddle.net/Renson/P4S8z/4/

This should keep the margins equal like you wanted

New HTML

<div class="container">
<div class="subcontainer">
    <div class="box" style="height:225px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:180px;">
        <h3>Blah blah</h3>
    </div>
</div>
<div class="subcontainer">
    <div class="box" style="height:160px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:200px;">
        <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:150px;">
    <h3>Blah blah</h3>
    </div>
    <div class="box" style="height:170px;">
    <h3>Blah blah</h3>
    </div>
</div>

Added to CSS

.subcontainer{
    float: left;
}

这篇关于如何无缝对齐不同大小的块div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆