CSS Grid vertical columns with infinite rows
Without knowing the exact amount of items this is not possible with CSS grid alone.
The only way to get around this limitation is to add a class to your second half of the items.
body { display: grid; grid-template-columns: 1fr 1fr; grid-auto-flow: row dense; /* extra styles */ grid-gap: 0.5rem;}
span { grid-column-start: 1; /* extra styles */ background-color: #def; padding: 0.5rem;}
.second-half { grid-column-start: 2; /* extra styles */ background-color: #abc;}
<span>1</span><span>2</span><span>3</span><span>4</span><span class="second-half">5</span><span class="second-half">6</span><span class="second-half">7</span>
Css grid styling 2 vertical columns infinite rows from 2 different types of div
Do you need such a result?
body {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-flow: row dense;
grid-gap: 0.5rem;
}
h1 {
grid-column-start: 1;
grid-column-end: none;
}
.edition,
.translation {
display: inherit;
grid-template-rows: 1fr 1fr;
}
.edition {
grid-column-start: 2;
}
.translation {
grid-column-start: 1;
}
.edition > div[type="ed"] {
background-color: #def;
padding: 0.5rem;
}
.translation > div[type="trans"] {
background-color: #abc;
padding: 0.5rem;
}
<body>
<h1>This is title</h1>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-edition_1" class="edition">
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p><mark>śrī gaṇeśāya namaḥ</mark> // <mark>atha rājayogaprakāro likhyate</mark> //</p>
<div class="app">
<span class="lem">
śrī gaṇeśāya namaḥ (<a href="#E" title="E Printed Edition">E</a> <a href="#L" title="L LALCHAND 5876">L</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>
<a href="#U1" title="U1 SORI 1574">U1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]
</span>
śrī ṇe ya maḥ (<a href="#P" title="P Pune BORI 664">P</a>), śrī gurave namaḥ (<a href="#N1" title="N1 NGMPP 38/31">N1</a>), śrī sarasvatyai namaḥ śrī nirañjanāya namaḥ (<a href="#D1" title="D1 IGNCA 30019">D1</a>), oṃ
śrī niraṃjanāya (<a href="#U1" title="U1 SORI 1574">U1</a>)
</div>
<div class="app">
<span class="lem">atha rājayogaprakāro likhyate (<a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>) ]</span> atha rājayogaprakāra likhyate (
<a href="#U1" title="U1 SORI 1574">U1</a>), rājayogāntargataḥ / binduyogaḥ (<a href="#E" title="E Printed Edition">E</a>), atha tattvabiṃduyogaprāraṃbhaḥ (<a href="#L" title="L LALCHAND 5876">L</a>), atha rājayoga
liṣyate (<a href="#P" title="P Pune BORI 664">P</a>), atha rājayoga likhyate (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p>rājayogasyedaṃ phalaṃ / yena rājayogenānekarājyabhogasamaya eva / anekapārthivavinoda<mark>prekṣaṇasamaya</mark> eva / bahutarakālaṃ śarīrasthitirbhavati / sa <mark>eva</mark> rājayogaḥ / tasyaite bhedāḥ /</p>
<div class="app">
<span class="lem">prekṣaṇasamaya (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U1" title="U1 SORI 1574">U1</a>) ]</span>
prekṣyaṇasamaya (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
<div class="app">
<span class="lem">eva (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]</span> evaṃ (
<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-translation_1" class="translation">
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
Homage to Śrī Gaṇeśa. Now the methods of rājayoga are laid down.
</div>
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
Rājayoga is that by which longterm durability of the body arises even amongst manifold royal pleasures even amongst the manifold royal entertainments and spectacle. This truly is rājayoga. Of this [rājayoga] these are the
varieties:
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-edition_1" class="edition">
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p><mark>śrī gaṇeśāya namaḥ</mark> // <mark>atha rājayogaprakāro likhyate</mark> //</p>
<div class="app">
<span class="lem">
śrī gaṇeśāya namaḥ (<a href="#E" title="E Printed Edition">E</a> <a href="#L" title="L LALCHAND 5876">L</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>
<a href="#U1" title="U1 SORI 1574">U1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]
</span>
śrī ṇe ya maḥ (<a href="#P" title="P Pune BORI 664">P</a>), śrī gurave namaḥ (<a href="#N1" title="N1 NGMPP 38/31">N1</a>), śrī sarasvatyai namaḥ śrī nirañjanāya namaḥ (<a href="#D1" title="D1 IGNCA 30019">D1</a>), oṃ
śrī niraṃjanāya (<a href="#U1" title="U1 SORI 1574">U1</a>)
</div>
<div class="app">
<span class="lem">atha rājayogaprakāro likhyate (<a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>) ]</span> atha rājayogaprakāra likhyate (
<a href="#U1" title="U1 SORI 1574">U1</a>), rājayogāntargataḥ / binduyogaḥ (<a href="#E" title="E Printed Edition">E</a>), atha tattvabiṃduyogaprāraṃbhaḥ (<a href="#L" title="L LALCHAND 5876">L</a>), atha rājayoga
liṣyate (<a href="#P" title="P Pune BORI 664">P</a>), atha rājayoga likhyate (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p>rājayogasyedaṃ phalaṃ / yena rājayogenānekarājyabhogasamaya eva / anekapārthivavinoda<mark>prekṣaṇasamaya</mark> eva / bahutarakālaṃ śarīrasthitirbhavati / sa <mark>eva</mark> rājayogaḥ / tasyaite bhedāḥ /</p>
<div class="app">
<span class="lem">prekṣaṇasamaya (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U1" title="U1 SORI 1574">U1</a>) ]</span>
prekṣyaṇasamaya (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
<div class="app">
<span class="lem">eva (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]</span> evaṃ (
<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-translation_1" class="translation">
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
Homage to Śrī Gaṇeśa. Now the methods of rājayoga are laid down.
</div>
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
Rājayoga is that by which longterm durability of the body arises even amongst manifold royal pleasures even amongst the manifold royal entertainments and spectacle. This truly is rājayoga. Of this [rājayoga] these are the
varieties:
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-edition_1" class="edition">
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p><mark>śrī gaṇeśāya namaḥ</mark> // <mark>atha rājayogaprakāro likhyate</mark> //</p>
<div class="app">
<span class="lem">
śrī gaṇeśāya namaḥ (<a href="#E" title="E Printed Edition">E</a> <a href="#L" title="L LALCHAND 5876">L</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>
<a href="#U1" title="U1 SORI 1574">U1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]
</span>
śrī ṇe ya maḥ (<a href="#P" title="P Pune BORI 664">P</a>), śrī gurave namaḥ (<a href="#N1" title="N1 NGMPP 38/31">N1</a>), śrī sarasvatyai namaḥ śrī nirañjanāya namaḥ (<a href="#D1" title="D1 IGNCA 30019">D1</a>), oṃ
śrī niraṃjanāya (<a href="#U1" title="U1 SORI 1574">U1</a>)
</div>
<div class="app">
<span class="lem">atha rājayogaprakāro likhyate (<a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>) ]</span> atha rājayogaprakāra likhyate (
<a href="#U1" title="U1 SORI 1574">U1</a>), rājayogāntargataḥ / binduyogaḥ (<a href="#E" title="E Printed Edition">E</a>), atha tattvabiṃduyogaprāraṃbhaḥ (<a href="#L" title="L LALCHAND 5876">L</a>), atha rājayoga
liṣyate (<a href="#P" title="P Pune BORI 664">P</a>), atha rājayoga likhyate (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p>rājayogasyedaṃ phalaṃ / yena rājayogenānekarājyabhogasamaya eva / anekapārthivavinoda<mark>prekṣaṇasamaya</mark> eva / bahutarakālaṃ śarīrasthitirbhavati / sa <mark>eva</mark> rājayogaḥ / tasyaite bhedāḥ /</p>
<div class="app">
<span class="lem">prekṣaṇasamaya (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U1" title="U1 SORI 1574">U1</a>) ]</span>
prekṣyaṇasamaya (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
<div class="app">
<span class="lem">eva (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]</span> evaṃ (
<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-translation_1" class="translation">
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
Homage to Śrī Gaṇeśa. Now the methods of rājayoga are laid down.
</div>
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
Rājayoga is that by which longterm durability of the body arises even amongst manifold royal pleasures even amongst the manifold royal entertainments and spectacle. This truly is rājayoga. Of this [rājayoga] these are the
varieties:
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-edition_1" class="edition">
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p><mark>śrī gaṇeśāya namaḥ</mark> // <mark>atha rājayogaprakāro likhyate</mark> //</p>
<div class="app">
<span class="lem">
śrī gaṇeśāya namaḥ (<a href="#E" title="E Printed Edition">E</a> <a href="#L" title="L LALCHAND 5876">L</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>
<a href="#U1" title="U1 SORI 1574">U1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]
</span>
śrī ṇe ya maḥ (<a href="#P" title="P Pune BORI 664">P</a>), śrī gurave namaḥ (<a href="#N1" title="N1 NGMPP 38/31">N1</a>), śrī sarasvatyai namaḥ śrī nirañjanāya namaḥ (<a href="#D1" title="D1 IGNCA 30019">D1</a>), oṃ
śrī niraṃjanāya (<a href="#U1" title="U1 SORI 1574">U1</a>)
</div>
<div class="app">
<span class="lem">atha rājayogaprakāro likhyate (<a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a>) ]</span> atha rājayogaprakāra likhyate (
<a href="#U1" title="U1 SORI 1574">U1</a>), rājayogāntargataḥ / binduyogaḥ (<a href="#E" title="E Printed Edition">E</a>), atha tattvabiṃduyogaprāraṃbhaḥ (<a href="#L" title="L LALCHAND 5876">L</a>), atha rājayoga
liṣyate (<a href="#P" title="P Pune BORI 664">P</a>), atha rājayoga likhyate (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
<div type="ed">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
<div xmlns="http://www.w3.org/1999/xhtml" class="wapp">
<p>rājayogasyedaṃ phalaṃ / yena rājayogenānekarājyabhogasamaya eva / anekapārthivavinoda<mark>prekṣaṇasamaya</mark> eva / bahutarakālaṃ śarīrasthitirbhavati / sa <mark>eva</mark> rājayogaḥ / tasyaite bhedāḥ /</p>
<div class="app">
<span class="lem">prekṣaṇasamaya (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U1" title="U1 SORI 1574">U1</a>) ]</span>
prekṣyaṇasamaya (<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
<div class="app">
<span class="lem">eva (<a href="#P" title="P Pune BORI 664">P</a> <a href="#N1" title="N1 NGMPP 38/31">N1</a> <a href="#D1" title="D1 IGNCA 30019">D1</a> <a href="#U2" title="U2 SORI 6082">U2</a>) ]</span> evaṃ (
<a href="#U2" title="U2 SORI 6082">U2</a>)
</div>
</div>
</div>
</div>
<div xmlns="http://www.tei-c.org/ns/1.0" xml:id="div-translation_1" class="translation">
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">intro1</h3>
Homage to Śrī Gaṇeśa. Now the methods of rājayoga are laid down.
</div>
<div type="trans">
<h3 xmlns="http://www.w3.org/1999/xhtml">rajayoga</h3>
Rājayoga is that by which longterm durability of the body arises even amongst manifold royal pleasures even amongst the manifold royal entertainments and spectacle. This truly is rājayoga. Of this [rājayoga] these are the
varieties:
</div>
</div>
</body>
Auto Grid columns, without wrapping to next row
You simply make the flow to be column like below:
.boxes {
display: grid;
grid-auto-columns: 381px;
grid-auto-flow: column; /* this will do the trick */
grid-gap: 10px;
}
.boxes>* {
height: 50px;
background: red;
}
<div class="boxes">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
CSS Grid set number of columns for the top row only
Use grid-column on your child elements
On your grid child elements (your navbar, for example), you can use the grid-column
property. You can set start and end values for where you want the child to be positioned.
For example:
.navbar {
grid-column: 1 / 7;
}
Next level: use grid-areas to label your child elements
You might also want to consider looking into grid-areas
, which is a nice way of labelling and referencing areas such as your header and navbar.
References:
- MDN: grid-column
- MDN: grid areas
How to align grid items vertically using display:grid and fill all vertical empty space?
Using CSS Grid
Following demo shows how you can achieve the desired layout using just the grid layout.
Its a 6 x 3
grid where items on the left span 3 rows each and the items on the right span 2 rows each. The item in the center spans all 6 rows.
Each grid items is adjusted in its place using the grid-row
and grid-column
properties.
The trick to achieving this layout is having more rows than columns.
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-template-rows: repeat(6, 1fr);
grid-gap: 30px;
height: 100vh;
}
.grid-item {
background: #eee;
width: 100%;
}
.center {
grid-row: 1 / span 6;
grid-column: 2 / span 1;
}
.left1 {
grid-row: 1 / span 3;
grid-column: 1 / span 1;
}
.left2 {
grid-row: 4 / span 3;
grid-column: 1 / span 1;
}
.right1,
.right2,
.right3 {
grid-column: 3 / span 1;
}
.right1 { grid-row: 1 / span 2; }
.right2 { grid-row: 3 / span 2; }
.right3 { grid-row: 5 / span 2; }
<div class="container">
<div class="grid-item left1">left1</div>
<div class="grid-item left2">left2</div>
<div class="grid-item center">center</div>
<div class="grid-item right1">right1</div>
<div class="grid-item right2">right2</div>
<div class="grid-item right3">right3</div>
</div>
How to repeat a CSS grid layout pattern?
You may consider many grid container that you repeat like below:
.container { max-width: 940px; margin: 0 auto; counter-reset: div;}
.grid-container { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 100px; grid-gap: 20px; margin-bottom:20px;}.grid-container:nth-child(2n+1) { grid-template-areas: "v1 v2 h1 h1" "v1 v2 h2 h2"}.grid-container:nth-child(2n) { grid-template-areas: "v1 v1 h1 h2 " "v2 v2 h1 h2 "}
.item-a { grid-area: v1;}
.item-b { grid-area: v2;}
.item-c { grid-area: h1;}
.item-d { grid-area: h2;}
.grid-container>div { box-shadow: 0 0 11px 0 #b9b9b9;}
.grid-container>div:before { counter-increment: div; content: counter(div); font-size: 40px;}
<div class="container"> <div class="grid-container"> <div class="item-a"></div> <div class="item-b"></div> <div class="item-c"></div> <div class="item-d"></div> </div> <div class="grid-container"> <div class="item-a"></div> <div class="item-b"></div> <div class="item-c"></div> <div class="item-d"></div> </div> <div class="grid-container"> <div class="item-a"></div> <div class="item-b"></div> <div class="item-c"></div> <div class="item-d"></div> </div> <div class="grid-container"> <div class="item-a"></div> <div class="item-b"></div> <div class="item-c"></div> <div class="item-d"></div> </div> <div class="grid-container"> <div class="item-a"></div> <div class="item-b"></div> <div class="item-c"></div> <div class="item-d"></div> </div>
</div>
Related Topics
CSS - Apply Opacity to Element But Not to Text Within the Element
Why 'Float:Left' Doesn't Work with a Fixed Width
Inline Svg Breaks in Safari and Mobile Safari
Background Color on Input Type=Button :Hover State Sticks in Ie
Export HTML Table to Excel and Keep CSS Styles
Internet Explorer Creates Horizontal Scrollbar for Vertical Scrollbar's Width
Jquery Mobile Android - Fixed Full-Screen Background Image
Best Practice for CSS Clear or Overflow
How to Make Diagonal Circle Border Gradient
How to Create a Gradient Border on a Circle with CSS3
Why Is My Bootstrap Code Not Working
How to Put Custom Content Like a Linebreak Inside P:Column Header
Precedence in CSS Selector Specifity Conflicts (Type VS Class Selector)