Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Grid causing horizontal scroll bar [duplicate]

Tags:

html

css

I have a grid that is causing a horizontal scroll bar I do not want, I have tried several things including putting my grid inside other divs. Here is the code in css for the grid.

.page-grid {
      display: grid;
      grid-template-rows: auto auto;
      grid-template-columns: 60% 40%;
      grid-template-areas:
      "ONE TWO"
      "THREE THREE"
       ;
      align-items: center;
      justify-content: center;
      text-align: center;
      grid-gap: 20px;
      margin-top: 70px;
      background: White;
      max-width:100%;
      box-sizing: border-box;
    }



like image 803
RavingPlaty Avatar asked Jul 18 '26 23:07

RavingPlaty


1 Answers

With grid-template-columns: 60% 40% you already say that the whole width is supposed to be used. Adding grid-gap: 20px extends it even further, causing the horizontal scroll bar.

Instead you can write grid-template-columns: 3fr 2fr

fr stands for fractions and has basically the same effect, except of not causing the extra pixels needed. It will adjust all the spacing of the grid-gap automatically.

.fractions {
    display: grid;
    grid-template-columns: 3fr 2fr;
    grid-gap: 20px;
    margin-bottom: 30px;
}
.grid-element {
    height: 80px;
    background: #00ff00;
}
<div class="fractions">
  <div class="grid-element"></div>
  <div class="grid-element"></div>
 </div>
like image 68
colin Avatar answered Jul 21 '26 13:07

colin