CSS Grid列在每个列下面堆叠,有大间隙。

huangapple go评论69阅读模式
英文:

CSS Grid Columns stacking with large gap under each column

问题

I'm relatively new to CSS grid, I'm having issues with my columns. On desktop, columns 2 and 4 have a large gap between them. I'm struggling to close this gap as they must sit directly under each other. So, when each column has different heights of text in, all columns must sit nicely under each other with no gap. Then on mobile they all just stack nicely.

I have created a demo:
http://jsfiddle.net/1uvwyL7h/

.one {
     background: red;
}
.two {
     background: pink;
}
.three {
     background: green;
}
.four {
     background: yellow;
}
.wrapper section {
     height: fit-content;
}
@media (max-width: 768px) {
     .wrapper section:nth-child(2) {
         margin: 32px 0 40px;
    }
     .wrapper section:nth-child(4) {
         margin: 24px 0 40px;
    }
}
@media (min-width: 768px) {
     .wrapper {
         display: grid;
         gap: 1rem 4rem;
         grid-template-columns: repeat(2, 1fr);
    }
     .wrapper section {
          grid-column: 1/2;
    }
     .wrapper section.sp {
         grid-column: 2/3;
        grid-row: 1 / span 3;
    }
     .wrapper section.sa {
         grid-column: 2/2;
        grid-row: 3 / span 1;
    }
}
@media (min-width: 1280px) {
     .wrapper {
         padding: 32px 24px 24px;
    }
}
<div class="wrapper">
  <section class="one">
    <p>
      MOBILE ORDER 1
    </p>
    <p>
      DESKTOP COL 1 TOP LEFT
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
  </section>
  <section class="two sp">
    <p>
      MOBILE ORDER 2
    </p>
    <p>
      DESKTOP COL 2 TOP RIGHT
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
  </section>
  <section class="three">
    <p>
      MOBILE ORDER 3
    </p>
    <p>
      DESKTOP COL 1 BOTTOM LEFT
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
  </section>
  <section class="four sa">
    <p>
      MOBILE ORDER 4
    </p>
    <p>
      DESKTOP COL 2 BOTTOM RIGHT
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
    <p>
    bla bla<br />bla bla<br />bla bla<br />bla bla<br />
    </p>
  </section>
</div>

Thanks

英文:

I'm relatively new to CSS grid, I'm having issues with my columns. On desktop, columns 2 and 4 have a large gap between them. I'm struggling to close this gap as they must sit directly under each other. So, when each column has different heights of text in, all columns must sit nicely under each other with no gap. Then on mobile they all just stack nicely.

I have created a demo:
http://jsfiddle.net/1uvwyL7h/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.one {
background: red;
}
.two {
background: pink;
}
.three {
background: green;
}
.four {
background: yellow;
}
.wrapper section {
height: fit-content;
}
@media (max-width: 768px) {
.wrapper section:nth-child(2) {
margin: 32px 0 40px;
}
.wrapper section:nth-child(4) {
margin: 24px 0 40px;
}
}
@media (min-width: 768px) {
.wrapper {
display: grid;
gap: 1rem 4rem;
grid-template-columns: repeat(2, 1fr);
}
.wrapper section {
grid-column: 1/2;
}
.wrapper section.sp {
grid-column: 2/3;
grid-row: 1 / span 3;
}
.wrapper section.sa {
grid-column: 2/2;
grid-row: 3 / span 1;
}
}
@media (min-width: 1280px) {
.wrapper {
padding: 32px 24px 24px;
}
}

<!-- language: lang-html -->

&lt;div class=&quot;wrapper&quot;&gt;
&lt;section class=&quot;one&quot;&gt;
&lt;p&gt;
MOBILE ORDER 1
&lt;/p&gt;
&lt;P&gt;
DESKTOP COL 1 TOP LEFT
&lt;/P&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;two sp&quot;&gt;
&lt;p&gt;
MOBILE ORDER 2
&lt;/p&gt;
&lt;P&gt;
DESKTOP COL 2 TOP RIGHT
&lt;/P&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;three&quot;&gt;
&lt;p&gt;
MOBILE ORDER 3
&lt;/p&gt;
&lt;P&gt;
DESKTOP COL 1 BOTTOM LEFT
&lt;/P&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;four sa&quot;&gt;
&lt;p&gt;
MOBILE ORDER 4
&lt;/p&gt;
&lt;P&gt;
DESKTOP COL 2 BOTTOM RIGHT
&lt;/P&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;
bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;
&lt;/p&gt;
&lt;/section&gt;
&lt;/div&gt;

<!-- end snippet -->

CSS Grid列在每个列下面堆叠,有大间隙。

Thanks

答案1

得分: 1

修改了CSS。你的网格中有一些不需要的因素。我们有一个名为'grid-container'的CSS,紧挨着你的DIV中的'wrapper',但你可以将它们合并在一起(尽管最好将所有网格设置都放在'grid-container'中)。从头开始创建一个起始网格,具备所有基本参数,可以帮助你捕捉某些错误,比如在文本中失去网格框的自动高度(一个网格中的长文本和另一个网格中的短文本)。

额外的“如何”信息:
mozilla

/*--====Grid====--*/
.grid-container {
  display: grid;
  grid-area: auto / auto / auto / auto;
  gap: 10px;
  padding: 10px;
  /*--background-color: #2196F3;--*/ /*--如果你想要背景颜色--*/
}

.one {background: red;}
.two {background: pink;}
.three {background: green;}
.four {background: yellow;}

/*--这部分不需要--*/
xxxx.wrapper section {height: fit-content;}

/*--这部分不需要--*/
xxx@media (max-width: 768px) {
     .wrapper section:nth-child(2) {
         margin: 32px 0 40px;
    }
     .wrapper section:nth-child(4) {
         margin: 24px 0 40px;
    }
}

@media (min-width: 768px) {
     .wrapper {
         display: grid;
         gap: 1rem 1rem;
		 grid-template-columns: repeat(2, 1fr);
		 }
        section.one {
            grid-area: 1 / 1;
        }
        section.two {
            grid-area: 1 / 2;
        }
        section.three {
            grid-area: 2 / 1;
        }
        section.four {
            grid-area: 2 / 2;
         }
    }

/*--这部分不需要--*/
xxx@media (min-width: 1280px) {
     .wrapper {
         padding: 32px 24px 24px;
    }
}

h1, h2, h3, h4, p {
  color: black;
  padding: 10px;
  margin: 0;
}
<div class="grid-container wrapper">
<section class="one">
    <h2> MOBILE ORDER 1 </h2>
    <h4>  DESKTOP COL 1 TOP LEFT  </h4>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
  </section>
  <section class="two sp">
    <h2>  MOBILE ORDER 2  </h2>
    <h4>  DESKTOP COL 2 TOP RIGHT  </h4>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
  </section>
  <section class="three">
    <h2>  MOBILE ORDER 3 </h2>
    <h4>  DESKTOP COL 1 BOTTOM LEFT  </h4>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
  </section>
  <section class="four sa">
    <h2>  MOBILE ORDER 4  </h2>
    <h4>  DESKTOP COL 2 BOTTOM RIGHT  </h4>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
    <p>  bla bla<br />bla bla<br />bla bla<br />bla bla<br />  </p>
  </section>
</div>
英文:

Revised the CSS. Some factors in your grid are not needed.
We have a CSS called 'grid-container' next to your 'wrapper' in DIV, but you can merge both of those together (though its ideal to place all grid setup in the 'grid-container'.
Making a starter grid from scratch with all the basic parameters helps you to catch certain errors, such as loosing the grid box auto height on the text (longer text in one grid and shorter text in other grid).

Additional 'How To' info:
mozilla

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/*--====Grid====--*/
.grid-container {
display: grid;
grid-area: auto / auto / auto / auto;
gap: 10px;
padding: 10px;
/*--background-color: #2196F3;--*/ /*--If you want background color--*/
}
.one {background: red;}
.two {background: pink;}
.three {background: green;}
.four {background: yellow;}
/*--This not needed--*/
xxxx.wrapper section {height: fit-content;}
/*--This not needed--*/
xxx@media (max-width: 768px) {
.wrapper section:nth-child(2) {
margin: 32px 0 40px;
}
.wrapper section:nth-child(4) {
margin: 24px 0 40px;
}
}
@media (min-width: 768px) {
.wrapper {
display: grid;
gap: 1rem 1rem;
grid-template-columns: repeat(2, 1fr);
}
section.one {
grid-area: 1 / 1;
}
section.two {
grid-area: 1 / 2;
}
section.three {
grid-area: 2 / 1;
}
section.four {
grid-area: 2 / 2;
}
}
}
/*--This not needed--*/
xxx@media (min-width: 1280px) {
.wrapper {
padding: 32px 24px 24px;
}
}
h1, h2, h3, h4, p {
color: black;
padding: 10px;
margin: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;grid-container wrapper&quot;&gt;
&lt;section class=&quot;one&quot;&gt;
&lt;h2&gt; MOBILE ORDER 1 &lt;/h2&gt;
&lt;h4&gt;  DESKTOP COL 1 TOP LEFT  &lt;/h4&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;two sp&quot;&gt;
&lt;h2&gt;  MOBILE ORDER 2  &lt;/h2&gt;
&lt;h4&gt;  DESKTOP COL 2 TOP RIGHT  &lt;/h4&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;three&quot;&gt;
&lt;h2&gt;  MOBILE ORDER 3 &lt;/h2&gt;
&lt;h4&gt;  DESKTOP COL 1 BOTTOM LEFT  &lt;/h4&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;four sa&quot;&gt;
&lt;h2&gt;  MOBILE ORDER 4  &lt;/h2&gt;
&lt;h4&gt;  DESKTOP COL 2 BOTTOM RIGHT  &lt;/h4&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;p&gt;  bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;bla bla&lt;br /&gt;  &lt;/p&gt;
&lt;/section&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 18:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定