r/csshelp • • Oct 03 '20

Justify-items not working, I'm only getting justify-content

justify-items (not working) to be exact it doesn't show and i was wondering what i was doing wrong?

```

.hlekkir{

display: grid;

grid-template-columns: 10em 10em 10em 10em;

height: 4em;

align-items: center;

justify-content: center;

}

```

0 Upvotes

3 comments sorted by

2

u/GekkePop Oct 04 '20

Can you make an example of codesandbox.io or codepen ? Because just this code doesn't show us anything.

1

u/LinkifyBot Oct 04 '20

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3

1

u/FriesWithThat Oct 04 '20

The size of your grid is less than the size of its grid container .hlekkir. Your grid has 4 columns defined with explicit units (px, em, etc). You can individually distribute those 10em columns anywhere within the container using justify-content. You can distribute the grid items within their columns (also along the inline axis) using justify-items. You don't have any code demonstrating what those items might look like, or rules defining their behavior, but if you just have a div with a background color with a defined width a little less than 10em, it's doubtful they're moving around much using justify-items here. It does work, just probably not what you want. You're just moving those boxes (grid-items around a bit within the space remaining in your columns - you're also not positioning any content within those grid items (except relative to those items themselves). For that you can use traditional css, flex, or make the grid item their own grid container. How I actually remember the difference between their intended purpose is the addition of the extra values available when you use justify-content as opposed to justify-items.

justify-items: start | end | center | stretch;

justify-content: start | end | center | stretch | space-around | space-between | space-evenly;

So justify content is meant to work on overall positioning for your entire grid, and justify items just for the positioning of those items within their grid columns - where those last 3 options wouldn't make any sense for positioning a singular item within a table cell.

Play around with this code: and you'll get it:

<!-- html -->
<div class="grid">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
</div>


/*  css */
.grid {
display: grid;
width: 1000px;
height: 200px;
background: blue;
border: 1px solid black;
grid-template-columns: 15em 15em 15em 15em;
align-items: center;
justify-content: center;
}

.grid-item {
/*   display: grid;
  justify-items: center;
  align-items:  center; */
  font-size: 2em;
  width: 5em;
  height: 5em;
  color: red;
  background: yellow;
}