Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore the margin inline blocks create with different heights? [duplicate]

Tags:

html

css

flexbox

I have an issue where the blocks stack by 2, and the next 2 blocks start at the end of the first 2 blocks. As shown in this JSFiddle demo.

HTML:

<div class="container">
  <div class="inline">
  A div with less content than that one >
  </div>
  <div class="inline">
  A div with more content than the one on the left. Now Inline 3 goes down to where this div ends. I want to move it up however, so it's right under the div that's above Inline 3.
  </div>
  <div class="inline">
  Inline 3
  </div>
  <div class="inline">
  Inline 4
  </div>
</div>

CSS:

.container {
  width:600px;
  background-color:rgb(40,40,40);
}

.inline {
  width:calc(50% - 22px);
  display:inline-block;
  vertical-align:top;
  color:white;
  background-color:#e74c3c;
  text-align:center;
  margin:5px 10px;
}

Output:

Output

Note: This doesn't take up the white space created by the top-right div.

Expected/wanted output:

Expected output

Note: This does make use of the whitespace.

I'm aware that this is possible with 2 columns, but I don't want to use 2 columns. Because I must be able to remove some div's without having unequal content in the columns.

like image 689
Daan Avatar asked Dec 16 '15 08:12

Daan


People also ask

Does margin work on inline block?

The block-direction margin on inline elements is ignored entirely. The padding on inline elements doesn't affect the height of the line of text.

Does setting margin top and margin bottom have an effect on an inline element?

Top and bottom margins do not affect inline elements because inline elements flow with content on the page. You can set left and right margins/padding on an inline element but not top or bottom because it would disrupt the flow of content.

How do you set inline margins in CSS?

The margin-inline CSS shorthand property is a shorthand property that defines both the logical inline start and end margins of an element, which maps to physical margins depending on the element's writing mode, directionality, and text orientation.


1 Answers

I had the same problem at the website i created. i used masonry to solve that: http://masonry.desandro.com/

like image 177
brandelizer Avatar answered Sep 27 '22 22:09

brandelizer