html - Creating a Masonry Layout using CSS? -


this question has answer here:

i want create layout using css , not jquery or javascript. possible , if so, please direct me right source. :)

i tried hand @ didn't come out well: http://codepen.io/anon/pen/gjzwox

html:

<div class="parent">    <div class="red">   </div>    <div class="blue">   </div>    <div class="red">   </div>     <div class="red">   </div>  </div> 

css:

.parent{   width:330px; }  .red{   float:left;   width:150px;   height:150px;   margin-bottom:10px;   margin-left:10px;   background-color:red; }  .blue{   float:left;   width:150px;   height:300px;   margin-bottom:10px;   margin-left:10px;   background-color:blue; } 

enter image description here

flexbox allows due hability distribute content , gaps.

flexbox use not easy not imposible. here help: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

.parent{   background: green;   width:330px;   height: 330px;   display: flex;   flex-flow: column wrap; }  .red, .blue{   margin: 10px; } .red{   flex: 1 1 100px;;   background-color:red; }  .blue{   flex: 2 2 150px;   background-color:blue; } 

here pen you:http://codepen.io/vandervals/pen/ovnvae?editors=110

so, explanation of happening here follows:

  1. the parent must have display: flex.

  2. you have tell flow direction, in case want columns.

  3. for items, have define flex properties. in case, want red smaller, proportion 1 , tendency 100px if can. blue has double "weight" , tendency 150px.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -