Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put content below the shorter column

Tags:

html

css

layout

enter image description here

I have a "dashboard" type layout in which I have two columns of differing heights. I have a third item that I would like to add to whichever is the shortest column (see attached screenshot).

In the first example, column 1 is shorter, so we add item 3 below that, but if column 2 is shorter it should be there.

I thought I should use floats, however it doesn't run right.

Can someone please enlighten me?

like image 608
James Holland Avatar asked Sep 03 '15 15:09

James Holland


People also ask

How do I put text in columns in HTML?

Defining columns in HTML The following syntax is used to add columns in HTML. <div class="row"> tag is used to initialize the row where all the columns will be added. <div class="column" > tag is used to add the corresponding number of columns.

What is the default space between columns?

The browser's default spacing is used between columns. For multi-column layout this is specified as 1em . For all other layout types it is 0.


2 Answers

You can float the three items left, right and left:

#wrapper {
  box-shadow: 0 0 0 1px #CCC;
}
/* clearfix */
#wrapper::after {
  content: "";
  display: block;
  clear: both;
}
.item {
  width: calc(50% - 1em);
  margin: .5em;
}
/* floats */
#item-1 {
  float: left;
  background-color: #080;
}
#item-2 {
  float: right;
  background-color: #F00;
}
#item-3 {
  float: left;
  background-color: #FF0;
}
/* demo */
textarea {
  box-sizing: border-box;
  margin: 1em;
  width: calc(100% - 2em);
  resize: vertical;
}
<p>Resize the boxes by dragging the resize handle of the textarea<br> The yellow box will position itself below the shorter of green and red box</p>
<div id="wrapper">
  <div class="item" id="item-1"><textarea rows="4">1</textarea></div>
  <div class="item" id="item-2"><textarea rows="9">2</textarea></div>
  <div class="item" id="item-3"><textarea rows="4">3</textarea></div>
</div>
like image 160
Salman A Avatar answered Oct 11 '22 23:10

Salman A


I don't think it is possible via a pure CSS solution. First the DOM must be render (the 2 boxes and their contents are rendered) and thus you have the exact height of each box. Then you need to calculate (Javascript ? ) which is the shortest and then append the 3rd box to that column.

So from all the above, i think that you need a combination of JS and CSS

like image 33
Andreas Traganidas Avatar answered Oct 12 '22 00:10

Andreas Traganidas