Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position floated elements directly under each other [duplicate]

I would like to display a row of tow divs next to each other while in the next row, the next div sits directly under the last one. Like this:

enter image description here

Because the layout has to be built into an CMS, I can't put Box 1,3 and 2,4 in a separat div. Is there a way to achieve this kind of behavior without extra wrapping elements? (Normal float behavior doesn't work, display inline/inline-block also doesn't do the trick.) Or is some JavaScript required to build a layout like this?

like image 708
wowpatrick Avatar asked Feb 24 '11 15:02

wowpatrick


People also ask

What is the position of the float?

FLOAT POSITION means a position for the purpose of providing coverage for approved absences of Employees. The position may or may not have a master rotation but will have scheduled hours within the posted and confirmed period in accordance with the letter of appointment.

What are the properties of float positioning?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

How do you keep a floated element from wrapping?

Use "overflow: hidden" to avoid floating elements from wrapping a container's text. We use flexbox for this use case now. The reason behind this is that " overflow: hidden " will give you a new block formatting context.

What is the difference between float and position in CSS?

Floats are meant to be used in images that you want the text to wrap around. Position absolute is meant to place elements with some kind of x and y coordinates.


1 Answers

Because of the different heights, this looks like the problem where I still haven't found a general purpose pure CSS technique to handle it properly, despite trying really hard.

I've posted this answer before: css alignment question

I've given up and used a jQuery plugin to do this in the past for something similar:

jQuery Masonry

A picture is worth a thousand words:

enter image description here

like image 199
thirtydot Avatar answered Nov 15 '22 05:11

thirtydot