CSS 高级 float /网格布局

标签 css css-float margin grid-layout

我对 CSS 中的网格布局有点着迷。我有一个包装器(1000 像素宽),并希望将 div 与 float:left; 放在一起,并且每个 div 之间的边距为 23px。这可以使用 :nth-child 选择器轻松实现。但是现在我在左上角有一个文本 block ,它的高度是可变的,网格元素的数量也是可变的。

所以我将文本 block 和网格项的 right-margin 设置为 23px。但是当最右边的网格元素有 23px right-margin 时,它会中断到下一行。

我不能在这里使用 nth-child,因为我不知道我会得到多少行的两个元素和多少个三个元素。

我希望有一个纯 CSS 的解决方案来解决我的问题。

更新

这是一个显示我所做尝试的 fiddle :jsfiddle.net

这是我的布局:

+-------------------------------------------+
|+---------------+ +----------+ +----------+|
||               | |          | |          ||
||               | |          | |          ||
||               | |          | |          ||
||               | +----------+ +----------+|
||   Textblock   | +----------+ +----------+|
||               | |          | |          ||
||               | |          | |          ||
||               | |          | |          ||
|+---------------+ +----------+ +----------+|
|+----------+     ^            ^           ^|
||          |  (margin)     (margin)  (no margin)
||          |                               |
||          |                               |
|+----------+                               |
+-------------------------------------------+
^
(no margin)

最佳答案

你可以这样做:http://codepen.io/pageaffairs/pen/svxLg

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.float, .boxes div {background: #f7f7f7; }
.wrap {width: 1000px; margin: 0 auto; padding: 20px 0; background: #30353b;}
.float {float: left; width: 476px; height: 440px; margin: 0 23px 23px 0;}
.boxes {text-align: justify;}
.boxes div {width: 238px; height: 238px; display: inline-block;}
.boxes:after {content: ''; width: 100%; display: inline-block;}

</style>
</head>
<body>

<div class="wrap">
    <div class="float"></div>
    <div class="boxes">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

</div>

</body>
</html>

这个解决方案部分来自另一个 conversation前几天,它引用了这个非常方便的技巧,将 inline-block 元素设置为 text-align: justify:http://www.barrelny.com/blog/text-align-justify-and-rwd/

这是另一个不同大小的版本:http://codepen.io/pageaffairs/pen/JrqIf

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.float {background: #ccc;} 
.boxes div {background: #f7f7f7; }
.wrap {width: 1000px; margin: 0 auto; padding: 20px 0; background: #30353b;}
.float {float: left; width: 318px; height: 400px; margin: 0 23px 23px 0;}
.boxes {text-align: justify;}
.boxes div {width: 318px; height: 200px; margin-bottom: 23px; display: inline-block;}
.boxes:after {content: ''; width: 100%; display: inline-block;}

</style>
</head>
<body>

<div class="wrap">
    <div class="float"></div>
    <div class="boxes">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>

</body>

</html>

编辑:我看到您现在已经发布了一个示例,下面是应用于您的代码的示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>

#main {
    width:1000px;
    position:relative;
    background-color:rgb(88, 88, 88);
}
#welcometext {
    float: left;
    width:318px;
    margin-right:23px;
}
#welcometext p {
    width:100%;
    margin-top:30px;
}
.navelement {
    width:318px;
    height:189px;
    overflow:hidden;
    margin-top:40px;
    border-radius:10px;
    background-color:white;
    display: inline-block;
}

.nav-wrap {text-align: justify;}
.nav-wrap:after {content: ''; width: 100%; display: inline-block;}


</style>
</head>
<body>

<div id="main">
    <div id="welcometext">
        <p>aximinctus incte pa idis sequati velit exero to tem si disci- enderi doloressit odi reptatum dolorrum et et autet aliti- assi rerio corum hitius am quidelibus. 318 px giatemporro esequam, eicias arum doleni vidis pre pa do- lupti orerum qui doluptatiam, voluptae conseritaque sita [...] imuscimin ne niendit.</p>
    </div>
    <div class="nav-wrap">
        <div class="navelement">
        </div>
        <div class="navelement">
        </div>
        <div class="navelement">
        </div>
    </div>
</div>

</body>
</html>

关于CSS 高级 float /网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17010334/

相关文章:

html - 为什么我改造后的汉堡包菜单关闭按钮不均匀?

javascript - 可查看的内容按设定的时间间隔更改 - 但之前的内容会在之后闪烁到 View 中

html - float 框旁边列表的左边距

css - 空白边距(不是图像)

html - 正确问题上的按钮

html - 如何在 Bootstrap 中将列包装在较短的列下

html - 如何将iframe置于最前面

css - 在动态场景中管理 CSS float

html - Bootstrap 下拉菜单项中的右对齐和垂直中间子文本

php - 打开/关闭图例的 JPGraph 底部边距