4

In the following jsFiddle, the width of the container div is 100% when there is two rows, and less than 100% when there is only one (i.e. it wraps the children).

http://jsfiddle.net/fpooemgj/

I'd like for the container div photo_stream_wrapper to be as tight as possible around the children. How can I achieve that?

One row: One row

Two rows: Two rows

I want this instead of the last image. I want to eliminate the extra grey space: enter image description here

Bathlamos
  • 312
  • 4
  • 10

2 Answers2

0

Interesting question. You would certainly expect the floated container div to be no more than the width of the children (as it is with only one row).

If you know the number of items in each row, wrapping the items with a row div seems to work:

<div class="photo_stream_wrapper">
    <div>   <!-- "row" div -->
        <div class="photo"></div>
        <div class="photo"></div>
        <div class="photo"></div>
    </div>
    <div>   <!-- "row" div -->
        <div class="photo"></div>
    </div>
</div>
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
0

I had a very similar problem, which can be referenced in my post:

How can I make a container div the same width as it's floating children and center it when there are multiple rows of floating children?

In my case I have something along the lines of this:

<div class="centered">
  <div class="container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
  </div>
</div>

It looks like jQuery is the answer. In order to "dynamically" set the width of the containing div, we need to calculate the width on a screen resize event via jQuery. We can use the width of the window and the width of the children to calculate the width of the inner container.

Here is the fiddle:

var resizeContainerDiv = function() {
  var screenWidth = $(window).width();
  var childWidth = $(".child").outerWidth();
  var innerContainerWidth = Math.floor(screenWidth / childWidth) * childWidth;

  $('.container').css('width', innerContainerWidth);
}

$(window).on("load", resizeContainerDiv);

$(window).on("resize", resizeContainerDiv).resize();


.centered {
  text-align: center;
}

.container {
  background: red;
  padding: 10px;
  display: inline-block;
}

.child {
  width: 100px;
  height: 100px;
  float: left;
}

.child:nth-child(even) {
  background: green;
}

.child:nth-child(odd) {
  background: blue;
}


<div class="centered">
  <div class="container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
  </div>
</div>

https://jsfiddle.net/02arvnLx/1/

Community
  • 1
  • 1
Justin B
  • 110
  • 1
  • 8
  • Even though I'm not too hot for JavaScript solutions in this case, it does work! Thanks for reviving this question! – Bathlamos Aug 24 '16 at 20:48
  • I didn't want to have to use JavaScript for this either, but it was the only way I found to make it work. No problem! – Justin B Aug 31 '16 at 15:49
  • Note that if you add true to .outerWidth(), the width includes the margin (e.g. .outerWidth(true)). By default, .outerWidth() only handles border and padding. – Justin B Aug 31 '16 at 15:59