Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get divs to wrap horizontally

I'm making a blog in which the results of a search for articles will be contained in divs. The layout of my website is all horizontal (i.e. the articles scroll horizontally).

Making a single line of divs is easy but that not what I want. It will be easier if I explain with a diagram. Here's how I want the divs to be:

 ______________   ______________   ______________
|    Div 1     | |    Div 4     | |    Div 7     |
|______________| |______________| |______________|
 ______________   ______________   ______________
|    Div 2     | |    Div 5     | |    Div 8     |
|______________| |______________| |______________|
 ______________   ______________ 
|    Div 3     | |    Div 6     |
|______________| |______________|

But if the window is taller then there's more space vertically, which should give:

 ______________   ______________ 
|    Div 1     | |    Div 5     |
|______________| |______________|
 ______________   ______________ 
|    Div 2     | |    Div 6     |
|______________| |______________|
 ______________   ______________ 
|    Div 3     | |    Div 7     |
|______________| |______________|
 ______________   ______________
|    Div 4     | |    Div 8     |
|______________| |______________|

This is what I mean by horizontal wrapping. In short, the divs take as much vertical space as then can before occupying a new column.

I was hoping this was possible with pure html/css but I'm having a feeling that it's not possible without a little bit of javascript.

like image 953
Pharoah Jardin Avatar asked Aug 01 '17 16:08

Pharoah Jardin


People also ask

How do I wrap the content of a div?

If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.

How do you force flex-wrap?

Making things wrap If you want to cause them to wrap once they become too wide you must add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap in the container.

What is flex and flex-wrap?

The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.

What is flex-wrap in html?

Definition and Usage The flex-wrap property specifies whether the flexible items should wrap or not. Note: If the elements are not flexible items, the flex-wrap property has no effect.


2 Answers

You can use Flexbox for this. You need to set fixed height on parent element or 100vh and flex-direction: column.

* {
  box-sizing: border-box;
}
ul {
  display: flex;
  height: 100vh;
  flex-direction: column;
  align-items: flex-start;
  align-content: flex-start;
  flex-wrap: wrap;
  list-style: none;
  padding: 0;
}

li {
  padding: 20px 60px;
  border: 1px solid black;
}
<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>
like image 106
Nenad Vracar Avatar answered Oct 16 '22 07:10

Nenad Vracar


I think in pure css this can only be done with flexbox:

  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: flex-start;

Live-Example (change the height of the container):

https://codepen.io/MattDiMu/pen/zdBxad

edit: Yes, using 100vh for the size of the container is the much more elegant solution :)

like image 40
MattDiMu Avatar answered Oct 16 '22 06:10

MattDiMu