CSS显示内联块并用元素填充所有空格

时间:2015-06-29 14:36:44

标签: javascript jquery html css

我有一个带有显示的元素:inline-block,这些元素比其他元素大,在元素之间产生空间,如图所示。

这里是这个例子的小提琴。

http://jsfiddle.net/uu64hyed/

注意:请展开结果窗口宽度以查看完整问题。

CSS

.calenderMonth_header {
    height:35px;
    line-height:35px;
    text-align:center;
    background-color: rgba(221,221,221,1);
    border-bottom: 1px solid rgba(221,221,221,1);
}
.calenderMonth {
    height: 160px;
    width:160px;
    margin:10px;
    border-radius:2px;
    background-color: rgba(238,238,238,1);
    border: 1px solid rgba(221,221,221,1);
    display:inline-block;
    vertical-align:top;
    cursor:pointer;
}
.activeMonth { height:340px; width:340px;}
.calenderMonth:hover { border: rgba(0,153,204,1) 1px solid}

JS

$(document).ready(function(e) {
    var months = [ 
    {month:'January', state:''},
    {month:'Feburary', state:''},
    {month:'March', state:''},
    {month:'April', state:''},
    {month:'December', state:''}];
    $(months).each(function(index, element){
        element.state == 'current' ?
        activeMonth = 'activeMonth':activeMonth = '';
        $('.monthsHolder').append('<article class="calenderMonth '+activeMonth+'">\
        <header class="calenderMonth_header">'+element.month+'</header>\
        </article>');
    });
});

HTML

<section class="app_section app_section_calender hide center">
  <header class="app_section_header">CALENDER
  <section class="monthsHolder"  align="center"></section>
</section>

如何让较小的盒子填满剩余的空间?

3 个答案:

答案 0 :(得分:3)

一种解决方案是将你的月份浮动,但是你必须将中心移动到你的月份并给出一个宽度。像这样:

.calenderMonth {
    height: 160px;
    width:160px;
    margin:10px;
    border-radius:2px;
    background-color: rgba(238,238,238,1);
    border: 1px solid rgba(221,221,221,1);
    display:inline-block;
    vertical-align:top;
    cursor:pointer;
    float: left;
}

.monthsHolder
{
    margin: 0 auto;
    width: 560px;
}

http://jsfiddle.net/7dyt1tLL/1/

答案 1 :(得分:1)

将float:left添加到activeMonth类:

.activeMonth {
  float: left;
  height: 340px;
  width: 340px;
}

JsFiddle:http://jsfiddle.net/ghorg12110/uu64hyed/2/

答案 2 :(得分:0)

内联块不会填充可用空间。它将按下任何不适合宽度的元素。浮动元素将解决此问题。

使用CSS浮动,可以向左或向右推动元素,允许其他元素环绕它。