You can do it this way with jQuery:
$(document).ready(function() {
var divPai = $("#idPai");
if (divPai.height() <= 200) {
divPai.addClass("umaColuna").removeClass("duasColunas");
} else {
divPai.addClass("duasColunas").removeClass("umaColuna");
}
});
Basically what this is going to do is:
If the div id="divPai"
is less than or equal to 400px
height (or whatever the value you want is the maximum value set for the height of the parent div), will add a class called umaColuna
at the #divPai
:
<div id="divPai" class="umaColuna">
and if the value is higher, add a class duasColunas
:
<div id="divPai" class="duasColunas">
Then to make the modification and transaction to divide the content within the #divPai
for him to keep one or two columns depending on this class change above, just create the CSS code for the classes as follows:
/* Personaliza o espaçamento entre as colunas */
#idPai {
-webkit-column-gap: 10px;
-moz-column-gap: 10px;
column-gap: 15px;
}
/* Divide o conteúdo dentro da #divPai em uma coluna */
.umaColuna {
-webkit-column-count: 1;
-moz-column-count: 1;
column-count: 1;
}
/* Divide o conteúdo dentro da #divPai em duas colunas */
.duasColunas {
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
}
.classFilho {
display: inline-block;
width: 100%;
height: 100px;
background-color: #DDD;
margin: 0 2px 15px 2px;
/* O código abaixo evita problemas de posicionamento das divs filhos */
-webkit-column-break-inside: avoid;
-moz-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid;
}
And the HTML code will be something like:
<div id="idPai">
<div class="classFilho">0</div>
<div class="classFilho">1</div>
<div class="classFilho">2</div>
<div class="classFilho">3</div>
</div>
Here’s an online jsFiddle example: http://jsfiddle.net/6sqmsLvv/
Click on the button Add new div Son in the example link, to add new divs
son.
Complement with the code you already have.
– David Schrammel
Edited with the code !
– Pedro Augusto
Using the flex-box property of css3 I managed to do what you need, but the width of the parent div does not accompany the children, it is fixed, maybe to fix this. Jsfiddle: https://jsfiddle.net/devgaspa/zgveahjp/2/ Reference:
– Lucas Fontes Gaspareto