Reduce space between elements in a css grid layout - html

I have a grid layout that looks like this:
I would like to reduce the whitespace between 'Job Role' and 'Company Name' & I cannot work out how to do this.
The container css looks like this:
.experience-child {
display: grid;
grid-template-columns: 2fr 6fr;
grid-template-rows: repeat(5, 1fr);
margin-bottom: 6em;
font-family: 'Alumni Sans', sans-serif;
}
I have tried:
Adjusting row-gap with row-gap 0;
Adjusting margin-bottom and padding-bottom of the individual elements with negative values.
Adding grid-auto-rows: min-content; to the above container
Thank you very much.
HTML & CSS to replicate:
<div class="experience-child">
<p class="years">2001-2001</p>
<p class="jobrole">Job Role</p>
<p class="company">Company Name</p>
<ul class="job-blurb">
<li>this</li>
<li>that</li>
<li>other</li>
</ul>
</div>
.experience-child {
display: grid;
grid-template-columns: 2fr 6fr;
grid-template-rows: repeat(5, 1fr);
margin-bottom: 6em;
font-family: 'Alumni Sans', sans-serif;
}
.last {
margin-bottom: 0;
}
.jobrole {
font-weight: bold;
}
.company {
grid-area: 2 / 2 / 3 / 3;
font-weight: bold;
}
.job-blurb {
grid-area: 3 / 2 / 5 / 3;
}

grid-template-rows: repeat(5, 1fr); indicates that your grid has 5 rows of equal height. That's the height of the .experience-child container divided by 5.
There are many ways to reduce the whitespace between Job Role and Company Name:
You could reduce the height of the first row, by replacing it with e.g. grid-template-rows: 1em repeat(4, 1fr);
You could keep equal row height and move Job Role within its container with e.g. position: absolute; bottom: 0;, or padding-top: 1em;
You could place Company Name in the same container as Job Role by wrapping <p class="jobrole">Job Role</p><p class="company">Company Name</p> in a new div

Change grid-template-rows: repeat(5, 1fr); to grid-template-rows: repeat(1, 1fr);.
See the snippet below.
.experience-child {
display: grid;
grid-template-columns: 2fr 6fr;
grid-template-rows: repeat(1, 1fr);
margin-bottom: 6em;
font-family: 'Alumni Sans', sans-serif;
background-color: gold;
}
.last {
margin-bottom: 0;
}
.jobrole {
font-weight: bold;
}
.company {
grid-area: 2 / 2 / 3 / 3;
font-weight: bold;
}
.job-blurb {
grid-area: 3 / 2 / 5 / 3;
}
p {
margin: 0;
}
<div class="experience-child">
<p class="years">2001-2001</p>
<p class="jobrole">Job Role</p>
<p class="company">Company Name</p>
<ul class="job-blurb">
<li>this</li>
<li>that</li>
<li>other</li>
</ul>
</div>

Related

Resize elements in same grid column to fit

I have a CSS grid with several columns and many rows (I'm building a timetable view). The rows and columns are defined on the grid element itself, and then on the elements within the grid I set their column (always only one column) and their rows (might be more than one row).
An example is as follows:
.grid {
display: grid;
grid-template-rows: [row-a] 1fr [row-b] 1fr [row-c] 1fr [row-d] 1fr;
grid-template-columns: [col] 1fr;
flex-grow: 1;
}
.entry-one {
grid-column: col;
grid-row: row-a/row-d;
background-color: red;
}
.entry-two {
grid-column: col;
grid-row: row-b;
background-color: green;
}
<div class='grid'>
<div class='entry-one'>
Foobar
</div>
<div class='entry-two'>
Barfoo
</div>
</div>
Now, what I would like to have is that the elements resize themselves and flow nicely, such that they fit next to each other. I can mock this using width and margin on the elements:
.grid {
display: grid;
grid-template-rows: [row-a] 1fr [row-b] 1fr [row-c] 1fr [row-d] 1fr;
grid-template-columns: [col] 1fr;
flex-grow: 1;
}
.entry-one {
grid-column: col;
grid-row: row-a/row-d;
background-color: red;
width: 50%; /* ADDED */
}
.entry-two {
grid-column: col;
grid-row: row-b;
background-color: green;
width: 50%; /* ADDED */
margin-left: 50%; /* ADDED */
}
<div class='grid'>
<div class='entry-one'>
Foobar
</div>
<div class='entry-two'>
Barfoo
</div>
</div>
However this is not optimal, especially as the elements are inserted dynamically. Is there a way to have the elements size & align themselves automatically using CSS? I've tried to use display: flex on the entries, but that did not result in what I want (or maybe I forgot to add another rule).
Thank you for any ideas, and have a nice day!
I made this to see if that is what you are looking for
.grid{
display: flex;
grid-template-rows: [row-a] 1fr [row-b] 1fr [row-c] 1fr [row-d] 1fr;
grid-template-columns: [col] 1fr;
flex-grow: 1;
}
I just changed your display to flex and delete your margin-left: 50%; on the entry two, hope it is what you are looking for

Div inside grid not taking the alloted area

I have a container where I am using grid layout as show below
My HTML looks like this
<div class='pagelayout'>
<div class="tophalf">This div needs to occupy entire width of the page</div>
</div>
CSS looks like this
.pagelayout {
display: grid;
flex-grow: 1;
grid-template-columns: 70fr 30fr;
grid-template-rows: auto auto;
overflow: auto;
grid-template-areas:
'tophalf tophalf'
'leftside rightside'
}
.tophalf{
grid-area: tophalf;
}
Snippet:
.pagelayout {
display: grid;
flex-grow: 1;
grid-template-columns: 70fr 30fr;
grid-template-rows: auto auto;
overflow: auto;
grid-template-areas: 'tophalf tophalf' 'leftside rightside'
}
.tophalf {
grid-area: tophalf;
}
<div class='pagelayout'>
<div class="tophalf">This div needs to occupy entire width of the page</div>
</div>
But the div(This div needs to occupy entire width of the page) is only taking 70fr and the rest 30fr is left unused.
If I change the grid template like this, everythign is working fine.
.pagelayout {
display: grid;
flex-grow: 1;
grid-template-columns: 1fr;
grid-template-rows: auto;
overflow: auto;
grid-template-areas:
'tophalf tophalf'
'leftside rightside'
}
.tophalf{
grid-area: tophalf;
}
Snippet:
.pagelayout {
display: grid;
flex-grow: 1;
grid-template-columns: 1fr;
grid-template-rows: auto;
overflow: auto;
grid-template-areas: 'tophalf tophalf' 'leftside rightside'
}
.tophalf {
grid-area: tophalf;
}
<div class ='pagelayout'>
<div class ="tophalf">This div needs to occupy entire width of the page</div>
</div>
What is that I'm doing wrong in the first case?
grid-template-columns: 70fr 30fr; this means create two colums one which takes 70fr & other which takes 30fr
grid-template-columns: 70fr 30fr;
grid-template-rows: auto auto;
.pagelayout {
display: grid;
flex-grow: 1;
grid-template-columns: 70fr 30fr; /*this means create two colums one which takes 70fr & other which takes 30fr*/
grid-template-rows: auto;
overflow: auto;
outline:2px solid green;
}
.tophalf {
outline:2px solid red;
}
<div class='pagelayout'>
<div class="tophalf">This div needs to occupy entire width of the page</div>
</div>

Why doesn't this CSS grid create the right number of rows and columns?

This CSS grid creates 7 rows only, as you can see by using "Inspect tool" of the browser.
Why?
.grid {
display: grid;
grid-gap: 1em;
height: 100%;
}
.grid8x8 {
grid-template-rows: repeat(8, 1fr);
grid-template-columns: repeat(8, 1fr);
}
.element {
background-color: yellow;
}
<div class="grid grid8x8">
<div class="element" style="grid-row: 2 / 4; grid-column: 3 / 6;">TEST</div>
</div>
You have actually 8 rows. What you see with the inspector is the gap between the cells, so for 8 rows there are 7 gaps (red lines are gaps):
If you reduce the gap size, you'll notice how the rows appear:
Note the 8 rows:
Your problem is the grid cell overflowing all the other cells because there's not enough space, because your grid-gap is too high.
Try this:
.grid { display: grid; grid-gap: 1em; height: 70vh; }
.grid8x8 { grid-template-rows: repeat(8, 1fr); grid-template-columns: repeat(8, 1fr); }
.element { background-color: yellow; }
<div class="grid grid8x8">
<div class="element" style="grid-row: 2 / 4; grid-column: 3 / 6;">TEST</div>
</div>

CSS grid make items same height

At first, all items in the grid are in the same size i want it to be
But after clicking the 'Load More' button, the height for the new items are not the same:
Example here
I want to make those items the same height as shown in the first image, but I'm not sure how
my html
<div>
<Title message={"Your Top Artists"}/>
<div className="topArtists">
{toDisplay.map(item=>{
return(
<div className="items">
<li className="items-li" onClick={(e) => handleClick(e, item)}>
<Link to={`${path}/artist/${item.id}`}>
{item.name}, {item.followers}, {item.genres}, {item.popularity}
</Link>
</li>
</div>
)
})}
</div>
<button onClick={loadMore}> Load More </button>
</div>
And my Css
.topArtists{
/*width: 100%;*/
display: grid;
grid-template-rows: repeat(3, 250px);
grid-template-columns: repeat(3, 1fr);
column-gap: 10px;
row-gap: 15px;
}
.items {
border: 1px solid red;
}
.items-li{
padding: 10px;
font-size: 25px;
list-style: none;
}
Currently, you have set height for only 3 first rows. Use grid-auto-rows instead
.topArtists{
display: grid;
grid-auto-rows: 250px;
grid-template-columns: repeat(3, 1fr);
column-gap: 10px;
row-gap: 15px;
}

Grid areas not laying out properly in CSS Grid

I want to make my website using CSS grid system but it seems not to be working. Here is my code:
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" "about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
When using the grid-template-areas property, string values must have the same number of columns.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" "about-us about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
You can use a period, or an unbroken line of periods, to represent an empty cell (spec reference).
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq" " ... about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>
From the Grid spec:
7.3. Named Areas: the grid-template-areas
property
All strings must have the same number of columns, or else the declaration is invalid.
If a named grid area spans multiple grid cells, but those cells do not form a single filled-in rectangle, the declaration is invalid.
Non-rectangular or disconnected regions may be permitted in a future version of this module.
Note: As stated in the spec, in addition to an equal number of columns, grid areas must also be rectangular (see this post for more details).
If this:
Is the desired result, then you've only made a minor error.
You've set the grid to be a 2 x 2 square here:
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
But you aren't filling all the space.
grid-template-areas: "logo faq", "about-us";
That line of code is saying "In the top two squares put logo and faq respectively. In the bottom two rows put about-us" and that causes an error. If you want one grid-area to fill the entire space then you need to declare it twice. Thus the above line becomes:
grid-template-areas: "logo faq", "about-us about-us";
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "logo faq", "about-us";
}
.logo {
background-color: blue;
grid-area: logo;
}
.faq {
background-color: red;
grid-area: faq;
}
.aboutUs {
background-color: cyan;
grid-area: about-us;
}
<div class="grid">
<div class="logo">
LOGO
</div>
<div class="faq">
FAq
</div>
<div class="aboutUs">
About-us
</div>
</div>