javascript - Is it possible for flex items to align tightly to the items above them? -
this is, in effect, pinterest layout. however, solutions found online wrapped in columns, means container inadvertently grows horizontally. not pinterest layout, , not work dynamically-loaded content.
what want have bunch of images of fixed width , asymmetrical height, laid out horizontally wrapping in new row when limits of fixed-width container met:
can flexbox this, or have resort js solution masonry?
flexbox "1-dimensional" layout system: can align items along horizontal or vertical lines.
a true grid system "2-dimensional": can align items along horizontal , vertical lines. in other words, cells can span across columns , rows, flexbox cannot do.
this why flexbox has limited capacity building grids. it's reason why w3c has developed css3 technology, grid layout (see below).
in flex container flex-flow: row wrap
, flex items must wrap new rows.
this means a flex item cannot wrap under item in same row.
notice above how div #3 wraps below div #1, creating new row. cannot wrap beneath div #2.
as result, when items aren't tallest in row, white space remains, creating unsightly gaps.
image credit: jefree sujit
column wrap
solution
if switch flex-flow: column wrap
, flex items stack vertically , grid-like layout more attainable. however, column-direction container has 3 potential problems right off bat:
- it expands container horizontally, not vertically (like pinterest layout).
- it requires container have fixed height, items know wrap.
- as of writing, has deficiency in major browsers the container doesn't expand accommodate additional columns.
as result, column-direction container may not feasible in many cases.
other solutions
add containers
in first image above, consider wrapping items 2 , 3 in separate container. new container becomes sibling item 1. done.
one downside worth highlighting: if you're wanting use
order
property re-arrange layout in media queries, method may eliminate option.-
masonry javascript grid layout library. works placing elements in optimal position based on available vertical space, sort of mason fitting stones in wall.
source: http://masonry.desandro.com/
how build site works pinterest
[pinterest] cool site, find interesting how these pinboards laid out... purpose of tutorial re-create responsive block effect ourselves...
source: https://benholland.me/javascript/2012/02/20/how-to-build-a-site-that-works-like-pinterest.html
css grid layout module level 1
this css module defines two-dimensional grid-based layout system, optimized user interface design. in grid layout model, children of grid container can positioned arbitrary slots in predefined flexible or fixed-size layout grid.
Comments
Post a Comment