Why justify-content: space-between doesnt work in this case? I want to push the last item to the right edge and center the middle one.
div{
background: lightblue;
width: 8rem;
height: 8rem;
}
main{
margin: 2rem auto;
width: 80%;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5rem 0rem;
background: yellow;
justify-content: space-between;
}
<main>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</main>
You're almost there, but instead of using fractional unit fr, you should use a fixed size 8rem (aligned with your box size).
fr has been stretching your grid box, so that's why you cannot apply justify-content without spare space.
div{
background: lightblue;
width: 8rem;
height: 8rem;
}
main{
margin: 2rem auto;
width: 80%;
display: grid;
grid-template-columns: repeat(3, 8rem); /*Modify 1fr to 8rem*/
gap: 5rem 0rem;
background: yellow;
justify-content: space-between;
}
<main>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</main>
div{
background: lightblue;
padding: 20px 0;
}
main{
display: grid;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 5rem 0rem;
background: yellow;
justify-content: space-between;
}
<main>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</main>
//you must use percent not absolute
Related
I'm trying to achieve this result, using css only: I have a container with a bunch of children inside. I would like the first child to stretch vertically and having the other children to wrap beside the first child.
expected result
this is the code:
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
align-content: center;
width: 350px;
border: 1px solid;
}
.container div {
min-height: 30px;
width: 100px;
background: green;
margin: 3px;
}
.container div:first-child {
background: red;
align-self: stretch;
}
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
this is how it looks
But apparently it is not possible with flexbox. Is there any other solutions?
I know I can achieve this by taking the first child out of the container and treat it separately. But I was wondering if I could do it without changing the markup?
Thank you all!
You need CSS grid for this. Resize the container to see the result:
.container {
display: grid;
grid-template-columns: repeat(auto-fit,100px); /* width of your element */
width: 350px;
border: 1px solid;
/* resize the container*/
overflow: auto;
resize: horizontal;
}
.container div {
min-height: 30px;
background: green;
margin: 3px;
}
.container div:first-child {
background: red;
grid-area:1/1/span 200; /* 1s row/1st column/ take many rows (stretch)*/
}
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
position: absolute can also do the job since the width is fixed:
.container {
display: flex;
flex-wrap: wrap;
padding-left: 106px;
width: 350px;
border: 1px solid;
position: relative;
/* resize the container*/
overflow: auto;
resize: horizontal;
}
.container div {
min-height: 30px;
width: 100px;
background: green;
margin: 3px;
}
.container div:first-child {
background: red;
position: absolute;
inset: 0 auto 0 0;
}
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
I am trying to let divs be inline, but grid-auto-columns does not make auto columns.
* {
margin: 0;
}
html,
body {
width: 100%;
}
#container {
width: 100%;
display: grid;
grid-auto-columns: 12vh;
grid-template-rows: 12vh;
justify-content: space-evenly;
}
#container div {
width: 100%;
height: 100%;
border: 1px solid black;
font-size: 4vh;
font-family: "Bebas Neue Regular";
}
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
I used grid-template-rows and grid-auto-columns, and I expected the each div to follow right behind the previous one as square. However, they were compressed for some reason.
I could make it with grid-template-columns: 12vh 12vh 12vh 12vh;, but I want to do it with grid-auto-columns because the number of boxes can be changed as the number of contents changes.
How can I do this and why does this happen?
Thank you.
* {
margin: 0;
}
html,
body {
width: 100%;
}
#container {
width: 100%;
display: grid;
grid-auto-flow: column;
grid-auto-columns: 12vh;
grid-template-rows: 12vh;
justify-content: space-evenly;
}
#container div {
width: 100%;
height: 100%;
border: 1px solid black;
font-size: 4vh;
font-family: "Bebas Neue Regular";
}
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
I am trying to center a grid display within an element 100% the size of a page, while making a space around the whole grid. I have tried auto margins, but the grid is sticking to the top of the parent. When adding manual margins, the body pushes down the grid's parent element acting as the margin of the grid. I have also tried another div within the parent element spacing the grid halfway down. Is there any way to do this cleaner (without the spacer)?
HTML:
<html>
<body>
<main>
<div class="spacer"></div>
<div class="grid">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</main>
</body>
</html>
CSS:
html, body {
height: 100%;
width: 100%;
}
main {
height: 100%;
width: 100%;
}
.spacer {
height: 10%;
}
.grid {
display: grid;
height: 80%;
width: 90%;
grid-template: 1fr 2fr 1fr / 1fr 2fr 1fr;
margin: auto;
}
JSFIDDLE:
https://jsfiddle.net/593Lovxw/22/
Try this:
html, body {
height: 100%;
width: 100%;
}
main {
background: #f00;
height: 100%;
width: 100%;
display: flex;
vertical-align: center;
}
.spacer {
background: orange;
height: 10%;
}
.grid {
display: grid;
height: 80%;
width: 90%;
grid-template: 1fr 2fr 1fr / 1fr 2fr 1fr;
margin: auto;
}
.grid div {
background: #00f;
border: thick solid black;
}
<html>
<body>
<div class="spacer"></div>
<main>
<div class="grid">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</main>
</body>
</html>
I want to use the new grid module in CSS but it isn't working.
This is the code I have:
div {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
p {
grid-column-start: 2;
grid-row-start: 2;
}
<div>
<p>
Hello World! :D
</p>
</div>
It is working properly, you just need to visualise it better. Try adding some other child elements to your main container, you'd see Hello world is positioned where it should be. It is just because you have empty space all around, you are having difficulty
div.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
background: green;
}
div {
background: yellow;
padding: 10px;
background-clip: content-box;
}
p {
grid-column-start: 2;
grid-row-start: 2;
}
<div class="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p>
Hello World! :D
</p>
</div>
I have been trying to show three columns per row. Is it possible using flexbox?
My current CSS is something like this:
.mainDiv {
display: flex;
margin-left: 221px;
margin-top: 43px;
}
This code puts all content in a single row.
I want to add a constraint to just shows three records per row.
This may be what you are looking for:
http://jsfiddle.net/L4L67/
body>div {
background: #aaa;
display: flex;
flex-wrap: wrap;
}
body>div>div {
flex-grow: 1;
width: 33%;
height: 100px;
}
body>div>div:nth-child(even) {
background: #23a;
}
body>div>div:nth-child(odd) {
background: #49b;
}
<div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Even though the above answer appears to be correct, I wanted to add a (hopefully) more readable example that also stays in 3 columns form at different widths:
.flex-row-container {
background: #aaa;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.flex-row-container > .flex-row-item {
flex: 1 1 30%; /*grow | shrink | basis */
height: 100px;
}
.flex-row-item {
background-color: #fff4e6;
border: 1px solid #f76707;
}
<div class="flex-row-container">
<div class="flex-row-item">1</div>
<div class="flex-row-item">2</div>
<div class="flex-row-item">3</div>
<div class="flex-row-item">4</div>
<div class="flex-row-item">5</div>
<div class="flex-row-item">6</div>
</div>
Hope this helps someone else.
Try this one using Grid Layout:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
font-size: 30px;
text-align: center;
}
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
You can now use grid-auto-flow
https://jsfiddle.net/chalcrow/bqye79kr/1/
CSS
.grid-flow {
display: grid;
grid-auto-flow: row;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
}
Note - the repeat(3, 1fr) means '3 columns, each taking up 1 (equal) fraction of the available space.
HTML
<div class="grid-flow">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
Result