i have created a dynamic grid. That is placing as many columns as possible. The only porblem is, that the widht need to be set. in my example i have it set to 100%.
That is generating a white space around the grid that can’t befilled with a column.
is there any way to center the grid in the div or make the width as wide as the available columns?
that my current css for the grid:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, calc(235px + 30px));
width: 100%;
height: 100%;
place-items: center;
grid-auto-rows: calc(325px + 30px);
}
and thats the html:
<div className="card-grid">
<h1>some Content</h1>
<h1>some Content</h1>
<h1>some Content</h1>
<h1>some Content</h1>
<h1>some Content</h1>
<h1>some Content</h1>
<h1>some Content</h1>
</div>
in the browser it looks like this:
And i want to remove the space that is overlapping on the right side.
Setting the width on auto doesn’t work bc. then the grid has only on column.
2
Answers
Try using
justify-items
andalign-itmes
separately instead ofplace-items
You can do it this way
elwidth is the width you want to set for the width of the item
the resize event adjusts the amount of element to display per row depending on the width of the browser
this script should be placed at the end of the body, after loading the dom
css
You can try with 1,2 3… the elements will always be centered
you can replace the selector
by
and you can place inside the div any type of element