Help with organizing divs with float
Post page with different heights, to present projects and news on a web page.
I try to make a grid where projects are shown in three columns, but sorted from left to right in order of organization rows in Z. So far I have tried two methods and each has its problems.
I tried to do it with css, but I think the solution has to be with java script. I would greatly appreciate some light. I've been trying to do something like that for a long time.
1 Div float left
This would be the best solution, but when the divs have different heights they become disorganized. One solution would be to give all divs the same height, but with this I do not get the effect I want. As a mosaic gallery, or something similar. It creates structures like the one shown in the previous image.
article{
background:#ccc;
width:33%;
float:left; padding:10px;
box-sizing: border-box;
background-clip:content-box;
text-align: center;}
<article> <h3>1</h3> <p>div altura 1 linea</p></article>
<article> <h3>2</h3> <p>div altura 1 linea</p></article>
<article> <h3>3</h3> <p><br> 3<br> lineas</p></article>
<article> <h3>4</h3><br> <p>3 lineas<br> Me gustaría que este div se pegara al (1) superior, no al anterior</p></article>
<article> <h3>5</h3><br><p> lineas<br>lineas</p></article>
<article> <h3>6</h3> <br><p> lineas</p></article>
<article> <h3>7</h3> <br><p> En este caso el alineamiento no queda a la izquierda</p> </article>
<article> <h3>8</h3> <br><p> lineas</p></article>
<article> <h3>9</h3> <br><p> lineas</p></article>
2 Multiple columns
This is the solution that I am currently using, Visually it is what I am looking for, but the order is wrong, since it orders it by columns and in many cases post as # 3 end at the end of the page, so it does not I show in chronological order, not as I pretend. In addition to this we must add that with the responsive the columns vary and the order also.
$('.grid').masonry({
// options
itemSelector: '.grid-item'
});
.contenedor {
column-count: 3;
column-gap: 20px;
}
article {
break-inside: avoid;
background: #ccc;
width: 100%;
float: left;
padding: 10px;
box-sizing: border-box;
background-clip: content-box;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/masonry.pkgd.min.js"></script>
<div class="contenedor">
<article> <h3>1</h3> <p>div altura 1 linea</p></article>
<article> <h3>2</h3> <p>div altura 1 linea</p></article>
<article> <h3>3</h3> <p><br> 3<br> lineas</p></article>
<article> <h3>4</h3><br> <p>3 lineas<br> Me gustaría que este div se pegara al (1) </p></article>
<article> <h3>5</h3><br><p> lineas<br>lineas</p></article>
<article> <h3>6</h3> <br><p> lineas</p></article>
<article> <h3>7</h3> <br><p> En este caso el alineamiento no queda a la izquierda</p> </article>
<article> <h3>8</h3> <br><p> lineas</p></article>
<article> <h3>9</h3> <br><p> lineas</p></article>
</div>
3 Masonry
$('.grid').masonry({
// options
itemSelector: '.grid-item'
});
@import url('https://fonts.googleapis.com/css?family=Roboto:100');
body{background:#333;font-family: 'Roboto', sans-serif; font-weight:bold; font-size:19px;}
h3{ font-size:35px; font-weight:100; margin-left:10px; color:#fff;}
article {
break-inside: avoid;
background: #d9db68;
width: 33%;
float: left;
padding: 10px 7px;
box-sizing: border-box;
background-clip: content-box;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/masonry.pkgd.min.js"></script>
<body>
<h3>Masonry</h3>
<div class="grid">
<article class="grid-item"> <h3>1</h3> <p>div altura 1 linea</p></article>
<article class="grid-item"> <h3>2</h3> <p>div altura 1 linea</p></article>
<article class="grid-item"> <h3>3</h3> <p><br> 3<br> lineas</p></article>
<article class="grid-item"> <h3>4</h3><br> <p>3 lineas<br> Me gustaría que este div se pegara al (1) </p></article>
<article class="grid-item"> <h3>5</h3><br><p> lineas<br>lineas</p></article>
<article class="grid-item"> <h3>6</h3> <br><p> lineas</p></article>
<article class="grid-item"> <h3>7</h3> <br><p> En este caso el alineamiento no queda a la izquierda</p> </article>
<article class="grid-item"> <h3>8</h3> <br><p> lineas</p></article>
<article class="grid-item"> <h3>9</h3> <br><p> lineas</p></article>
</div>
</body>