0

Scenario:

I am trying to implement masonry layout using CSS column property. (By refering this).

What I tried beforehand

HTML:

<div class="container">
  <div class="child item1">
    <div class="content">
      Some content 1
    </div>
    <div class="placeholder"></div>
  </div>
  <div class="child item2">
    <div class="content">
      Some content 2
    </div>
    <div class="placeholder"></div>
  </div>
  <div class="child item3">
    <div class="content">
      Some content 3
    </div>
    <div class="placeholder"></div>
  </div>
  <div class="child item4">
    <div class="content">
      Some content 4
    </div>
    <div class="placeholder"></div>
  </div>
  <div class="child item5">
    <div class="content">
      Some content 5
    </div>
    <div class="placeholder"></div>
  </div>
</div>

JS:

var children = document.querySelectorAll(".child");

for (var i = 0; children.length; i++) {
  children[i].addEventListener("click", function() {
    var placeholder = this.querySelector(".placeholder");

    placeholder.innerHTML +=
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  });
}

CSS:

.container {
  width: 100%;
  column-count: 2;
  column-gap: 16px;
}

.child {
  width: 100%;
  margin-bottom: 16px;
  border: 1px solid red;
  break-inside: avoid;
  display: inline-block;
}

.content {
  width:100%;
  break-inside:avoid;
}

.placeholder {
  float: left;
}

.child:nth-child(odd) {
  min-height: 100px;
}

.child:nth-child(even) {
  min-height: 40px;
}

Kindly see this Codepen for the implementation of what I want to achieve.

When the tile in the layout is clicked, for example 2nd tile, some content is added to it and it is required to grow in height, and grow at the same position it is rendered initially.

enter image description here

Problem:

When its height grows, the subsequent tile shifts to next column. See 3rd tile's position before and after the content getting added.

Limitation:

  • Using display: inline-block or float does not make any difference.
  • Can not use jQuery or any other plugin.

Question:

What can be done to avoid a tile shifting back or forth with dynamically added content?

Sujit Y. Kulkarni
  • 1,186
  • 3
  • 22
  • 37
  • why is it downvoted? – Sujit Y. Kulkarni May 13 '19 at 12:16
  • because you should share your minimal code that's needed to understand your problem here on this site. – cloned May 13 '19 at 12:33
  • *Links to codepen.io must be accompanied by code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.* Highlighting one word as code does not count as code – Pete May 13 '19 at 12:34
  • Also Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the **shortest code necessary to reproduce it in the question itself**. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Pete May 13 '19 at 12:37
  • @Pete can you please explain how is this a duplicate when the other question has different problem looking for a different solution, completely different scenario than mine, or is it just because both the questions have 'masonry layout' as a common theme? – Sujit Y. Kulkarni May 14 '19 at 13:33
  • 1
    I didn't vote to close as duplicate, I voted to close because of the reasons I pasted above - ask Paulie, he was the one that voted to close due to duplicate. Seeing as though you have now edited, I will remove my downvote - in future, please do not ignore the rules of SO – Pete May 14 '19 at 13:39
  • @Pete thank you. I'll take care of it. – Sujit Y. Kulkarni May 15 '19 at 02:07

0 Answers0