1

creating an (ultimately) three tier tree view structure sports > teams> players progress: https://jsfiddle.net/zigzag/kusuz233/1/

body {
  margin: 10px;
}
.firstLevel {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-template-rows: 200px;
  background-color: #fff;
  color: #444;
  margin-bottom: 20px;
}

.firstLevel > .box{
    background-color: red;
}

.secondLevel > .box{
    background-color: gray;
}

.secondLevel {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-template-rows: 200px 200px;
  grid-gap: 10px;
  background-color: #fff;
  color: #444;
}

.box {
  color: #fff;
  padding: 20px;
  font-size: 150%;
}
<div class="firstLevel">
  <div class="box">Sports</div>

</div>
<div class="secondLevel">
  <div class="box a">NBA</div>
  <div class="box b">NFL</div>
  <div class="box c">NHL</div>
  <div class="box d">IPL</div>
  <div class="box e">MLB</div>
  <div class="box f">MLS</div>
</div>

How do I get the sports box to be centered with same width as the others?

Also, I started out doing one grid container but then had issue with the top node so separated in two grid containers. perhaps this can be accomplished within just one container? In my case the text and description here will be dynamic and so will the number of objects so just planning for that... Appreciate any input.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
zigzag
  • 57
  • 10

1 Answers1

0

There are many ways to center grid items. See various methods here:

In this particular case, a simple solution would be the addition of pseudo-elements. Only one container is needed overall.

When the ::before and ::after pseudo-elements are applied to an element, they become children of that element (read more).

So, pseudo-elements on a grid container become grid items.

Place those grid items in the first and last columns of the first row. This will force the "Sports" item to be centered.

body {
  margin: 10px;
}

.firstLevel {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-auto-rows: 200px;
  grid-gap: 1em;
  background-color: #fff;
  color: #444;
}

.firstLevel::before {
  content: "";
  grid-column: 1 / 2;
  grid-row: 1 / 2;
  /* border: 1px dashed black;  << remove commenting for illustration */
}

.firstLevel::after {
  content: "";
  grid-column: 3 / 4;
  grid-row: 1 / 2;
  /* border: 1px dashed black;  << remove commenting for illustration */
}

.box {
  background-color: gray;
  color: #fff;
  padding: 20px;
  font-size: 150%;
}

.red {
  background-color: red;
}
<div class="firstLevel">
  <div class="box red">Sports</div>
  <div class="box a">NBA</div>
  <div class="box b">NFL</div>
  <div class="box c">NHL</div>
  <div class="box d">IPL</div>
  <div class="box e">MLB</div>
  <div class="box f">MLS</div>
</div>

jsFiddle demo

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Thanks, Michael. That is how I wanted it to look. Couple of questions- 1) I just noticed IE 11 doesn't render even my half broken code for grid. Grids and what we are trying to do here is that supported in IE much? 2)In terms of the solution you recommended, in my case the sport league boxes are going to be dynamic and I won't know how many until run time. Can you do percentage width for grid-template-columns? Each box is going to have to have a set width and the top one needs to be centered though always. Thoughts? – zigzag Dec 17 '17 at 05:39