1

How to use CSS to place items into columns without changing the order of the html tags?

<ul>
  <li class="column-2">1</li>
  <li class="column-1">2</li>
  <li class="column-1">3</li>
  <li class="column-3">4</li>
  <li class="column-3">5</li>
</ul>

Should look like this:

enter image description here

As items are added they should fall into their specified column (Tetris style).

7
  • "as list items are added" - why not create 3 <div> columns of 33% width and prepend the items to the correct div? Commented May 22, 2016 at 5:08
  • 2
    If you can support HTML5, you could use the flex layout, with the order attribute. See this site for more information and try it Commented May 22, 2016 at 5:27
  • the picture you have shown is what you want rite ? Commented May 22, 2016 at 5:28
  • u may want to explain a bit more so i can help you :) Commented May 22, 2016 at 5:46
  • With float:left, fixed width, and setting margin-left to something positions it in the correct column. But the margin-left leaves empty boxes of whitespace. Commented May 22, 2016 at 5:53

1 Answer 1

1

CSS grid with grid-auto-flow: column; achieves the effect I was looking for.

.wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-auto-flow: column;
}

.column-1 {
  grid-column-start: 1;
}

.column-2 {
  grid-column-start: 2;
}

.column-3 {
  grid-column-start: 3;
}
<div class="wrapper">
  <div class="column-2">1</div>
  <div class="column-1">2</div>
  <div class="column-1">3</div>
  <div class="column-3">4</div>
  <div class="column-3">5</div>
  <div class="column-1">6</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.