0

I would like to align horizontally on a single line a bunch of divs of class a within a container div.

Why is the below css code not working ?

.a {
  border: 1px solid black;
}

#container {
  display:grid;
  grid-auto-columns: minmax(10px, 35px);
  grid-template-rows:1fr;
}

I have created a jsFiddle for it. I was expecting the divs 1 to 6 there to all be on the same line but I get the following result :

the non-desired result

Chapo
  • 2,563
  • 3
  • 30
  • 60

2 Answers2

0

You need to change the default flow ref to column:

.a {
  border: 1px solid black;
}

#container {
  display: grid;
  grid-auto-columns: minmax(10px, 35px);
  grid-template-rows: 1fr;
  grid-auto-flow:column; /* added */
}
<div id="container">
  <div class="a">
    1
  </div>
  <div class="a">
    2
  </div>
  <div class="a">
    3
  </div>
  <div class="a">
    4
  </div>
  <div class="a">
    5
  </div>
  <div class="a">
    6
  </div>
</div>

Or define a template column like below:

.a {
  border: 1px solid black;
}

#container {
  display: grid;
  grid-template-columns:repeat(auto-fill, minmax(10px, 35px));
  grid-template-rows: 1fr;
}
<div id="container">
  <div class="a">
    1
  </div>
  <div class="a">
    2
  </div>
  <div class="a">
    3
  </div>
  <div class="a">
    4
  </div>
  <div class="a">
    5
  </div>
  <div class="a">
    6
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

Try below code:

CSS:

#container {
    display: grid;
    grid-template-rows: 1fr;
    grid-template-columns: repeat(auto-fill, minmax(75px, 1fr));
    grid-gap: 1rem;
}

Note: Its align horizontally on a single line.

HAPPY SINGH
  • 536
  • 4
  • 13