I use the jQuery Masonry library for the gallery. However, there is the centering of the first element top:0; left:0; top:0; left:0; If you put it in the center, then all other photos will go the same. How to write a check for js / jQuery, if div 1 (one), then use for it styles that align it in the center?

 <div style="position:relative; min-width:80%; clear:both; height:auto; margin:0 auto; padding-top:2%;padding-bottom:16%;"> <div class="grid"> <div class="grid-sizer"></div> <div class="grid-item marg_center"> <a class="example-image-link" href="http://timworld.info/wp-content/uploads/2016/05/Bauhaus_in_Tel_Aviv_7_120x80.jpg" data-lightbox="example-set" > <img class="example-image" src="http://timworld.info/wp-content/uploads/2016/05/Bauhaus_in_Tel_Aviv_7_120x80.jpg" alt=""/> </a> </div> </div> </div> 
  • 3
    if ($('div.grid').length == 1) { сделать что то } ? :) - Vasily Barbashev
  • Thank you, I thought about how to form a condition, it helped. if ($ ('div.grid-item'). length == 1) {$ ('. grid'). css ({'margin': '0 auto', ' width ':' 20% '}); $ ('. grid-item') .css ('width', '100%'); } - BonBonSlick

1 answer 1

Try using the element.childNodes.length function. If it returns 0, then there are no blocks in the block, and then apply the necessary styles.