Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

html - Empty vertical space between columns in Bootstrap 4

I have this html structure:

<div class="container">
   <div class="row">
      <div class="col-lg-9 description"></div>
      <div class="col-lg-3 sidebar"></div>
      <div class="col-lg-9 comments"></div>
   </div>
</div>

The final effect is this: enter image description here I need this HTML structure because when I have a smaller viewports and Bootstrap switches to the 1-column mode, I need that sidebar column goes between description and comments columns (where actually there is the empty space).

The problem is that I want to avoid to have that empty space when the template isn't in "mobile mode".

I tried many ways but I can't achieve this, someone could help me?

EDIT

I tried this way:

<div class="container">
   <div class="row">
      <div class="col-lg-9">
         <div class="description"></div>
         <div class="comments"></div>
      </div>
      <div class="col-lg-3 sidebar"></div>
   </div>
</div>

And tried to reorder by using the css "order", but doesn't worked (the only thing it allows me to do is to put sidebar at the start of the page, but isn't what I want).

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's happening because Bootstrap 4 is flexbox, and all of the columns become the same height as the tallest column...

Instead of this:

 ------------------  ---------
|                  ||         |
|                  ||         |
 ------------------ |         |
 ------------------ |         |
|                  ||         |
 ------------------  ---------

You get this:

 ------------------  ---------
|                  ||         |
|                  ||         |
 ------------------ |         |
                    |         |
                    |         |
 ------------------  ---------
|                  |
 ------------------

Usually, you can workaround this (as you tried) by nesting the columns, but then you won't get the desired column order....

Nesting:

<div class="container">
    <div class="row">
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12 description"></div>
                <div class="col-lg-12 commments"></div>
            </div>
        </div>
        <div class="col-lg-3 sidebar"></div>
    </div>
</div>

However, to get the desired column order, the columns must be contained in a single .row.

Floats:

So, the workaround in BS4 is to use floats (like BS3 did) as explained here: Bootstrap 4 - I don't want columns to have the same height. Like this:

https://www.codeply.com/go/wnRnLl3Jmo

<div class="container">
   <div class="row d-lg-block">
      <div class="col-lg-9 float-left description">Desc</div>
      <div class="col-lg-3 float-right sidebar">Sidebar</div>
      <div class="col-lg-9 float-left comments">Comments</div>
   </div>
</div>

The d-lg-block sets display:block on the row instead of display:flex which allows the columns to float in lg screen widths.

Related:
Bootstrap with different order on mobile version


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...