my code is not working to set grid columns. However I'm following the exact same format as I've seen across the internet. Does someone know the solution to this? I think it might be by including a "repeat" function to create 6 grid columns of the same width. (it might work in VSC but it does not work in Cengage)
My grid-template-rows is working perfectly fine.
body {
width: 90%;
min-width: 640px;
max-width: 1024px;
margin-top: 30px;
margin-bottom: 30px;
margin-left: auto;
margin-right: auto;
display: grid;
gap: 15px;
grid-template-columns: 1fr repeat;
grid-template-rows: 50px 30px 1fr 1fr 100px;
}
grid-template-columns: 1fr repeat;
should be
grid-template-columns: repeat(6, 1fr);
Related
I have a column of a defined size (let's say 50px), then an undetermined number of columns that should divide the available space among themselves, and finally another 50px column.
I know this could be achieved if I knew the total number of columns beforehand:
body > div {
grid-template-columns: 50px repeat(3, 1fr) 50px;
display: grid; grid-gap: 1rem; background: beige; width: 80%; padding: 1rem;
}
div div { background: IndianRed; min-height: 5rem;}
<div><div></div><div></div><div></div><div></div><div></div></div>
I also know I can do a fixed sized column + n columns this way:
body > div {
grid-template-columns: 50px repeat(auto-fit, minmax(1px, 1fr));
display: grid; grid-gap: 1rem; background: beige; width: 80%; padding: 1rem;
}
div div { background: IndianRed; min-height: 5rem;}
<div><div></div><div></div><div></div><div></div><div></div></div>
But the specific case doesn't work as it could this way. Instead, I'm left with an empty column at the end:
body > div {
grid-template-columns: 50px repeat(auto-fit, minmax(1px, 1fr)) 50px;
display: grid; grid-gap: 1rem; background: beige; width: 80%; padding: 1rem;
}
div div { background: IndianRed; min-height: 5rem;}
<div><div></div><div></div><div></div><div></div><div></div></div>
Is the thing I'm trying to do achievable with grid?
Remove the last column from the grid template. The column template would now look like this:
grid-template-columns: 50px repeat(auto-fit, minmax(1px, 1fr));
Then add this:
div div:last-child {
grid-column: -1;
width: 50px;
}
That should work!
So I have a container as such:
.container{
max-width: 1300px;
margin: 0 auto;
padding: 20px 0px;
}
And inside the container, I have another child container/element which is a grid:
.child{
list-style-type: none;
padding: 0px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 15px;
}
Now for some reason, the child is causing the margin: 0 auto on the container to act weirdly: for example, at one time the margin on the left/right sides could be 280px, and other times it could be 297px, even if the screen width has not changed.
If I take out the following line from the .child,
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
...it works as expected, and the margin doesn't change.
I spent hours trying to figure this out with no luck, any ideas as to what could be happening here?
Hello i'm having this problem where if i don't have enough items there is more space between them than the row gap i provided? Why is that happening? (i have the same issue when i put flex-wrap:wrap......
This is what i have on the container
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 400px));
grid-template-rows: 100px;
grid-column-gap: 10px;
grid-row-gap: 10px;
justify-content: center;
height: calc(100% - 204px);
padding: 10px 10px 60px 10px;
On the items
height: 100px;
cursor:pointer;
width:100%;
border-radius: 0.75rem;
https://i.stack.imgur.com/f3g2h.png
grid-template-rows: 100px; means that the first row will be 100px and all the other will have an auto height. Either remove the property or use grid-auto-rows: 100px; to make sure all the rows will behave the same
This question already has an answer here:
Why percentage value within grid-gap create overflow in CSS grid?
(1 answer)
Closed 2 years ago.
I am making a list of items (cards) using grid, and the subgrid overflows into the next element element. Now, In Firefox, I don't have this problem. here is the code for the grid parent adn subgrid:
.grid-parent{
display: grid;
margin-top: 2rem;
grid-template-columns: 26rem [main-start]repeat(5, 1fr[main-end]);
grid-template-rows: min-content 1fr min-content max-content;
}
grid-child{
grid-column: 2/-1;
grid-row: 1/-1;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min-content, 30rem));
grid-template-rows: repeat(auto-fit, 1fr);
justify-content: center;
align-items: start;
padding: 0 3%;
grid-gap: 2%;
}
.big-btn{
grid-column: 1/-1;
width: 40%;
padding: 1rem 0;
font-size: 2.6rem;
#extend .fw-700;
}
Try to put a margin-bottom on the big-btn?
I'm trying to use CSS grid to vertically and horizontally center some text in a header/title area, and have that area (and the whole layout) be somewhat flexible/responsive.
I've set up a simple example in this code pen, but if you don't want to head over there, the HTML and CSS in the example are this:
<div class='layout'>
<div class='titleArea'>
<svg viewBox='0 0 100 100' preserveAspectRatio='xMidYMid meet'>
<text x='50%' y='60%' textAnchor='middle' dy='0.07em' class='titleText'>
Hello World
</text>
</svg>
</div>
<div class='mainArea'>
</div>
</div>
and this:
.layout {
height: 400px;
border: 1px solid red;
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr;
-ms-grid-rows: minmax(80px, 1fr) minmax(200px, 3fr);
grid-template-columns: 1fr;
grid-template-rows: minmax(80px, 1fr) minmax(200px, 3fr);
}
.titleArea {
border: 1px solid blue;
width: 100%;
-ms-grid-row: 1;
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr;
-ms-grid-rows: 1fr;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
svg {
height: 100%;
width: 100%;
/* uncomment line below to see text centering work in Edge */
/* max-height: 80px; */
justify-self: center;
align-self: center;
-ms-grid-column: 1;
-ms-grid-row: 1;
}
}
.titleText {
font-size: 2.5em;
}
.mainArea {
border: 1px solid green;
}
The centering and flexing don't quite work in the pen, but it's pretty close, and certainly if viewed in Edge the problem becomes readily apparent.
If I open some dev tools and go and look at what's happening, the height: 100% is getting calculated not as the height of the parent container, but matches what's calculated for the width: 100%, which makes the text render huge and it completely breaks out of the parent container. If I set a max-height value to something within the range of what I expect the flexing to be, it reins everything in and the text ends up where it should, but then it's frozen at that size and doesn't subtly shrink/grow as the viewport changes.
Is there some workaround for this, or is this a legitimate bug in Edge?
The above setup works just fine in Chrome, Firefox, and even IE 11, the issue is only with Edge.
I have reproduced this problem on my machine, it seems that the issue is related to the SVG element's x and y attribute, it will set the starting point of the text baseline based on the whole page, instead of the SVG element. It can be possible that it is some kind of bug or it is Edge default behavior. I will try to submit the feedback regarding this issue.
As a workaround, I suggest you could try to change the CSS units, such as 'px'. And you could modify your code as below:
<style>
.layout {
height: 400px;
border: 1px solid red;
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr;
-ms-grid-rows: minmax(80px, 1fr) minmax(200px, 3fr);
grid-template-columns: 1fr;
grid-template-rows: minmax(80px, 1fr) minmax(200px, 3fr);
}
.titleArea {
border: 1px solid blue;
width: 100%;
-ms-grid-row: 1;
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr;
-ms-grid-rows: 1fr;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
text-align: center;
}
.titleArea svg {
height: 100%;
width: 100%;
/* uncomment line below to see text centering work in Edge */
/* max-height: 80px; */
justify-self: center;
align-self: center;
-ms-grid-column: 1;
-ms-grid-row: 1;
}
.titleText {
font-size: 20px;
}
.mainArea {
border: 1px solid green;
}
</style>
<div class='layout'>
<div class='titleArea'>
<svg width="100%" height="100" preserveAspectRatio='xMidYMid meet'>
<text x='50%' y='50' textAnchor='middle' dy='0.07em' class='titleText'>
Hello World
</text>
</svg>
</div>
<div class='mainArea'>
</div>
</div>
The result as below (using Microsoft Edge 44.18362.387.0 version):