Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS columns and margin issue

Update: The latest code which works in all browsers is located in this JSFiddle - https://jsfiddle.net/webvitaly/yu00ugft/5/

I am trying to make responsive columns using pure CSS approach.

I have an issue where columns sometimes have gap/margin at the top.

JSFiddle link to try and explore - https://jsfiddle.net/webvitaly/yu00ugft/

How to solve it?

CSS:

.fx-columns {
  background: yellow;
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
    -webkit-column-gap: 30px;
    -moz-column-gap: 30px;
    column-gap: 30px;
}

.fx-columns-3 {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;
}

.fx-columns-4 {
    -webkit-column-count: 4;
    -moz-column-count: 4;
    column-count: 4;
}

.fx-columns .fx-column {
    -webkit-column-break-inside: avoid;
    break-inside: avoid;
  background: pink;
  clear: both;
  margin-bottom: 20px;
}

HTML:

<div class="fx-columns fx-columns-4">
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
  <div class="fx-column">
    <h3>Header</h3>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
    <p>Text paragraph</p>
  </div>
</div>
like image 900
webvitaly Avatar asked Jun 24 '16 19:06

webvitaly


People also ask

How do you add a column margin in CSS?

You can use calculated width/margins., no change to your HTML necessary. E.g. the width of col-md-3 is 100/4=25%. Therefore you can reduce this, say to 20%, and allocate the remaing 5% to your margins.

Does CSS support multiple columns for laying out text?

CSS Multi-column Layout is a module of CSS that adds support for multi-column layouts.

What is column-gap?

The column-gap CSS property sets the size of the gap (gutter) between an element's columns.


1 Answers

Currently, you're keeping the content from breaking between columns. However, the browser breaks the margin between columns, which is causing some of the columns to not start at the top.

Add this to your css:

.fx-column {
  display: inline-block;
}

You'll then have to give the columns a fixed width.

Know, however, that there are a lot of issues with using the column count css property. See this article for more information.

Edit: If you want the column widths to be responsive, you can simply set them to width: 100% so that they expand as the browser expands.

like image 137
jtmingus Avatar answered Oct 06 '22 00:10

jtmingus