Each row can have a max of 4 elements, but if it has less (1, 2, 3) those elements should be centered. And if there are only 1,2, or 3 elements, those should be centered too. The justify-content doesn’t seem to be working and I’ve also tried using justify-items as well.
Clarification: the ‘.tech-used’ tag is just for a parent element, but I didn’t include it in the JSX code snippet.
<div className="tech-grid">
{project.skills.map((skill, index) => (
<div className="tech-item" key={index}>
<img src={skill.logo} alt={`${skill.name} logo`} className="skill-logo"/>
<p>{skill.name}</p>
</div>
))}
</div>
.tech-used{
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.tech-grid{
display: grid;
grid-template-columns: repeat(4,1fr);
gap: 30px;
justify-content: center;
}
.tech-item{
display: flex;[![enter image description here][1]][1]
flex-direction: column;
align-items: center;
}
2
Answers
Given these defined parameters you can leverage
nth-child():nth-last-child()
to manage the position of the 1st element.Note that this requires a 16 column grid with each child spanning 4 columns.
For this problem, a flexbox which wraps is a better solution than a grid. The key is to specify the width of the child elements in order to get the desired number of columns.
The calculation above uses a couple of global variables:
It divides the width of the parent (100%) by the number of columns (4 in this case), then subtracts the gap. However, because there is one fewer gap than the number of columns, we don’t need to subtract the entire gap, only this fraction of it …
… where 𝑥 is the number of columns. So in this case the fraction is ¾.