I've a css grid and multiple items inside it. Right know it's covering the whole width possible. What I want is to limit the width of the container (such as making it only 700px) instead of covering the whole width.
I've tried to use width or max-width properties for the container but none of them is successful. The content inside the container overflows. I should be missing something but do not know what.
Here is the codepen:
https://codepen.io/sercanyilmaz/full/YaRmPN/
#import url('https://fonts.googleapis.com/css?family=Muli');
#import url('https://fonts.googleapis.com/icon?family=Material+Icons');
.wrapper {
display: grid;
grid-template-columns: repeat(350, 1fr);
grid-template-rows: repeat(33, 1fr);
grid-gap: 4px;
grid-row-gap: 5px;
background-color: #a5adb0;
border-radius: 5px;
color: #ddd;
padding: 15px;
font-family: 'Muli';
}
.func {
grid-column: span 25;
grid-row: span 3;
border-radius: 5px;
background: #20252A;
display: grid;
align-items: center;
justify-items: center;
border-bottom: 5px solid #000;
cursor: pointer;
}
.num {
grid-column: span 24;
grid-row: span 6;
border-radius: 5px;
background: #20252A;
display: grid;
align-items: center;
justify-items: center;
border-bottom: 5px solid #000;
cursor: pointer;
}
.back {
grid-column: span 38;
}
.letters {
grid-column: span 24;
grid-row: span 6;
border-radius: 5px;
background: #20252A;
display: grid;
align-items: center;
justify-items: center;
border-bottom: 5px solid #000;
cursor: pointer;
}
.tab {
grid-column: span 38;
}
.caps {
grid-column: span 42;
}
.enter {
grid-column: span 44;
}
.left-shift {
grid-column: span 50;
}
.right-shift {
grid-column: span 60;
}
.fn {
grid-column: span 22;
}
.left-command,
.right-command {
grid-column: span 28;
}
.space {
grid-column: span 120;
}
.up {
grid-row: span 3;
}
.down {
grid-row: span 3;
border-bottom: 0px;
border-top: 5px solid #000;
;
}
.left,
.right {
grid-column: span 28;
}
.func:active,
.num:active,
.letters:active {
border-bottom: 5px inset #20252A;
background: #111;
}
.down:active {
border-bottom: 0px !important;
border-top: 5px inset #20252A;
background: #111;
}
<div class="wrapper">
<!-- first row -->
<div class="func">esc</div>
<div class="func">F1</div>
<div class="func">F2</div>
<div class="func">F3</div>
<div class="func">F4</div>
<div class="func">F5</div>
<div class="func">F6</div>
<div class="func">F7</div>
<div class="func">F8</div>
<div class="func">F9</div>
<div class="func">F10</div>
<div class="func">F11</div>
<div class="func">F12</div>
<div class="func">F13</div>
<!-- second row -->
<div class="num">~<br/>`</div>
<div class="num">!<br/>1</div>
<div class="num">#<br/>2</div>
<div class="num">#<br/>3</div>
<div class="num">$<br/>4</div>
<div class="num">%<br/>5</div>
<div class="num">^<br/>6</div>
<div class="num">&<br/>7</div>
<div class="num">*<br/>8</div>
<div class="num">(<br/>9</div>
<div class="num">)<br/>0</div>
<div class="num">-<br/>_</div>
<div class="num">+<br/>=</div>
<div class="num back">delete</div>
<!-- third row -->
<div class="letters tab"><i class="material-icons"></i></div>
<div class="letters">Q</div>
<div class="letters">W</div>
<div class="letters">E</div>
<div class="letters">R</div>
<div class="letters">T</div>
<div class="letters">Y</div>
<div class="letters">U</div>
<div class="letters">I</div>
<div class="letters">O</div>
<div class="letters">P</div>
<div class="letters">{<br/>[</div>
<div class="letters">}<br/>]</div>
<div class="letters">|<br/>/</div>
<!-- fourth row -->
<div class="letters caps">caps lock</div>
<div class="letters">A</div>
<div class="letters">S</div>
<div class="letters">D</div>
<div class="letters">F</div>
<div class="letters">G</div>
<div class="letters">H</div>
<div class="letters">J</div>
<div class="letters">K</div>
<div class="letters">L</div>
<div class="letters">:<br/>;</div>
<div class="letters">"<br/>'</div>
<div class="letters enter">enter<br/>return</div>
<!-- fifth row -->
<div class="letters left-shift">shift</div>
<div class="letters">Z</div>
<div class="letters">X</div>
<div class="letters">C</div>
<div class="letters">V</div>
<div class="letters">B</div>
<div class="letters">N</div>
<div class="letters">M</div>
<div class="letters">
<<br/>,</div>
<div class="letters">><br/>.</div>
<div class="letters">?<br/>\</div>
<div class="letters right-shift">shift</div>
<!-- sixth row -->
<div class="letters fn">fn</div>
<div class="letters">control</div>
<div class="letters">option</div>
<div class="letters left-command">⌘<br/>command</div>
<div class="letters space"></div>
<div class="letters right-command">⌘<br/>command</div>
<div class="letters">option</div>
<div class="letters left">←</div>
<div class="letters up">↑</div>
<div class="letters right">→</div>
<div class="letters down">↓</div>
</div>
The problem is the grid-column-gap you have set on the grid container.
.wrapper {
display: grid;
grid-template-columns: repeat(350, 1fr);
grid-template-rows: repeat(33, 1fr);
grid-gap: 4px; <----- PROBLEM HERE
grid-row-gap: 5px;
background-color: #a5adb0;
border-radius: 5px;
color: #ddd;
padding: 15px;
font-family: 'Muli';
}
When you set grid-gap: 4px, that is shorthand for:
grid-row-gap: 4px
grid-column-gap: 4px
(Incidentally, the grid-row-gap set in the shorthand is overridden with your next line of code.)
Now look at how many columns you have set:
grid-template-columns: repeat(350, 1fr)
Now multiply 350 * 4px. The minimum possible width of a grid row is 1400px.
For an illustration, set the container's width to 1400px and remove the padding: 15px. The grid items begin to overflow at 1399px.
If you disable grid-gap: 4px you'll see that your layout scales nicely without any overflow.
Depending on how you want to space the keys, you'll have to test different units on the column gaps, such as straight percentages (%) or viewport percentages (vh, vw, vmin, vmax, etc).
Or consider using less (wider) columns.
Related
This question already has answers here:
What is the difference between align-items vs. align-content in Grid Layout?
(1 answer)
CSS Grid - Auto height rows, sizing to content
(3 answers)
Closed 1 year ago.
I have a css grid (display:grid) and rows with fixed height as well.
Can I align the rows to the top of the grid instead of distributing them vertically?
.grid {
height: 180px;
display: grid;
grid-template-columns: 1fr;
border: solid 1px red;
align-content: top;
align-items: top;
}
.row {
grid-column: 1 / -1;
height: 20px;
background: silver;
border: dashed 1px blue;
}
<div class="grid">
<div class="row">row 1</div>
<div class="row">row 2</div>
<div class="row">row 3</div>
</div>
I want to achieve this:
Just add align-content: flex-start
.grid {
height: 180px;
display: grid;
grid-template-columns: 1fr;
align-content: flex-start;
border: solid 1px red;
}
.row {
grid-column: 1 / -1;
height: 20px;
background: silver;
border: dashed 1px blue;
}
<div class="grid">
<div class="row">row 1</div>
<div class="row">row 2</div>
<div class="row">row 3</div>
</div>
You can achieve this with grid-template-rows which defines dimensions for the rows and prevents them of getting a third of the container height (180px).
Working example:
.grid {
height: 180px;
display: grid;
grid-template-rows: 20px 20px 20px;
border: solid 1px red;
}
.row {
height: 20px;
background: silver;
border: dashed 1px blue;
}
<div class="grid">
<div class="row">row 1</div>
<div class="row">row 2</div>
<div class="row">row 3</div>
</div>
You can set the grid-auto-rows to the same height as your rows to align them at the top:
grid-auto-rows
The grid-auto-rows CSS property specifies the size of an implicitly-created grid row track or pattern of tracks.
.grid {
height: 180px;
display: grid;
grid-template-columns: 1fr;
border: solid 1px red;
grid-auto-rows: 20px;
}
.row {
grid-column: 1 / -1;
height: 20px;
background: silver;
border: dashed 1px blue;
}
<div class="grid">
<div class="row">row 1</div>
<div class="row">row 2</div>
<div class="row">row 3</div>
</div>
Change the grid height to auto .grid{height:auto;}
.grid {
height:auto;
display: grid;
grid-template-columns: 1fr;
border: solid 1px red;
align-content: top;
align-items: top;
}
.row {
grid-column: 1 / -1;
height: 20px;
background: silver;
border: dashed 1px blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="grid">
<div class="row">row 1</div>
<div class="row">row 2</div>
<div class="row">row 3</div>
</div>
</body>
</html>
I have a layout that is a sidebar and a grid both wrapped in a flexbox. I'd like to put a div underneath the grid so it can have prev/next buttons, like in this image, but I can't figure out how to do that. The grid resizes itself with the window so the grid can take as many rows as necessary and then the div should go below that, and be as wide as the grid.
This is what I have, but the div is on the right of the grid:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Boardgame Database</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
aside {
background-color: red;
flex: 1;
min-width: 250px;
}
.grid-container {
flex: 4;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}
.grid-item {
border: 1px solid black;
padding: 20px;
font-size: 24px;
text-align: center;
overflow: hidden;
}
#flex-container {
display: flex;
flex-direction: row;
min-height: 100vh;
}
</style>
</head>
<body>
<div id="flex-container">
<aside class="sidebar">
</aside>
<section 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 class="grid-item">10</div>
<div class="grid-item">11</div>
<div class="grid-item">12</div>
<div class="grid-item">13</div>
<div class="grid-item">14</div>
<div class="grid-item">15</div>
<div class="grid-item">16</div>
<div class="grid-item">17</div>
<div class="grid-item">18</div>
</section>
<div id="page-buttons">
prev
next
</div>
</div>
Checkout the following Code.
#main{
display :flex;
}
#sidebar{
width:70px;
height: 300px;
border: solid black 1px;
}
#grid-area{
width:200px;
height: 300px;
border: solid black 1px;
display: block;
}
#grid{
width:200px;
height: 250px;
border: solid black 1px;
display: block;
}
<div id="main">
<div id="sidebar"></div>
<div id="grid-area">
<div id="grid"></div>
<div id="button">next / prev</div>
</div>
</div>
You should use nested flex containers. Section and bottom div should be wrapped inside another flex container with flex direction to column.
So outer flex will make sidebar & inner flex container to be side by side.
Or just use a normal div container instead of flex.
here is another example only with grid keeping the pre/next button at the bottom of the viewport:
body {
margin: 0;
}
#grid-container {
display: grid;
height: 100vh;
grid-template-columns: minmax(250px, 1fr) 4fr;
grid-template-rows: 1fr auto;
}
aside {
background-color: red;
border: 1px solid;
margin: 0.25em;
grid-row: span 2;
grid-column: 1;
}
section,
#page-buttons {
grid-column: 2;
border: solid 1px;
margin: 0.25em;
}
section {
overflow: auto;
}
#page-buttons {
display: flex;
gap: 1em;
padding: 0.5em;
background: lightgray;
justify-content: center;
}
.grid-item {
border: 1px solid black;
padding: 20px;
font-size: 24px;
text-align: center;
overflow: hidden;
}
<div id="grid-container">
<aside class="sidebar">
</aside>
<section 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 class="grid-item">10</div>
<div class="grid-item">11</div>
<div class="grid-item">12</div>
<div class="grid-item">13</div>
<div class="grid-item">14</div>
<div class="grid-item">15</div>
<div class="grid-item">16</div>
<div class="grid-item">17</div>
<div class="grid-item">18</div>
</section>
<div id="page-buttons">
prev
next
</div>
</div>
I have 7 divs. I am trying to make a three row three column layout. Two of the divs are different sizes. I have everything the way I want it, it's just on the third row one div jumps up to row two, and it wont budge down to row three even with clear:right.I am trying my hardest to have the design Internet Explorer 11 ready before I give up and cut off all traffic to IE.
The way the layout is rendering
The way it should be rendering
<style>
[div_glimg]{ width:390px; height:390px}
[glimg]{ float:left; background-size: cover; }
[div_glvideo], [glvideoobject]{ width:780px; height:390px; float:left}
</style>
<div style="background-color: white ;float:left" div_glimg>
<div glim>
<div glim1title glim1titlerez style=" margin-top:0 !important;">Life<a> in</a> </div>
<div glmaincdiv></div>
<div glimcontent>info.</div>
</div>
</div>
<div style="background-color: hsla(359,36%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(213,35%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(51,35%,62%,1.00); clear:left" glimgTall glimg> tall</div>
<div style="background-color: hsla(199,35%,62%,1.00); clear:right" div_glvideo> video </div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 1</div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 2</div>
```
Problem : First of all, the answer of the question, that why clear: right; is not working is because when the element, with clear: right; property is rendered, the element in the right of it has not been rendered. That is why, the right element after getting rendered is not affected.
Solution : Either you can choose the grid-system, or if you want to go with what you have, with fixed dimensions, you can do it like this :
<style>
[div_glimg] {
width: 78px;
height: 78px
}
[glimg] {
float: left;
background-size: cover;
}
[div_glvideo],
[glimgTall] {
float: left;
}
[div_glvideo] {
width: 156px;
height: 78px;
}
[glimgTall] {
height: 156px;
width: 78px;
}
[wrapper] {
height: 234px;
width: 234px;
}
</style>
<div wrapper>
<div style="background-color: white ;float:left" div_glimg>
<div glim>
<div glim1title glim1titlerez style=" margin-top:0 !important;">Life<a> in</a> </div>
<div glmaincdiv></div>
<div glimcontent>info.</div>
</div>
</div>
<div style="background-color: hsla(359,36%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(213,35%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(51,35%,62%,1.00);" glimgTall glimg> tall</div>
<div style="background-color: hsla(199,35%,62%,1.00);" div_glvideo> video </div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 1</div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 2</div>
</div>
I would suggest go for grid layout for better experience. Here is example.
.grid-container {
display: grid;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px;
font-size: 30px;
}
.item1 {
grid-column: 1;
grid-row: 1;
}
.item2 {
grid-column: 2;
grid-row: 1;
}
.item3{
grid-column:3;
grid-row:1;
}
.item5 {
grid-column: 2 / span 2;
grid-row: 2;
}
.item4{
grid-row: 2 / span 2;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>A Five Items Grid Layout:</h1>
<div class="grid-container">
<div class="grid-item item1">1</div>
<div class="grid-item item2">2</div>
<div class="grid-item item3">3</div>
<div class="grid-item item4">4</div>
<div class="grid-item item5">5</div>
<div class="grid-item item6">6</div>
<div class="grid-item item7">7</div>
</div>
<p>Direct child elements(s) of the grid container automatically becomes grid items.</p>
<p>Item 1, 2, and 5 are set to span multiple columns or rows.</p>
</body>
</html>
It would be easier to use CSS Grid for your solution. The layout was generated using this tool: Layoutit Grid
html,
body,
.grid-container {
height: 100%;
margin: 0;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "Div-1 Div-2 Div-3" "Div-4 Div-5 Div-5" "Div-4 Div-6 Div-7";
padding: 10px;
}
div {
border: 2px solid #000;
}
.Div-1 {
grid-area: Div-1;
background: #E975AF;
}
.Div-2 {
grid-area: Div-2;
background: #0075AE;
}
.Div-3 {
grid-area: Div-3;
background: #27408F;
}
.Div-4 {
grid-area: Div-4;
background: #FFF200;
}
.Div-5 {
grid-area: Div-5;
background: #40AD47;
}
.Div-6 {
grid-area: Div-6;
background: #EC008C;
}
.Div-7 {
grid-area: Div-7;
background: #00ADEF;
}
<div class="grid-container">
<div class="Div-1"></div>
<div class="Div-2"></div>
<div class="Div-3"></div>
<div class="Div-5"></div>
<div class="Div-6"></div>
<div class="Div-7"></div>
<div class="Div-4"></div>
</div>
I am displaying two columns in the center of my grid with contents and I need to make these two columns above each other in mobile view how I can do that?
.two-pics {
display: grid;
grid-gap: 50px;
grid-template-columns: 400px 400px;
object-fit: cover;
color: #444;
margin-top: 100px;
justify-content: center;
}
.box {
background-color: white;
color: #fff;
border-radius: 5px;
font-size: 150%;
}
.a {
grid-column: 1;
grid-row: 1 / 12;
}
.b {
grid-column: 2;
grid-row: 1 / 12;
}
<div class="two-pics">
<div class="box a">
<img src="assets/images/pic1.jpg">
<br>
<p>title</p>
<ul>
<li></li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
</ul>
</div>
<div class="box b">
<img src="assets/images/pic2.jpg">
<br>
<p>ldkjh ldkjfh ldk hjf</p>
</div>
</div>
Using the auto-fit property you can create as many 400px grid tracks as will fit in the container.
codepen
body {
background: grey;
}
.two-pics {
display: grid;
grid-gap: 50px;
grid-template-columns: repeat(auto-fit, 400px);
color: #444;
margin-top: 100px;
justify-content: center;
}
.box {
background-color: white;
color: #000;
border-radius: 5px;
font-size: 150%;
}
<div class="two-pics">
<div class="box a">
<img src="https://unsplash.it/400">
<br>
<p>title</p>
<ul>
<li>dfdfd</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
</ul>
</div>
<div class="box b">
<img src="https://unsplash.it/400">
<br>
<p>ldkjh ldkjfh ldk hjf</p>
</div>
</div>
I have a CSS grid that represents the tic-tac-toe game. I wanted to put an border only inside the grid. Today, I proceed in this way:
:root {
--border: 2px dashed #393939;
--symbol-color: #FF7F5B;
}
.grid {
height: 100%;
display: grid;
grid-template-columns: repeat(3, calc(100%/3));
grid-template-rows: repeat(3, calc(100%/3));
}
.child {
display: flex;
align-items: center;
align-content: center;
color: var(--symbol-color);
font-size: 2.5rem;
}
.child:nth-child(1),
.child:nth-child(2),
.child:nth-child(3) {
border-bottom: var(--border);
}
.child:nth-child(7),
.child:nth-child(8),
.child:nth-child(9) {
border-top: var(--border);
}
.child:nth-child(1),
.child:nth-child(4),
.child:nth-child(7) {
border-right: var(--border);
}
.child:nth-child(3),
.child:nth-child(6),
.child:nth-child(9) {
border-left: var(--border);
}
<div class="grid">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
Result:
This solution works but I find it unattractive. Do you have an idea to refactor this solution?
Since you want a stylized border (dashed, in this case), then your approach and the approach taken in the other answers appears to be useful.
However, if you decide to use a simple, solid line border, then the approach can be simplified. Just use the background color of the grid for border color, and the grid-gap property for border width.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
background-color: black;
grid-gap: 1px;
height: 100vh;
}
.child {
background-color: white;
display: flex;
align-items: center;
justify-content: center;
color: #FF7F5B;
font-size: 2.5rem;
}
body { margin: 0;}
<div class="grid">
<div class="child"></div>
<div class="child"></div>
<div class="child">X</div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child">O</div>
<div class="child"></div>
</div>
One thing you can use the nth-child selector in a better way like below instead of targeting one by one.
.child:nth-child(-n+3) {
border-bottom: var(--border);
}
.child:nth-child(3n+1) {
border-right: var(--border);
}
.child:nth-child(3n) {
border-left: var(--border);
}
.child:nth-child(n+7) {
border-top: var(--border);
}
:root {
--border: 2px dashed #393939;
--symbol-color: #FF7F5B;
}
.grid {
height: 100%;
display: grid;
grid-template-columns: repeat(3, calc(100%/3));
grid-template-rows: repeat(3, calc(100%/3));
}
.child {
display: flex;
align-items: center;
align-content: center;
color: var(--symbol-color);
font-size: 2.5rem;
}
.child:nth-child(-n+3) {
border-bottom: var(--border);
}
.child:nth-child(3n+1) {
border-right: var(--border);
}
.child:nth-child(3n) {
border-left: var(--border);
}
.child:nth-child(n+7) {
border-top: var(--border);
}
<div class="grid">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
<div class="child">7</div>
<div class="child">8</div>
<div class="child">9</div>
</div>
You may consider this workaround.
You may use grid-template-columns to do the trick.
create a parent container that will hold your four images.
set a background color (desire color of the border).
set the padding to 0
then do the trick arrange the images by grid-template-column: auto
auto;
then add gap to them grid-gap: 10px; (to show the background color of
the container as grid).
please see code below for reference
.container {
width: 200px;
display: grid;
grid-template-columns: auto auto;
grid-gap: 10px;
background-color: #000;
padding: 0;
}
.container > div {
background-color: #ccc;
padding: 20px;
text-align: center;
}
html
<div class="container">
<div>Image here</div>
<div>Image Here</div>
<div>Image here</div>
<div>Image here</div>
</div>
to help you visualize i create a sample code
http://plnkr.co/edit/gIeumXLt0k3FPVCgGlDd?p=preview
Hope it helps
Cheers!
You can reduce number of nth-child selector here from this answer.
body {
margin: 0;
}
:root {
--border: 2px dashed #393939;
--symbol-color: #FF7F5B;
}
.grid {
height: 100vh;
display: grid;
grid-template-columns: repeat(3, calc(100%/3));
grid-template-rows: repeat(3, calc(100%/3));
}
.child {
display: flex;
align-items: center;
justify-content: center;
color: var(--symbol-color);
font-size: 2.5rem;
}
.child:not(:nth-child(3n)) {
border-right: var(--border);
}
.child:not(:nth-last-child(-n + 3)) {
border-bottom: var(--border);
}
<div class="grid">
<div class="child"></div>
<div class="child"></div>
<div class="child">x</div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child">o</div>
<div class="child"></div>
</div>
Try to make use of negative margin in .child class with overflow:hidden in parent .grid class here...No need to use nth-child selector here...
:root {
--border: 2px dashed #393939;
--symbol-color: #FF7F5B;
}
.grid {
height: 100%;
display: grid;
grid-template-columns: repeat(3, calc(100%/3));
grid-template-rows: repeat(3, calc(100%/3));
overflow: hidden;
}
.child {
height: 100px;
display: flex;
align-items: center;
align-content: center;
color: var(--symbol-color);
font-size: 2.5rem;
border-bottom: var(--border);
border-left: var(--border);
margin-left: -2px;
margin-bottom: -2px;
}
<div class="grid">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
What about using background and linear-gradient:
body {
margin: 0;
}
.grid {
--b: #393939 0px, #393939 5px, transparent 5px, transparent 8px;
height: 100vh;
display: grid;
grid-template-columns: repeat(3, calc(100% / 3));
grid-auto-rows:calc(100% / 3);
background:
repeating-linear-gradient(to right ,var(--b)) 0 calc(100% / 3),
repeating-linear-gradient(to bottom,var(--b)) calc(2 * (100% / 3)) 0,
repeating-linear-gradient(to right ,var(--b)) 0 calc(2 * (100% / 3)),
repeating-linear-gradient(to bottom,var(--b)) calc(100% / 3) 0;
background-size:100% 2px,2px 100%;
background-repeat: no-repeat;
}
.child {
display: flex;
align-items: center;
justify-content: center;
}
<div class="grid">
<div class="child">A</div>
<div class="child">B</div>
<div class="child">C</div>
<div class="child">D</div>
<div class="child">E</div>
<div class="child">F</div>
<div class="child">G</div>
<div class="child">H</div>
<div class="child">I</div>
</div>