I have a CSS Grid layout in which I specified the starting/ending rows and columns for each div as you can see below with the CSS
.grid-container {
width: 100%;
height: 100%;
display: grid;
grid-gap: 0;
}
.grid-item {
position: relative;
display: flex;
padding: 0;
border-radius: 10px;
margin: 10px;
background-color: #f6f6f6;
}
.g-1 {
margin-top: 20px;
margin-left: 20px;
background-color: white;
grid-column: 1/10;
grid-row: 1/2;
}
.g-2 {
margin-top: 20px;
margin-right: 20px;
margin-bottom: 20px;
grid-column: 10/14;
grid-row: 1/40;
}
.g-3 {
margin-left: 20px;
background-color: #eaf0ff;
grid-column: 1/4;
grid-row: 2/12;
}
.g-4 {
background-color: #ebe3ff;
grid-column: 4/7;
grid-row: 2/12;
}
.g-5 {
background-color: #dff6db;
grid-column: 7/10;
grid-row: 2/12;
}
.g-6 {
margin-left: 20px;
margin-bottom: 20px;
grid-column: 1/5;
grid-row: 12/40;
}
.g-7 {
grid-column: 5/10;
grid-row: 12/28;
}
.g-8 {
margin-bottom: 20px;
grid-column: 5/10;
grid-row: 28/40;
}
which results in this
However, when I then add a simple h1 inside of the blue container .g-3 it expands both vertically and horizontally despite being told that its columns and rows are set to 1/4 and 2/12 respectively.
<div className="view">
<div className="grid-container">
<div className="grid-item g-1">
<h1 className="header">Hello, Levi</h1>
</div>
<div className="grid-item g-2"></div>
<div className="grid-item g-3">
<h2>Testing Size</h2>
</div>
<div className="grid-item g-4"></div>
<div className="grid-item g-5"></div>
<div className="grid-item g-6"></div>
<div className="grid-item g-7"></div>
<div className="grid-item g-8"></div>
</div>
</div>
How can I prevent the contents of the grid items from expanding them from their original layout as seen in the first screenshot, especially when considering there is still plenty of room for the text?
you didn't define any sizing for your columns so the content will define this and you will have a different layout each time you update the content.
You need to define an explicit size for the columns:
.grid-container {
height: 100vh;
min-height:600px;
display: grid;
grid-gap: 0;
grid-auto-columns:1fr; /* this should do the job and force all the columns to be equal */
/* you can also try minmax(0,1fr) (related: https://stackoverflow.com/a/52861514/8620333) */
}
.grid-item {
position: relative;
display: flex;
padding: 0;
border-radius: 10px;
margin: 10px;
background-color: #f6f6f6;
}
.g-1 {
margin-top: 20px;
margin-left: 20px;
background-color: white;
grid-column: 1/10;
grid-row: 1/2;
}
.g-2 {
margin-top: 20px;
margin-right: 20px;
margin-bottom: 20px;
grid-column: 10/14;
grid-row: 1/40;
}
.g-3 {
margin-left: 20px;
background-color: #eaf0ff;
grid-column: 1/4;
grid-row: 2/12;
}
.g-4 {
background-color: #ebe3ff;
grid-column: 4/7;
grid-row: 2/12;
}
.g-5 {
background-color: #dff6db;
grid-column: 7/10;
grid-row: 2/12;
}
.g-6 {
margin-left: 20px;
margin-bottom: 20px;
grid-column: 1/5;
grid-row: 12/40;
}
.g-7 {
grid-column: 5/10;
grid-row: 12/28;
}
.g-8 {
margin-bottom: 20px;
grid-column: 5/10;
grid-row: 28/40;
}
<div class="grid-container">
<div class="grid-item g-1">
<h1 class="header">Hello, Levi</h1>
</div>
<div class="grid-item g-2"></div>
<div class="grid-item g-3">
<h2>Testing Size</h2>
</div>
<div class="grid-item g-4"></div>
<div class="grid-item g-5"></div>
<div class="grid-item g-6"></div>
<div class="grid-item g-7"></div>
<div class="grid-item g-8"></div>
</div>
Related
I'm trying to make an "example page" of all the layouts (so one under the other) How do I place a grid wrapper under a flex container and not be shown in the same line? if I remove the display: flex it automatically goes under but flex remains in the same line.
And why do they both have the same salmon background color?
Thanks.
.flex-wrapper {
display: flex;
border: 5px solid rgb(0, 0, 0);
}
.flex-wrapper>div {
padding: 20px;
margin: 5px;
background-color: salmon;
}
/* grid */
.grid-wrapper {
display: grid;
border: 5px solid purple;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 100px;
gap: 10px;
}
.grid-wrapper>div {
padding: 20px;
margin: 5px;
border-radius: 5px;
background-color: aquamarine;
}
.box1 {
grid-column: 2 / 4;
grid-row: 1;
}
.box2 {
grid-column: 1;
grid-row: 1 / 3;
}
.box3 {
grid-row: 2;
grid-column: 3;
}
<h1>Flexbox Layout</h1>
<div class="flex-wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<!--Grid-->
<div class="grid-wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
<div class="box6">Six</div>
</div>
</div>
Just wrap the boxes of box in a container and put the add a flex-direction to column property in your flex-wrapper css class selector
.flex-wrapper {
display: flex;
flex-direction: column;
border: 5px solid rgb(0, 0, 0);
}
.flex-wrapper .box-container > div {
padding: 20px;
margin: 5px;
background-color: salmon;
}
/* grid */
.grid-wrapper {
display: grid;
border: 5px solid purple;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 100px;
gap: 10px;
}
.grid-wrapper>div {
padding: 20px;
margin: 5px;
border-radius: 5px;
background-color: aquamarine;
}
.box1 {
grid-column: 2 / 4;
grid-row: 1;
}
.box2 {
grid-column: 1;
grid-row: 1 / 3;
}
.box3 {
grid-row: 2;
grid-column: 3;
}
<h1>Flexbox Layout</h1>
<div class="flex-wrapper">
<div class="box-container">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
</div>
<!--Grid-->
<div class="grid-wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
<div class="box6">Six</div>
</div>
</div>
why do they both have the same salmon background color?
Because .flex-wrapper > div applies to every div that's an immediate child of flex-wrapper.
How do I place a grid wrapper under a flex container and not be shown in the same line?
You could add a flex-wrap rule to your flex-wrapper and set the grid item to be wide enough to wrap, as in the example below, but you might consider whether your outer container should be a grid instead of flex. You'd have more control that way.
.flex-wrapper {
display: flex;
border: 5px solid rgb(0, 0, 0);
flex-wrap: wrap; /* <=== */
}
.flex-wrapper>div {
padding: 20px;
margin: 5px;
background-color: salmon;
}
/* grid */
.grid-wrapper {
display: grid;
border: 5px solid purple;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 100px;
gap: 10px;
flex: 1 1 100%; /* <=== */
}
.grid-wrapper>div {
padding: 20px;
margin: 5px;
border-radius: 5px;
background-color: aquamarine;
}
.box1 {
grid-column: 2 / 4;
grid-row: 1;
}
.box2 {
grid-column: 1;
grid-row: 1 / 3;
}
.box3 {
grid-row: 2;
grid-column: 3;
}
<h1>Flexbox Layout</h1>
<div class="flex-wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<!--Grid-->
<div class="grid-wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
<div class="box6">Six</div>
</div>
</div>
I'm trying to create a section on my html where I can show some grids. Can you help me with this please? I'm attaching a pic where I'm showing what I want to do. Yellow color is how it is right now, and blue is the final result.
.wrapper {
display: grid;
grid-gap: 20px;
justify-content: center;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.a {
grid-column: 1 / 3;
grid-row: 1;
}
.b {
grid-column: 3;
grid-row: 1 / 3;
}
.c {
grid-column: 1;
grid-row: 2;
}
.d {
grid-column: 2;
grid-row: 2;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
</div>
I am asking this for my own education, if it is something that is bad practice feel free to say so.
I want elements #one #two #three and #four to have their parent property grid-template-rows: 100px 100px 100px 100px overwritten by setting individual grid-row settings to each element.
Elements #one and #two are responding as I expect when I set them like this:
#one{
grid-row: 1/ span 4 ;
}
#two{
grid-row: 1/ span 4 ;
}
However, when I attempt to apply similar settings to #three and #four I do not get the result I expect. I expect the height of #three and #four to be exactly the same as #one and #two. Instead they fly off to the side of the page.
I expected this to display the elements similar to #one and #two. It doesn't work:
#three{
grid-row: 2/ span 4 ;
}
#four{
grid-row: 2/ span 4 ;
}
I want to know how to fix this and the correct individual values to set #three and #four to overwrite grid-template-rows as described.
Code: https://jsfiddle.net/ek2r6a19/
* {
font-family: Helvetica;
font-size: 1em;
color: white;
text-align: center;
}
img {
display: block;
margin: 20px 20px;
margin-left: auto;
margin-right: auto;
border-radius: 50%;
border-width: 5px;
border-style: solid;
border-color: rgb(141, 110, 99);
max-width: 50%;
height: auto;
}
/*_____________________________GRID */
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px 100px 100px 100px 100px 100px;
grid-gap: 10px;
}
.container div:nth-child(even) {
background-color: orange;
}
.container div:nth-child(odd) {
background-color: purple;
}
#one {
grid-row: 1/ span 4;
}
#two {
grid-row: 1/ span 4;
}
/*
I expected this to display the elements similar to #one and #two. It doesn't work
#three{
grid-row: 2/ span 4 ;
}
#four{
grid-row: 2/ span 4 ;
}
*/
<div class="container">
<div id="one">1
<div class="content-container">
</div>
</div>
<div id="two">
2
<div class="content-container">
text
</div>
</div>
<div id="three">3
<div class="content-container">
text
</div>
</div>
<div id="four">4
<div class="content-container">
text
</div>
</div>
<div id="five">5
<div class="content-container">
text
</div>
</div>
<div id="six">6
<div class="content-container">
text
</div>
</div>
</div>
You need to override the grid auto-placement algorithm, which calculates the "right" places to render grid areas, when they're not explicitly defined.
You're defining the rows. Just define the columns, as well.
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px 100px 100px 100px 100px 100px;
grid-gap: 10px;
}
#one {
grid-row: 1 / span 4;
grid-column: 1; /* new */
}
#two {
grid-row: 1 / span 4;
grid-column: 2; /* new */
}
#three {
grid-row: 2 / span 4;
grid-column: 1; /* new */
}
#four {
grid-row: 2 / span 4;
grid-column: 2; /* new */
}
* {
font-family: Helvetica;
font-size: 1em;
color: white;
text-align: center;
}
img {
display: block;
margin: 20px 20px;
margin-left: auto;
margin-right: auto;
border-radius: 50%;
border-width: 5px;
border-style: solid;
border-color: rgb(141, 110, 99);
max-width: 50%;
height: auto;
}
.container div:nth-child(even) {
background-color: orange;
}
.container div:nth-child(odd) {
background-color: purple;
}
<div class="container">
<div id="one">1
<div class="content-container"></div>
</div>
<div id="two">2
<div class="content-container">text</div>
</div>
<div id="three">3
<div class="content-container">text</div>
</div>
<div id="four">4
<div class="content-container">text</div>
</div>
<div id="five">5
<div class="content-container">text</div>
</div>
<div id="six">6
<div class="content-container">text</div>
</div>
</div>
You'll notice that #one and #three, and #two and #four, don't appear to be the same height. Actually, they are.
The issue is that you've set #three and #four to start spanning at grid row line 2.
Well, #one and #two are set to start at grid row line 1, and span 4 rows, so they are overlapping #three and #four (or vice versa). You would need to start #three and #four at grid row line 5.
.container {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px 100px 100px 100px 100px 100px;
grid-gap: 10px;
grid-auto-rows: 100px; /* added to accommodate more rows */
}
#one {
grid-row: 1 / span 4;
grid-column: 1;
}
#two {
grid-row: 1 / span 4;
grid-column: 2;
}
#three {
grid-row: 5 / span 4;
grid-column: 1;
}
#four {
grid-row: 5 / span 4;
grid-column: 2;
}
* {
font-family: Helvetica;
font-size: 1em;
color: white;
text-align: center;
}
img {
display: block;
margin: 20px 20px;
margin-left: auto;
margin-right: auto;
border-radius: 50%;
border-width: 5px;
border-style: solid;
border-color: rgb(141, 110, 99);
max-width: 50%;
height: auto;
}
.container div:nth-child(even) {
background-color: orange;
}
.container div:nth-child(odd) {
background-color: purple;
}
<div class="container">
<div id="one">1
<div class="content-container"></div>
</div>
<div id="two">2
<div class="content-container">text</div>
</div>
<div id="three">3
<div class="content-container">text</div>
</div>
<div id="four">4
<div class="content-container">text</div>
</div>
<div id="five">5
<div class="content-container">text</div>
</div>
<div id="six">6
<div class="content-container">text</div>
</div>
</div>
I start learning working with the CSS grid. I am making a grid there is looking like this. I need the item 3 and 4 to align with item 1 horizontally.
The height on all items should give 700px, so that part should fit. I am thinking if I am doing something wrong in the code regarding the rows and columns?
.wrapper {
display: grid;
grid-template-columns: repeat(11, 1fr);
grid-gap: 1em;
}
.wrapper>div {
background-color: #eee;
padding: 1em;
}
.wrapper>div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
height: 340px;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
height: 350px;
}
.item4 {
grid-row: 2/3;
grid-column: 9/12;
height: 350px;
}
<div class="wrapper">
<div class="item1">
This is item 1
</div>
<div class="item2">
This is item 2
</div>
<div class="item3">
This is item 3
</div>
<div class="item4">
This is item 4
</div>
</div>
A couple of changes should help. Firstly, change the grid-gap on the wrapper from 1em to 10px. This helps with the gap issue with 1em usually being 16px by default. Then just add box-sizing: border-box; to the .wrapper > div.
Here's a working example:
.wrapper {
display:grid;
grid-template-columns:repeat(11,1fr);
grid-gap: 10px;
}
.wrapper > div {
background-color: #eee;
padding: 1em;
box-sizing: border-box;
}
.wrapper > div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
height: 340px;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
height: 350px;
}
.item4 {
grid-row:2/3;
grid-column: 9/12;
height: 350px;
}
<div class="wrapper">
<div class="item1">
This is item 1
</div>
<div class="item2">
This is item 2
</div>
<div class="item3">
This is item 3
</div>
<div class="item4">
This is item 4
</div>
</div>
Your problem was the padding:1em on each of the grid elements. This makes them bigger than you expect.
I've amended your example below. I hope this helps :-)
.wrapper {
display: grid;
grid-template-columns: repeat(11, 1fr);
grid-gap: 1em;
}
.wrapper > div {
background-color: #eee;
padding: 1em;
}
.wrapper > div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
}
.item4 {
grid-row: 2/3;
grid-column: 9/12;
}
<div class="wrapper">
<div class="item1">This is item 1</div>
<div class="item2">This is item 2</div>
<div class="item3">This is item 3</div>
<div class="item4">This is item 4</div>
</div>
So I've been trying to put an image inside a grid but its causing me problems.
Right now, my biggest issue is that is pushing another grid item down.
body {
padding: 0;
margin: 0;
}
.main {
width: 100%;
height: 100vh;
margin: 0;
padding: 0;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
}
.main-bar {
grid-row: 1/16;
grid-column: 4/21;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
}
.main-info {
grid-column: 1/21;
grid-row: 1/21;
background: #333;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
}
.header-title {
grid-column: 3;
grid-row: 2/8;
background: #000;
}
.business {
grid-column: 17;
}
.side-bar {
background: #fff;
grid-row: 1/21;
grid-column: 1/4;
display: grid;
grid-template-rows: repeat(10, 1fr);
border-right: 1px solid #0F6B99;
}
.side-bar img {
width: 100%;
height: auto;
display: block;
margin-left: auto;
margin-right: auto;
margin-top: -24px;
}
.home-button {
padding: 20px;
text-align: center;
background: #0F6B99;
grid-row: 3/4;
}
.buy-button {
padding: 20px;
text-align: center;
background: #59B3B3;
grid-row: 4/5;
}
.sell-button {
padding: 20px;
text-align: center;
background: #8FCCB8;
grid-row: 5/6;
}
.rent-button {
padding: 20px;
text-align: center;
background: #B8E6B8;
grid-row: 6/7;
}
.article1 {
background: #e6174b;
grid-row: 16/21;
grid-column: 4/11;
}
.article2 {
background: #8FCCB8;
grid-row: 16/21;
grid-column: 11/18;
}
.article3 {
background: #B8E6B8;
grid-row: 16/21;
grid-column: 18/21;
}
<div class="main">
<div class="main-bar">
<div class="main-info">
<img class="business" src="http://pngimg.com/uploads/businessman/businessman_PNG6564.png" alt="">
<div class="header-title">High Quality Realstate Asistance</div>
</div>
</div>
<div class="side-bar">
<!--<img src="img/logo.png" alt="">-->
<div class="home-button">
Home
</div>
<div class="buy-button">
Buy
</div>
<div class="sell-button">
Sell
</div>
<div class="rent-button">
Rent
</div>
</div>
<div class="article1">
</div>
<div class="article2">
</div>
<div class="article3">
</div>
</div>
The image in question has a class as business and the item is pushing down has a class as header-title. Header-title should be inside main-info, but when 'business' appears, it pushes header-title down!
!
The issue here is that your image with the business class is overflowing its own grid and the grid of its container.
In order to resolve this add the property overflow: hidden to both the .main-info class and the .business class.
These classes also need the "display: grid" property so the browser can process the grid-column and grid-row property accordingly for those two classes.
Once those additions are made you can tweak the grid-row and grid-column for the the .business class and the .header-title classes accordingly to find your desired positions.
Full CSS and HTML Below:
body {
padding: 0;
margin: 0;
}
.main {
width: 100%;
height: 100vh;
margin: 0;
padding: 0;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
}
.main-bar {
grid-row: 1/16;
grid-column: 4/21;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
}
.main-info {
grid-column: 1/21;
grid-row: 1/21;
background: #333;
display: grid;
grid-template: repeat(20, 1fr) / repeat(20, 1fr);
overflow: hidden;
}
.header-title {
grid-column: 3;
grid-row: 2/8;
background: #000;
display: grid;
}
.business {
grid-column: 17;
overflow: hidden;
display: grid;
}
.side-bar {
background: #fff;
grid-row: 1/21;
grid-column: 1/4;
display: grid;
grid-template-rows: repeat(10, 1fr);
border-right: 1px solid #0F6B99;
}
.side-bar img {
width: 100%;
height: auto;
display: block;
margin-left: auto;
margin-right: auto;
margin-top: -24px;
}
.home-button {
padding: 20px;
text-align: center;
background: #0F6B99;
grid-row: 3/4;
}
.buy-button {
padding: 20px;
text-align: center;
background: #59B3B3;
grid-row: 4/5;
}
.sell-button {
padding: 20px;
text-align: center;
background: #8FCCB8;
grid-row: 5/6;
}
.rent-button {
padding: 20px;
text-align: center;
background: #B8E6B8;
grid-row: 6/7;
}
.article1 {
background: #e6174b;
grid-row: 16/21;
grid-column: 4/11;
}
.article2 {
background: #8FCCB8;
grid-row: 16/21;
grid-column: 11/18;
}
.article3 {
background: #B8E6B8;
grid-row: 16/21;
grid-column: 18/21;
}
<div class="main">
<div class="main-bar">
<div class="main-info">
<img class="business" src="http://pngimg.com/uploads/businessman/businessman_PNG6564.png" alt="">
<div class="header-title">High Quality Realstate Asistance</div>
</div>
</div>
<div class="side-bar">
<!--<img src="img/logo.png" alt="">-->
<div class="home-button">
Home
</div>
<div class="buy-button">
Buy
</div>
<div class="sell-button">
Sell
</div>
<div class="rent-button">
Rent
</div>
</div>
<div class="article1">
</div>
<div class="article2">
</div>
<div class="article3">
</div>
</div>