Fluid width with equally spaced DIVs

前端 未结 7 1750
耶瑟儿~
耶瑟儿~ 2020-11-22 01:21

I have a fluid width container DIV.

Within this I have 4 DIVs all 300px x 250px...

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 02:00

    in jQuery you might target the Parent directly.

    THIS IS USEFUL IF YOU DO NOT KNOW EXACTLY HOW MANY CHILDREN WILL BE ADDED DYNAMICALLY or IF YOU JUST CAN'T FIGURE OUT THEIR NUMBER.

    var tWidth=0;
    
    $('.children').each(function(i,e){
    tWidth += $(e).width();
    
    ///Example: If the Children have a padding-left of 10px;..
    //You could do instead:
    tWidth += ($(e).width()+10);
    
    })
    $('#parent').css('width',tWidth);
    

    This will let the parent grow horizontally as the children are beng added.

    NOTE: This assumes that the '.children' have a width and Height Set

    Hope that Helps.

提交回复
热议问题