Drawing column/rows in incomplete css3 grid - css

I'm using css-grid to generate a grid like display. But this display does not contain an element for every cell.
Is it possible to draw the outline of every lines/rows using CSS without adding the missing elements?
Example of grid:
.grid {
display: grid;
grid-template-columns: 15px 25px 35px;
grid-template-rows: 30px 20px 10px;
grid-gap: 5px 5px;
}
.cell1 {
grid-area: 1 / 1 / span 1 / span 1;
background: green;
}
.cell2 {
grid-area: 3 / 3 / span 1 / span 1;
background : blue;
}
<div class="grid">
<div class="cell1"></div>
<div class="cell2"></div>
</div>

As I said in the comment, you can use background linear gradients, without css-grid
.grid {
display: grid;
grid-template-columns: 25px 25px 25px;
grid-template-rows: 25px 25px 25px;
grid-gap: 5px 5px;
background-image: repeating-linear-gradient(0deg,transparent,transparent 25px,#CCC 25px,#CCC 30px),repeating-linear-gradient(-90deg,transparent,transparent 25px,#CCC 25px,#CCC 30px);
background-size: 30px 30px;
background-position: -5px -5px;
width: 85px;
height: 85px;
}
.cell1 {
grid-area: 1 / 1 / span 1 / span 1;
background: green;
}
.cell2 {
grid-area: 3 / 3 / span 1 / span 1;
background : blue;
}
<div class="grid">
<div class="cell1"></div>
<div class="cell2"></div>
</div>

Seems like you have to add the remaining cells but just dont add a class.
I think you need the DOM element to render css without sth like canvas.
.grid {
display: grid;
grid-template-columns: 25px 25px 25px;
grid-template-rows: 25px 25px 25px;
grid-gap: 0;
}
.grid > * {
border: 1px solid rgb(137,153,175);
}
.cell1 {
grid-area: 1 / 1 / span 1 / span 1;
background: green;
}
.cell2 {
grid-area: 3 / 3 / span 1 / span 1;
background : blue;
}
<div class="grid">
<div class="cell1"></div>
<div class="cell2"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>

Related

How to make my grid cell same size after spanding it over the next cell?

enter image description here
How do I make my grid cell same size after spanning it over the next cell? This is what I have tried so far
.factory {
display: grid;
grid-template-columns: 1fr 1fr 2fr;
height: 600px;
gap: 10px;
margin-top: 100px;
padding: 10px;
}
.factory>div {
background-color: gray;
border: 1px green solid;
}
.image-1 {
width: 100%;
height: auto;
}
.div-3 {
grid-column: 3 / span 5;
}
.div-5 {
grid-column: 1 / span 2;
}
<div class="factory">
<div class="div-1">box1</div>
<div class="div-2">box2</div>
<div class="div-3">box3</div>
<div class="div-5">box5</div>
<div class="div-6">box6</div>
</div>
As you can see box6 is smaller than the rest of the boxes.
Your issue is that on div-3 you try to span 5 columns after it has started at column 3 which will make the browser attempt to add 5 more columns to the grid.
.div-3 {
grid-column: 3 / span 5;
}
You can either remove this section completely because you have already specified in your grid-template-columns: 1fr 1fr 2fr; that the third column should be double the space of the first two.
Another option would be to span 1
.div-3 {
grid-column: 3 / span 1;
}
You can see this clearly by using the inspection tools.
.factory {
display: grid;
grid-template-columns: 1fr 1fr 2fr;
height: 600px;
gap: 10px;
margin-top: 100px;
padding: 10px;
}
.factory>div {
background-color: gray;
border: 1px green solid;
}
.image-1 {
width: 100%;
height: auto;
}
/*.div-3 {
grid-column: 3 / span 5;
}*/
.div-5 {
grid-column: 1 / span 2;
}
<div class="factory">
<div class="div-1">box1</div>
<div class="div-2">box2</div>
<div class="div-3">box3</div>
<div class="div-5">box5</div>
<div class="div-6">box6</div>
</div>

grid-row-gap is adding extra height to first (2-row) element in grid layout

I'm trying to create a simple layout, where I have a grid made up of three rows. I want my first element to take up 2 rows, and the second element to take up the remaining row. That works, but when I introduce a grid-row-gap: 30px;, it seems to add an additional 30px to the height of my first element.
<div id="grid">
<div id="one">1 - height is actually 332px</div>
<div id="two">2 - height is 150px, which I'd expect</div>
</div>
<style>
body {
background: #000;
color: #fff;
}
#grid {
display: grid;
grid-template-columns: 1;
grid-template-rows: repeat(3, 150px);
grid-row-gap: 30px; // the problem with this is that it takes space away from the lower rows, but not the top ones. seeming to add height to the top section
height: calc(450px + 30px);
width: 600px;
}
#grid div {
margin: 0;
padding: 0.75rem;
border: 3px solid #FFF;
border-radius: 12px;
overflow: hidden;
background: #3A3A3A;
}
#one {
grid-row: span 2;
max-height: 300px;
}
#two {
grid-row: span 1;
}
</style>
I made an example codepen at https://codepen.io/grayayer/pen/xxzQWex
Seems like this should be simpler than it is.
I was expecting the first element to be 300px high, but it's 330px high. The second element is 150px as expected.
I've added box-sizing: border-box; — perhaps this reduces the confusion? Remember that with the default box-sizing method, the size of your boxes does not include any padding.
https://css-tricks.com/box-sizing/
But the basic equation is pretty simple: you have two rows which are each 150px high, plus you have a 30px gap between them, so the height of an element which spans both rows will be 150px + 30px + 150px = 330px.
body {
background: #000;
color: #fff;
margin: 30px;
}
#sponsors_grid {
display: grid;
grid-template-columns: 2;
grid-template-rows: repeat(3, 150px);
gap: 30px;
}
#sponsors_grid div {
margin: 0;
padding: 0.75rem;
border: 3px solid #FFF;
border-radius: 12px;
overflow: hidden;
background: #3A3A3A;
box-sizing: border-box;
}
#one {
grid-column: 1;
grid-row: 1 / span 2;
}
#two {
grid-column: 1;
grid-row: 3;
}
#three {
grid-column: 2;
grid-row: 1;
}
#four {
grid-column: 2;
grid-row: 2;
}
#five {
grid-column: 2;
grid-row: 3;
}
<div id="sponsors_grid">
<div id="one">1 - 330px</div>
<div id="two">2 - 150px</div>
<div id="three">3 - 150px</div>
<div id="four">4 - 150px</div>
<div id="five">5 - 150px</div>
</div>

grid cell gets pushed down to the next row

So I just started trying out the grid display, but for some reason with this basic of a code.
div{
padding: 50px;
margin: 10px;
border: 5px solid black;
}
.grid{
display: grid;
grid-template-columns: auto auto auto;
}
.grid-header{grid-column: 1 / span 3;}
.grid-main{grid-column: 1 / span 2;}
I keep getting this as a result though.
Result
but what I want is for it to look like this
Intended
Here's the full code if it helps.
<html>
<head>
<style>
div {
padding: 50px;
margin: 10px;
border: 5px solid black;
}
.grid {
display: grid;
grid-template-columns: auto auto auto;
}
.grid-header {
grid-column: 1 / span 3;
}
.grid-main {
grid-column: 1 / span 2;
}
</style>
</head>
<body>
<div class="grid">
<div class="grid-header"></div>
<div></div>
<div class="grid-main"></div>
<div class="grid-main"></div>
<div></div>
</div>
</body>
</html>
I have changed one div's class grid-main to grid-main2. Let me know if face any issue
<html>
<head>
<style>
div {
padding: 50px;
margin: 10px;
border: 5px solid black;
}
.grid {
display: grid;
grid-template-columns: auto auto auto;
}
.grid-header {
grid-column: 1 / span 3;
}
.grid-main {
grid-column: 2 / span 2;
}
.grid-main2 {
grid-column: 1 / span 2;
}
</style>
</head>
<body>
<div class="grid">
<div class="grid-header"></div>
<div></div>
<div class="grid-main"></div>
<div class="grid-main2"></div>
<div></div>
</div>
</body>
</html>
One more solution is possible with your existing html without changing class.
Here is the css:
div {
padding: 50px;
margin: 10px;
border: 5px solid black;
}
.grid {
display: grid;
grid-template-columns: auto auto auto;
}
.grid-header {
grid-column: 1 / span 3;
}
.grid-main:nth-child(3) {
grid-column: 2 / span 2;
}
.grid-main:nth-child(4) {
grid-column: 1 / span 2;
}

css-grid: place element "outside" of grid

I am new to css grid I try to achieve the layout from the image attached where ONE element DIV 4 is wider than the grid layout. I try to avoid to close the grid-div before DIV 4 and then reopen the grid after DIV 4 again so I can controll the appearance of each grid element and how it is displayed through ONE css-class only and it won't need a different div-structure.
https://codepen.io/anon/pen/RBdjbd
.grid-2er {
grid: auto-flow dense / 1fr 1fr;
display: grid;
grid-gap: 20px;
grid-auto-rows: auto;
}
.grid-2er .halfwidth {
grid-column: 1 / -1;
}
.grid-2er .fullwidth {
grid-column: 1 / -1;
}
Might I suggest a four column grid
grid-template-columns: 1fr minmax(0, 400px) minmax(0, 400px) 1fr;
Codepen Demo
* {
margin: 0;
padding: 0;
text-decoration: none;
outline: none;
font-weight: 300;
border: none;
font-family: "Source Sans Pro", sans-serif;
text-align: center;
}
*,
*::after,
*::before {
box-sizing: border-box;
}
.grid-2er {
grid-template-columns: 1fr minmax(0, 400px) minmax(0, 400px) 1fr;
display: grid;
grid-gap: 20px;
grid-auto-rows: auto;
}
.grid-2er * {
background: blue;
color: white
}
.grid-2er .mainwidth {
grid-column: 2 / 4;
}
.grid-2er .halfwidth {
grid-column: 2;
}
.halfwidth+.halfwidth {
grid-column: 3;
}
.grid-2er .fullwidth {
grid-column: 1 / -1;
}
<div class="grid-2er">
<div class="mainwidth">DIV 1</div>
<div class="halfwidth">DIV 2</div>
<div class="halfwidth">DIV 3</div>
<div class="fullwidth">DIV 4</div>
<div class="halfwidth">DIV 5</div>
<div class="halfwidth">DIV 6</div>
</div>
You can use negative margin. If the width of the whole grid is maximized to 800px then you can have a negative margin of (800px - 100vw)/2 on each side. Then when the size of the window is less than 800px you reset margin to 0:
Here is an example (I used 600px in this case)
* {
margin: 0;
padding: 0;
font-weight: 300;
border: none;
font-family: "Source Sans Pro", sans-serif;
text-align: center;
padding: 10px;
}
*,
*::after,
*::before {
box-sizing: border-box;
}
.grid-2er {
grid: auto-flow dense / 1fr 1fr;
display: grid;
grid-gap: 20px;
grid-auto-rows: auto;
max-width: 600px;
margin: auto;
}
.grid-2er .halfwidth {
grid-column: 1 / -1;
background: blue;
color: white
}
.grid-2er .fullwidth {
grid-column: 1 / -1;
background: blue;
color: white
}
.outside {
margin: 0 calc((600px - 100vw)/2);
}
#media only screen and (max-width: 600px) {
.outside {
margin: 0;
}
}
#media only screen and (min-width: 480px) {
.grid-2er .halfwidth {
grid-column: auto;
}
.grid-2er .fullwidth .tile {
width: 50%;
}
}
<div class="grid-2er">
<div class="fullwidth ">
DIV 1
</div>
<div class="halfwidth">
DIV 2
</div>
<div class="halfwidth">
DIV 3
</div>
<div class="fullwidth outside">
DIV 4
</div>
<div class="halfwidth">
DIV 5
</div>
<div class="halfwidth">
DIV 6
</div>
</div>
Use calc to get the value of your gutter. The width of the gutter is half the width of it's container minus half the width of the content container. In your case the math is calc(50% - 400px). I like to add a minmax to keep the gutter from collapsing completely but you can ignore that if you don't need it. This frees you up to use 1fr for your interior columns so they are responsive and you can change the number of columns without having to recalculate their widths.
.content {
display: grid;
grid-template-columns: calc(50% - 400px) repeat(2, 1fr) calc(50% - 400px);
gap: 1rem;
}
.column {
text-align: center;
background-color: #ddd;
}
.--column1 {
grid-column: 2 / 3;
}
.--column2 {
grid-column: 3 / 4;
}
.--columnfull {
grid-column: 1 / 5;
}
<div class="container">
<div class="content">
<div class="column --column1">column</div>
<div class="column --column2">column</div>
<div class="column --columnfull">column</div>
</div>
</div>

grid-auto-columns does not completely work in Firefox

I don't understand why DIV 3 is not the same size as DIV 1 + DIV 2.
https://codepen.io/anon/pen/vaVqPW
.grid {
display: grid;
grid-auto-columns: 1fr 1fr; /* I also tried 50% 50% */
grid-gap: 20px;
}
Firefox 61 should support css grid according to caniuse
https://caniuse.com/#feat=css-grid
* {
box-sizing: border-box;
}
.grid {
display: grid;
grid-auto-columns: 1fr 1fr;
grid-gap: 20px;
}
.content {
grid-column: 1;
background: red;
}
.sidebar {
grid-column: 2;
background: blue;
}
.grid>* {
/*border: 1px dashed red; */
/* demo only */
}
.box {
width: 50%;
height: 75px;
background-color: black;
color: #fff;
padding: 20px;
position: relative;
float: left;
}
.box100 {
width: 100%;
height: 75px;
color: #fff;
padding: 20px;
}
.box.arrow-left:after {
content: " ";
position: absolute;
left: -15px;
margin-top: -15px;
top: 50%;
border-top: 15px solid transparent;
border-right: 15px solid black;
border-left: none;
border-bottom: 15px solid transparent;
}
<div class="grid">
<div class="content">
<div class="box" style="background:gray">
DIV 1 (50% of column 1)
</div>
<div class="box arrow-left">
DIV 2 (50% of column 1)
</div>
</div>
<div class="sidebar">
<div class="box100">DIV 3 (100% of column 2)</div>
</div>
</div>
<div>
<div class="content" style="background:tomato">
<p>content 4 (100% of column 1 + GAP + 100% of column 2 )</p>
</div>
</div>
Firefox does indeed support CSS Grid (see caniuse.com).
The problem is that Firefox does not appear to support multiple values in grid-auto-columns.
This is your code in Chrome. No problems.
This is your code in Firefox. There's a problem. The code is invalid / not recognized.
It fails in Firefox here, too:
The grid-auto-columns property can take multiple values, per the spec definition. However, Firefox appears to lack support for this set-up. It only accepts a single value.
Your correction to the problem, as stated in your answer and copied below, is simply to switch from implicit columns (grid-auto-columns) to explicit columns (grid-template-columns).
grid: auto-flow dense / 1fr 1fr;
The grid property is a shorthand property that allows you to set all explicit and implicit rules in a single declaration. Your rule above breaks down to this:
So in the end, it appears that a simple switch from grid-auto-columns to grid-template-columns was all you needed.
grid: auto-flow dense / 1fr 1fr;
this seems to solve the Problem!
At the same time deleting the line:
grid-auto-columns: 1fr 1fr;
Pen is updated: https://codepen.io/anon/pen/vaVqPW

Resources