2

我有 3 列延伸到相等的高度。现在唯一的问题是,如果我只有 2 列,它们会扩展它们的宽度并占据主容器的 100%。所以基本上我需要它们具有相同的宽度,无论是 1、2 还是 3 列。关于如何实现这一目标的任何想法?非常感谢!

小提琴演示

.content > img {
    vertical-align:top;
}
#wrapper {
    height: 100%;
    width: 100%;
    margin: 20px auto;
    display:table;
    overflow: hidden;
    border-spacing: 30px;
}
#wrapper > .col {
    display: table-cell;
    width: 30.3%;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    position: relative;
    height: 100%;
    padding-bottom: 2px;
    border:1px solid #000;
}
#wrapper > .col > .content {
    padding:0 0 35px;
    height:100%;
}
.content {
    margin-bottom:30px;
    position: relative;
}
.content > p {
    vertical-align:top;
}
.content h3 {
    font-size:1.375rem;
    font-weight:400;
    text-align:center;
    margin-bottom: 20px;
    padding: 25px 27px 0;
}
.content p {
    text-align:left;
    padding: 0 27px 30px;
}
.button.moreinfo {
    margin-top: 5px;
    margin-bottom: 0;
    padding-top: 0.5rem;
    padding-right: 0.3rem;
    padding-bottom: 0.5rem;
    padding-left: 0.3rem;
    background-color: #2a2a2a;
    font-size: 0.9rem;
    color: #f39c12;
    font-weight: 400 !important;
}
div.btn-align-bottom {
    position:absolute;
    bottom:50px;
    left:0;
    right:0;
}
.info-box {
    margin-bottom:0;
    margin-top: 15px;
}
.info-box img {
    max-width: 100%;
    max-height: 100%;
    vertical-align: middle;
}
.info-box-inner {
    background: #FFF;
    padding:25px 27px 35px;
    display:inline-block;
    overflow:hidden;
    float:none;
    min-height:270px;
    text-align:center;
    -moz-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    -webkit-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    z-index:1;
}
.info-box-inner p {
    text-align:left;
}
.info-box-inner h3 {
    font-size:1.375rem;
    font-weight:400;
    text-align:center;
}
4

1 回答 1

1

好的,最后这是我的工作 jsFiddle 演示。有关结果,请参见http://jsfiddle.net/rfjg4/10/show/

我将类添加col2到第二个包装器和 CSS 中:

.wrapper.col2:after {
    content: "";
    display: table-cell;
    width: 30.3%;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    height: 100%;
    padding-bottom: 2px;
}

使用上面的代码,一个带有空内容的新单元格被添加到<div class="wrapper col2">标签中。

col2或者,您可以通过使用 CSS 3 选择器来避免:

wrapper:first-child:nth-last-child(3) ~ div::after {
    content: "";
    display: table-cell;
    width: 30.3%;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    height: 100%;
    padding-bottom: 2px;
}

有关示例,请参阅此 jsFiddlehttp://jsfiddle.net/rfjg4/11/show/。(有关选择器的解释,请参阅这个 Stackoverflow 问题Can CSS detect the number of children have a element? )。

一些注意事项:

  • id="wrapper"在您的 jsFiddle 示例中,您使用了两次<div>. 您必须将其更改idclass(并更新您的 CSS)
  • 第 17 行有一个</div>额外的内容(在其他行中也是如此)。这些标签在 jsFiddle 中标记为红色。
  • 您的 CSS 中不需要position:relative;(我在示例中删除了它)。
于 2014-04-09T15:55:54.840 回答