Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packing fixed width div's into fluid container

I have simple structure with container and inside boxes:

<div id="container">
    <div class="block"></div>
    // more blocks
    <div class="block"></div>
</div>

What I would like to achieve is to center boxes inside this container but to pack them as much as possible in a one line. The same I can do using JS: http://jsfiddle.net/JhxSd/ but I would like to avoid that, and use only CSS. Is that possible?

like image 438
Paweł Fus Avatar asked Jun 12 '13 16:06

Paweł Fus


3 Answers

@media queries

Use a set of @media queries to define different layouts for the grid based on the current screen size. The only part of the layout that needs to vary is the width of the grid wrapper.

For all practical purposes, this is the only CSS solution available at present. For an explanation of why @media queries are appropriate, and why other available CSS options won't work, see this answer.

JSFiddle Demo

The above demo has @media queries for screen sizes up to 1200px wide (more can be added as needed), and does not use JavaScript. The rendered width of #container is always 75% (not counting the border), and the grid is centered within #container.

Note: This solution requires adding a wrapper div around the blocks. In each @media query, the width of the wrapper is just enough to fit the number of columns appropriate for the current screen size. The fixed wrapper width is what allows the grid as a whole to be centered within #container. If editing the static HTML isn't an option, the wrapper div can be added when the page loads using jQuery.

HTML

<div id="container">
    <div class="grid-wrapper">
        <div class="block"></div>
        ...
    </div>
</div>

CSS

#container {
    width: 75%;
    ...
}
.grid-wrapper {
    margin: 0 auto;
    width: 70px;   /* Default: 1 column */
}
@media (min-width: 200px) {
    .grid-wrapper {width: 140px;}   /* 2 columns */
}
@media (min-width: 290px) {
    .grid-wrapper {width: 210px;}   /* 3 columns */
}
...
like image 90
Matt Coughlin Avatar answered Nov 17 '22 07:11

Matt Coughlin


I hope this will do the trick:

http://jsfiddle.net/CnjZR/1/

<div id="container">
<div id="wrap">
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
</div>

CSS:

#container {
width: 100%;
overflow: hidden;
background: red;
    text-align: center;
}
.block {
    width: 20px;
    height: 20px;
    background: blue;
    float: left;
    margin: 5px;
}

#wrap {
    background: green;
    overflow: hidden;
    display: inline-block;
}
like image 36
Alan Piralla Avatar answered Nov 17 '22 07:11

Alan Piralla


Not too sure if you where looking for something like 'flex-justify' , I added in the demo a turn around based on inline-boxes behavior and text-align values.

edit : point cleared: text-align:center ; is it. http://jsfiddle.net/JhxSd/10/

The point is you should not use float, but display.

Float is not friendly with centering , nor vertical nor horizontal, since it is not standing in the natural flow of the document.

 #container {
    width: 75%;
    border: 1px solid;
    text-align:center;
    overflow:hidden;
    padding:1em 1em 0;
    box-sizing:border-box;
    float:left;
}

#container .block {
    width: 50px;
    height: 50px;
    background-color: blue;
    display:inline-block;
    margin: 10px;
}
like image 27
G-Cyrillus Avatar answered Nov 17 '22 09:11

G-Cyrillus