CSS Flexbox for a Slithery Layout

Struggling to solve a CSS challenge here. I need to arrange an unknown number of items (either spans or divs) in a snake-like pattern within a container.

To clarify, let's say I have 10 items, each 20px wide. I want them to display like this in a 60px wide container:

0 1 2
5 4 3
6 7 8
    9

I've experimented with flexbox in CSS, but so far I can only achieve this layout:

0 1 2
3 4 5
6 7 8
9

If it helps, I know the width of individual items but not the container width. Any guidance would be greatly appreciated.

Thanks in advance!

Answer №1

To achieve the desired outcome of your HTML structure organized with parent, rows, and items, you can apply the CSS property flex-direction: row-reverse to elements with the class .row:nth-child(2n).

.content {
  display: flex;
  flex-direction: column;
}
.row {
  display: flex;
  justify-content: space-between;
}
.row:nth-child(2n) {
  flex-direction: row-reverse;
}
.row:nth-child(2n):last-child .item {
  margin-right: auto;
}
<div class="content">
  <div class="row">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
  <div class="row">
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
  <div class="row">
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
  </div>
  <div class="row">
    <div class="item">10</div>
  </div>
</div>

Answer №2

To solve this issue within the constraints of your markup, you can utilize the structure and float values outlined below.

.left-to-right, .right-to-left {
  width: 60px;
}
.left-to-right div, .right-to-left div {
  width: 20px;
  height: 20px;
  display: inline-block;
}
.right-to-left div {
  float: right;
}
.left-to-right div {
  float: left;
}
<div class="left-to-right">
  <div>0</div><div>1</div><div>2</div>
</div>
<div class="right-to-left">
  <div>3</div><div>4</div><div>5</div>
</div>
<div class="left-to-right">
  <div>6</div><div>7</div><div>8</div>
</div>

Answer №3

To reverse the flow of your content, you can use the CSS property direction: rtl;

Check out a live demo here: http://codepen.io/sol_b/pen/YpjGKK

<div class="container">
  <div class="row">
    <span>1</span>
    <span>2</span>
    <span>3</span>
  </div>
  <div class="row2">
    <span>4</span>
    <span>5</span>
    <span>6</span>
  </div>
  <div class="row">
    <span>7</span>
    <span>8</span>
    <span>9</span>
  </div>

</div>

.row2 {
  direction: rtl;
  float:left;
}

.row {
  clear:both;
}

Answer №4

utilize the flex method

function createItem(){
  var element = document.createElement('div')
  element.className = "item"
  element.style.setProperty("--i", list.children.length + 1)
  element.textContent = list.children.length + 1
  list.append(element)
  element.scrollIntoView()
}
function removeItem() {
  list.querySelector('.item:last-child').remove()
}
html,body{
  margin: 0;
  height: 100%;
  display: flex;
  /* justify-content: center; */
  flex-direction: column;
  align-items: center;
  background: aliceblue;
}
.list{
  display: flex;
  width: 300px;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 20px;
}
.item{
  width: calc( (100% - 30px) / 4 );
  aspect-ratio: 1/1;
  background: royalblue;
  color: #fff;
  font-size: 30px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  order: var(--i);
  filter: hue-rotate(calc(var(--i) * 10deg));
}
.item:nth-child(8n + 5){
  order: calc(var(--i) + 3)
}
.item:nth-child(8n + 6){
  order: calc(var(--i) + 1)
}
.item:nth-child(8n + 7){
  order: calc(var(--i) - 1)
}
.item:nth-child(8n + 8){
  order: calc(var(--i) - 3)
}
.item:nth-child(8n + 5):last-child,
.item:nth-child(8n + 6):last-child,
.item:nth-child(8n + 7):last-child{
  margin-left: auto;
}
.action{
  margin-top: 20px;
}
<div class="list" id="list">
  <div class="item" style="--i: 1">1</div>
  <div class="item" style="--i: 2">2</div>
  <div class="item" style="--i: 3">3</div>
</div>
<div class="action">
  <button onclick="createItem()">Add</button>
  <button onclick="removeItem()">Remove</button>
</div>

Link to Flex Method Example

try utilizing the grid method

function createItem(){
  var item = document.createElement('div')
  item.className = "item"
  item.style.setProperty("--i", list.children.length + 1)
  list.append(item)
  item.scrollIntoView()
}
function removeItem() {
  list.querySelector('.item:last-child').remove()
}
html,body{
  margin: 0;
  height: 100%;
  display: flex;
  /* justify-content: center; */
  flex-direction: column;
  align-items: center;
  background: aliceblue;
}

.list{
  display: grid;
  width: 300px;
  gap: 10px;
  margin-top: 20px;
  counter-reset: count;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-flow: row dense;
}

.item{
  counter-increment: count;
  aspect-ratio: 1/1;
  background: royalblue;
  color: #fff;
  font-size: 30px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  filter: hue-rotate(calc(var(--i) * 10deg));
  
  &::before {
    content: counter(count);
  }
  
  &:nth-child(8n + 5){
    grid-column-end: -1;
  }
  
  &:nth-child(8n + 6){
    grid-column-end: -2;
  }
  
  &:nth-child(8n + 7){
    grid-column-end: -3;
  }
  
  &:nth-child(8n + 8){
    grid-column-end: -4;
  }
}
.action{
  margin-top: 20px;
}

/* Additional CSS Code for Enhancement */
<div class="list" id="list">
  <div class="item" style="--i: 1"></div>
  <div class="item" style="--i: 2"></div>
  <div class="item" style="--i: 3"></div>
</div>
<div class="action">
  <button onclick="createItem()">Add</button>
  <button onclick="removeItem()">Remove</button>
</div>

Link to Grid Method Example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Content does not become interactive upon the initial loading of the page

I've modified a W3 schools slideshow template to use text instead of dots, but I'm encountering two issues 1: The first image doesn't load when the page loads. Although using a class ID and setting it to active fixes this issue, it leads to ...

What might be causing Flex not to function properly on my personalized landing page?

I attempted to create a basic landing page featuring an image background with a logo, button, and a row of 4 images displayed side by side using Flex. However, for some reason, the Flex is not functioning properly for the row of images. Here is the code s ...

The HTML checkbox appears disproportionately large on the user's screen when using Firefox and Chrome, but not when using Internet Explorer

There seems to be a strange issue that I've come across. One of our QA tester's computers is displaying an HTML checkbox in a very large size when using Firefox and Chrome, but it appears normal in size when viewed in IE. Meanwhile, on my comput ...

Ways to eliminate spacing from a bullet point in a list?

Is there a way to eliminate indentation from ul? I attempted adjusting margin, padding, and text-indent to 0, but with no success. It appears that setting text-indent to a negative value works - but is this the only solution for removing the indentation? ...

All pictures aligned with overflow

The first container is working fine, displaying 2 images vertically, but when there are 6 images, it overflows. The problem lies with container 2, where I want to create a horizontal list of images with overflow (only horizontal). This is the current sta ...

CSS designs that adapt flawlessly to high-resolution mobile devices with perfect responsiveness

Currently, I am implementing max-width: 768px to modify the appearance of my website. However, with the increasing number of high-resolution devices available in the market such as 4K mobile phones, I am wondering how I can detect them. Should I consider ...

Merge Various Ng-Loops

I am working with an HTML structure in which each child has varying lengths: <div ng-repeat='element in treeView'> <div ng-repeat='element1 in element.children'> <div ng-repeat='element2 in element1.chil ...

Iframe overlay feature functioning on Chrome but not on IE11

I have a Document viewer with a .less file containing the following styling: div.document-previewer-container { //height: 400px; //width: 300px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; //padding: 5px 2px; > div.document-preview { h ...

Does embedding an Iframe for external files from your server enhance the loading speed of the current page compared to directly loading it on the page?

I'm facing an issue with the loading time of a Facebook post on my webpage index.php. The current method of using the embedded post provided by Facebook is taking too long to load. My solution is to create a separate page, post.php, where only the Fac ...

Is there a clash between ng-if and col col-50?

Currently, I am attempting to create a table using Angular code within HTML. The structure of the table is exactly how I want it to be, and here is the code snippet representing it: <div class="row"> <div class="col col-25">Date</div> ...

Attempting to utilize jQuery for altering the background URL leads to the unexpected removal of the -webkit-background-clip:text; effect

Can the -webkit-background-clip:text; effect be preserved when using jQuery to switch the background url? I am experiencing issues where changing the URL removes the text clipping effect. jQuery(function($) { var whatToSwitch = $('.homepage_ove ...

Text alignment in the horizontal direction does not function as expected within the `<span>` element in Bootstrap 4

The code snippet below is what I am currently working with: <div class="card" > <div class="card-block"> <h4 class="card-title"> <span class="text-left"> ...

Difficulty with collapsing and expanding levels in Bootstrap 4 sidebar navigation

Having some trouble with my vertical Nav created using bootstrap 4. I have 2 levels of lists, but when I click on one dropdown, the other dropdown doesn't close. Here is a link to the issue: https://jsfiddle.net/thilanka/cr0Lfmd1 <ul class="na ...

jQuery animation not executing as expected due to transition issues

I'm looking to create a cool effect where the previous quote disappears and a new one appears when I click on a button, all with a smooth transition effect. In my attempt below using jQuery .animate and opacity property, I'm struggling to make i ...

Transforming Button style in jQuery Mobile version 1.3.0

There are reports of a new function in jQuery Mobile 1.3.0 that allows for dynamically changing button themes using JavaScript: http://jquerymobile.com/blog/2013/02/20/jquery-mobile-1-3-0-released/ However, upon attempting to run the provided code snippe ...

What causes the less-than-perfect smoothness in my CSS3/jQuery transitions and how can I improve their fluidity?

After tirelessly searching for answers all over the web, I've hit a dead end and now I turn to you for assistance. My current dilemma revolves around the lack of smoothness in my animations, whether it be through jQuery .animate or CSS3 transitions. ...

CSS Attribute Selector Wildcard Capture Tool

Suppose I have the following HTML structure: <div class="tocolor-red"> tocolor </div> <div class="tocolor-blue"> tocolor </div> <div class="tocolor-green"> tocolor </div> <div class="tocolor-yellow"> tocolor ...

What are alternative methods for adjusting the value of a CSS property with alternative parameters?

When it comes to using similarities in my code, I often find myself needing a more flexible solution. For example: .position{ position:absolute; right:10px; top:20px; } This is where the idea of using a mixin comes in handy: .position(@absolute:ab ...

Assistance needed for aligning a div to the right using

Check out my website at www.whiterootmedia.com. I am trying to prevent my ads on the right from wrapping and disappearing off the screen when the width is around 800px. I believe I need to create a wrap div container, float my ads div to the right, set a ...

Encountered a problem while executing CSS in Shiny R

I am attempting to incorporate my css file into Shiny R. I have been able to run the application without any issues before adding the style.css file. Below are the steps I have taken: library(shiny) library(tidyverse) library(leaflet) library(leaflet.extra ...