【问题标题】:CSS Advanced float / grid layoutCSS 高级浮动/网格布局
【发布时间】:2013-06-09 22:44:34
【问题描述】:

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

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

我不能在这里使用nth-child,因为我不知道两个项目有多少行以及三个项目中有多少行。

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

更新

这是一个 Fiddle,它显示了我所做的尝试:jsfiddle.net

这是我的布局:

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

【问题讨论】:

  • 你能分享你试过的代码吗?
  • 可以在文本容器上使用min-height 或max-height 和overflow-y。
  • 对不起,忘记小提琴链接,看我的更新

标签: css css-float margin grid-layout


【解决方案1】:

你可以这样做: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>

【讨论】:

  • 哇!很好的答案!唯一的问题是,如果最后一行有两个元素,它们会被对齐并坚持左/右。您链接中文章的作者使用了占位符。但由于内容来自 CMS,我对内容没有任何影响。
  • 嗯。我想在紧要关头你可以使用 JS 来计算盒子的数量并根据需要添加额外的占位符。
  • 好的...所以我看到没有更好的解决方案。我会尝试连接到 CMS。我确信有一种方法可以获取元素的数量。再次感谢,干得好!
  • 没有问题。抱歉,我无法提供更好的选择。我想如果你知道有一个像这样的最终搁浅的盒子,你可以这样做:.boxes div:last-child {position: relative; left: -341px;}
  • 嗯...您的解决方案非常好!我已经尝试过你所做的。但是,如果最后一行有两个项目,并且:last-child 也适用于最后一行已满时,这也无济于事......我很确定我会很高兴计算项目......我会看到。
猜你喜欢
  • 2011-04-28
  • 2017-11-13
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多