简体   繁体   English

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

[英]CSS display inline-block and fill all spaces with elements

I have elements with a display:inline-block, and these's one elements that's bigger than the rest causing a space between the elements, as in the picture. 我有一个带display:inline-block的元素,这是一个比其他元素大的元素,在元素之间造成了空间,如图所示。

Here's a fiddle to the example. 这是该示例的小提琴。

http://jsfiddle.net/uu64hyed/ http://jsfiddle.net/uu64hyed/

NOTE: Do expand the result window width to see the full problem. 注意:请扩大结果窗口的宽度以查看完整的问题。

CSS 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 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 HTML

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

How do I make the smaller boxes to fill the remaining spaces? 如何制作较小的盒子来填充剩余的空间?

One solution would be to float your months left, but then you'd have to move the centering to your months holder and give a width. 一种解决方案是让您的月份浮动,但随后您必须将居中位置移至月份持有者并指定宽度。 Like this: 像这样:

.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/ http://jsfiddle.net/7dyt1tLL/1/

Add a float: left to your activeMonth class : 添加一个float:到您的activeMonth类:

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

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

Inline block will not fill available spaces. 内联块将不会填充可用空间。 It will push down any elements which cannot fit in the width available. 它将向下推入所有不适合可用宽度的元素。 Floating elements will fix this. 浮动元素将解决此问题。

With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it. 使用CSS float,可以将一个元素向左或向右推,允许其他元素环绕它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM