I'm trying to create layout with two columns as nested grid with left grid having dynamic number of columns/width and right column with static width. Left grid can have many columns (from 0 to n) and right grid has static width. I want to center parent of these grids horizontally but adding justify-content: center causes overflow to clip some content of left grid
where I want it to work as here:
https://jsfiddle.net/4o8bkdtm/17/
Where is centered to the full width of the grid parent.
Reproduction
.grid {
background-color: #e2e2e2;
height: 400px;
display: grid;
overflow: auto;
grid-template-columns: max-content 400px;
justify-content: center;
}
.grid-1,
.grid-2 {
border: 1px solid #000000;
display: grid;
grid-template-columns: 1fr;
}
.grid-1 {
direction: rtl;
grid-auto-columns: max-content;
}
.grid-1>div {
width: 150px;
}
<div class="grid">
<div class="grid-1">
<div style="grid-column: 1; grid-row: 1;">first</div>
<div style="grid-column: 2; grid-row: 1;">second</div>
<div style="grid-column: 3; grid-row: 1;">third</div>
</div>
<div class="grid-2">
static width
</div>
</div>
Reproduction fiddle: https://jsfiddle.net/zLmxorgv/5/
Related
I have a grid container with 2 items in each row. The last row has only 1 item and I want it to be centered horizontally and still be as wide as all the other items. Right now, the last item is on the left.
I have tried justify-self: center; on the last item, but it doesn't work.
.container {
width: 700px;
display: grid;
grid-template-columns: repeat(2, 1fr);
column-gap: 40px;
row-gap: 20px;
}
.item {
background-color: #38bdf8;
border-radius: 16px;
height: 50px;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
I want it to be centered horizontally and still be as wide as all the other items.
When it comes to CSS grid, you can't just center something, it has to follow the grid. You defined two columns, and so there's no middle column for the last element to occupy.
To achieve this using CSS grid, we need to add more columns.
.container {
width: 700px;
display: grid;
grid-template-columns: repeat(4, 1fr);
column-gap: 40px;
row-gap: 20px;
}
.item {
background-color: #38bdf8;
border-radius: 16px;
height: 50px;
grid-column: span 2;
}
.item:last-child {
grid-column: 2 / span 2;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
almost there... just add span on last item
.container {
width: 700px;
display: grid;
grid-template-columns: repeat(2, 1fr);
column-gap: 40px;
row-gap: 20px;
}
.item {
background-color: #38bdf8;
border-radius: 16px;
height: 50px;
}
.item:last-child {
grid-column: auto / span 2;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
if you want all items same width, and based on your actual css with container 700px and column-gap 40px, you can modify the last-child css as follow:
.item:last-child {
grid-column: auto / span 2;
width: 330px;
justify-self: center;
}
here we give a real width at the last item equal to others
we keep the span 2 but we justify center this last item
I'm trying to solve a simple task but the solutions seem not be that simple.
Basically, I want many blocks with the same size to be aligned in center but I nee 1 block that is twice bigger than the others.
If I use FLEX - there are blank spaces around the big block.
If I use GRID - I can't align the blocks in the center.
Please help!
#all {
width: 100%;
border: 1px solid #ff0000;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fill, 150px);
grid-template-rows: repeat(auto-fill, 150px);
}
#all div {
width: 150px; height: 150px;
border: 1px solid #ff0000;
}
#all .big {
width: 310px; height: 312px;
grid-column: 2/ 4;
grid-row: 2 / 4;
}
<html>
<head>
<style>
</style>
</head>
<body>
<div id=all>
<div></div>
<div></div>
<div class=big></div>
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>
</body>
</html>
I need them to be aligned in the center
Here is an edited answer from W3Schools.
NOTE: there must be enough items to circle the one in the middle.
First, you need to add odd items in the grid.
Then, add odd columns to align them properly as you want.
Last, use grid-area to start any of the items from the 2nd row and column. then end it at the other corner according to the number of items in the grid.
and the good thing about this is that it's responsive and you can select any item to put it in the middle.
here is the code.
.grid-container {
display: grid;
grid-template-columns: auto auto auto ; /* odd column */
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item5 {
grid-area: 2 / 2 / 4 / 3; /* start and end the selected item */
display: flex;
justify-content: center;
align-items: center;
}
<h1>The grid-column Property</h1>
<p>Use the <em>grid-column</em> property to specify where to place an item.</p>
<div class="grid-container">
<!-- add odd items in the grid -->
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
<div class="item9">9</div>
<div class="item10">10</div>
<div class="item11">11</div>
</div>
Why do I have an overflow on the X axis in the following snippet?
The overflow is generated once I apply grid-gap: 35px on my .box grid container.
.container {
width: 500px;
border: 1px solid #000;
margin: 0 auto;
}
.box {
display: grid;
grid-template-columns: repeat(16, 1fr);
background: #00f;
gap: 35px;
}
.item {
height: 50px;
background: #0f0;
text-align: center;
line-height: 40px;
vertical-align: middle;
}
.span4 {
grid-column: span 4;
}
<div class="container">
<div class="box">
<div class="item span4">A</div>
<div class="item span4">B</div>
<div class="item span4">C</div>
<div class="item span4">D</div>
<div>
</div>
This is because a Grid item(i.e. .container) cannot be smaller than it's contents(all .item combined). Let's consider your case here.
container width = 500px
grid-template-columns is repeating 16 times with gap of 35px each. If we do the math here that would be 560px(16*35) which will be greater than your container width(500px).
To fix this either you increase the container width to 560px or make in percentages i.e. 100%
.container {
width: 100%; /*560px*/
border: 1px solid #000;
margin: 0 auto;
}
.box {
display: grid;
grid-template-columns: repeat(16, 1fr);
background: #00f;
gap: 35px;
}
.item {
height: 50px;
background: #0f0;
text-align: center;
line-height: 40px;
vertical-align: middle;
}
.span4 {
grid-column: span 4;
}
<div class="container">
<div class="box">
<div class="item span4">A</div>
<div class="item span4">B</div>
<div class="item span4">C</div>
<div class="item span4">D</div>
<div>
</div>
Since you have 4 div elements of class="item", in .box you can reduce the number of columns to 4
grid-template-columns: repeat(4, 1fr);
where each .item element spans 1 column.
grid-column: span 1;
This fixes the overflow problem.
As to why this happens:
For a certain column width, the column gap is supposed to take up a percentage of the column. If the column gap is smaller than the column width, then the column gap is by default included in the column. If the column gap is larger than the column width, then the column cannot hold the column gap and has no choice but to overflow. Since the column width which is 500/16 = 31.25 is smaller than the column gap 35, it overflows.
Note: this only seems to happen when an element is spamming more than one column. If an element is spanning one column and the the column gap is greater than column width, then it is constrained at the given column width. Hope this helped :)
Hope this helped :)
I'm trying to display a list of flex items with fixed width in the center of flexbox with wrap by using margin: auto. When wrap happens, the wrapped item also centers in its own container:
Is there a way to keep the wrapped item on the left while everything else is centred?
.parent {
display: flex;
flex-wrap: wrap;
height: 100px;
}
.children {
flex: 1;
border: 1px solid black;
max-width: 300px;
min-width: 300px;
margin: auto;
height: 100px;
margin-top: 1rem;
}
<div class="parent">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
You're saying you want the items centered, but when there is only one item that wraps, you want it left-aligned.
The problem is that there is really no left-alignment in the flex container. Everything is centered, based on available space in the row. So the single item in the last row has no concept of what's going on above, and nothing to align with.
Here's what happens if you left-align the last item (on wider screens):
.parent {
display: flex;
flex-wrap: wrap;
}
.children {
border: 1px solid black;
max-width: 300px;
min-width: 300px;
margin: auto;
height: 100px;
margin-top: 1rem;
}
.children:last-child {
margin-left: 0;
}
<div class="parent">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
What you need is a nested grid structure.
A top-level grid to establish the centering.
A nested grid for the wrapping.
In the demo below, you'll find a three-column grid. The left and right columns are empty (spacer) items, created with CSS pseudo-elements, and set to equal widths. This centers the middle item.
The middle item is also a grid container. Using the repeat() and auto-fill functions, the items can wrap, with individual items aligning left.
body {
display: grid;
grid-template-columns: auto 1fr auto;
}
body::before,
body::after {
content: ''; /* in grid (and flex) layouts, pseudo-elements on the container
are treated as items */
}
.parent {
display: grid;
grid-template-columns: repeat(auto-fill, 300px);
grid-auto-rows: 100px;
grid-gap: 10px;
justify-content: center; /* centers the columns (not the items, like in flex) */
}
.children {
border: 1px solid black;
}
<div class="parent">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
jsFiddle demo
I'm currently working on some layout and I have an issue with image width exceeding the container width when applying CSS grid rules.
Please, take a look at the code below.
img {
max-width: 100%;
height: auto;
display: block;
}
.page {
display: grid;
grid-template-columns: auto minmax(300px, 400px);
grid-column-gap: 1rem;
height: 100vh;
}
.content {
grid-column: 1 / 2;
background: red;
width: 100%;
}
.sidebar {
grid-column: 3;
background: lime;
}
<div class="page">
<div class="content">
<img src="http://via.placeholder.com/900x400" alt="" width="900" height="400">
<p>content</p>
</div>
<div class="sidebar">
<p>sidebar</p>
</div>
</div>
I'm wondering what might cause that issue. The sidebar has to be between 300px and 400px. The content container should take the rest.
For some reason, the image is breaking this layout setup.
https://codepen.io/sunpietro/pen/ZyNgqe
Fixed grid-template-columns. It should be grid-template-columns: auto 1fr minmax(300px, 400px). auto for image, 1fr for content, and minmax(300px, 400px) forsidebar` (it resides in 3rd column). Demo:
img {
max-width: 100%;
height: auto;
display: block;
}
.page {
display: grid;
grid-template-columns: auto 1fr minmax(300px, 400px);
grid-column-gap: 1rem;
height: 100vh;
}
.content {
grid-column: 1 / 2;
background: red;
width: 100%;
}
.sidebar {
grid-column: 3;
background: lime;
}
<div class="page">
<div class="content">
<img src="http://via.placeholder.com/900x400" alt="" width="900" height="400">
<p>content</p>
</div>
<div class="sidebar">
<p>sidebar</p>
</div>
</div>
You have a two-column grid.
grid-template-columns: auto minmax(300px, 400px);
This means there are three grid column lines: 1, 2 and 3.
The image is nested in a grid item which occupies the first column:
.content {
grid-column: 1 / 2;
}
But you have the sidebar skipping the second column and starting at grid column line 3 (the end of the explicit grid):
.sidebar {
grid-column: 3;
}
This breaks down to:
grid-column-start: 3;
grid-column-end: auto;
So there's nothing in the second column.
Try this instead:
.sidebar {
grid-column: 2 / 3;
}
revised codepen