CSS Columns will not horizontally align
Asked Answered
S

5

6

I am using column count to allow my text to flow into two different columns but the top of the first column (leftmost) is lower than the other column?

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
}
<div id="col">
  <h3>
    Options
  </h3>
  <h3>
    Features and Benefits
  </h3>
  <h3>
    Specifications
  </h3>
  <h3>
    hey
  </h3>
  <h3>
    30 Years Experience
  </h3>
</div>

I have included a limited section of the code, and even when I fill it with text, there is still a difference in the top of the columns.

Solano answered 11/7, 2017 at 12:54 Comment(3)
It's always nice to give more information on the browser you useLeonardaleonardi
FireFox applies vertical margin collapsing in the first column, and gets confused in the second. See: developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/…Ribeiro
@Ronan Garrison, which answer you marked?you test it?it is wrong.Griffis
G
2
  #col{
     margin-top:0px;
  }
  #col h3{
      display:inline-block;
      vertical-align:top; // middle or bottom
  }
Generalist answered 11/7, 2017 at 13:3 Comment(0)
G
3

Use :

#col h3 {
  margin-top: 0;
}

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
}

#col h3 {
  margin-top: 0;
}
<div id="col">
  <h3>Options</h3>
  <h3>Features and Benefits</h3>
  <h3>Specifications</h3>
  <h3>hey</h3>
  <h3>30 Years Experience</h3>
</div>
Griffis answered 11/7, 2017 at 13:2 Comment(0)
S
3

Just a bit of CSS:

CSS:

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
  position:relative;
}
h3{display:inline-block;width:100%;}
// Best would be #col > * , because all direct children must be affected.

HTML:

<div id="col">
    <h3>
        Options 
    </h3>
    <h3>
        Features and Benefits
    </h3>
    <h3>
        Specifications
    </h3>
    <h3>
        hey
    </h3>
    <h3>
        30 Years Experience
    </h3>
    </div>

Snippet:

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
  position:relative;
}
h3{display:inline-block;width:100%;}
<div id="col">
    <h3>
        Options 
    </h3>
    <h3>
        Features and Benefits
    </h3>
    <h3>
        Specifications
    </h3>
    <h3>
        hey
    </h3>
    <h3>
        30 Years Experience
    </h3>
    </div>
Sprinkler answered 11/7, 2017 at 13:2 Comment(1)
THANK YOU! After all the stuff I saw lying around, this is the one that did it, specifically the second rule. CSS can really be quirky. I'm sorry I could only vote this up once.Leta
G
2
  #col{
     margin-top:0px;
  }
  #col h3{
      display:inline-block;
      vertical-align:top; // middle or bottom
  }
Generalist answered 11/7, 2017 at 13:3 Comment(0)
T
1

The fact that h3 elements have a margin-top by default, caused this problem. Removing the margin fixes it, as seen in the snippet below.

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
}
h3 {
  margin-top: 0;
  }
<div id="col">
  <h3>
    Options
  </h3>
  <h3>
    Features and Benefits
  </h3>
  <h3>
    Specifications
  </h3>
  <h3>
    hey
  </h3>
  <h3>
    30 Years Experience
  </h3>
</div>
Tyro answered 11/7, 2017 at 13:6 Comment(0)
P
0

Just remove the top margin from the h3 element

#col {
  -moz-column-count: 2;
  -webkit-column-count: 2;
  column-count: 2;
}

h3 {
  margin-top: 0;
}
<div id="col">
  <h3>
    Options
  </h3>
  <h3>
    Features and Benefits
  </h3>
  <h3>
    Specifications
  </h3>
  <h3>
    hey
  </h3>
  <h3>
    30 Years Experience
  </h3>
</div>
Pumpkin answered 11/7, 2017 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.