how to give grid 2 columns instead of 1 in css - css

Im using css grids for my project.
I have this css for the page
.App {
text-align: center;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(3, minmax(0, 1fr));
height: 100vh;
}
.grid-item {
background-color: rgba(0, 0, 0, 0.8);
border: 0.5px dotted rgba(255, 255, 255, 0.8);
padding: 20px;
color: white;
text-align: center;
}
and here is my layout
I want the grids where the calendar is shown to be just one grid with 2 rows span. Sorry, i know this is a stupid question but I'm not very good at styling

You could do it by defining grid-template-areas on the parent and placing the specific div in the wanted area using grid-area.
.app {
text-align: center;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(3, minmax(0, 1fr));
height: 100vh;
/* Naming the grid areas */
grid-template-areas:
"weather clock spotify"
"calendar mic number"
"calendar message feed";
}
.grid-item {
background-color: rgba(0, 0, 0, 0.8);
border: 0.5px dotted rgba(255, 255, 255, 0.8);
padding: 20px;
color: white;
text-align: center;
}
/* Instead if using nth-of-type you should add a custom class to the specific html element if it's possible. */
.app > div:nth-of-type(4) {
background-color: red;
/* Place the element in the desired area. */
grid-area: calendar;
}
You didn't include your HTML structure. But I imagine it'll look something like this.
<div class="app">
<div class="grid-item">
city weather
</div>
<div class="grid-item">
Clock
</div>
<div class="grid-item">
Spotify
</div>
<div class="grid-item">
Calendar
</div>
<div class="grid-item">
Mic
</div>
<div class="grid-item">
Some number
</div>
<div class="grid-item">
Message
</div>
<div class="grid-item">
Text feed
</div>
</div>
CSS Tricks has a quick overview on using css grid with template areas. https://css-tricks.com/snippets/css/complete-guide-grid/#grid-template-areas

Related

grid-template-rows and grid-template-columns not working

My Code:
<div class="container">
<div class="item item-1">1</div>
<div class="item item-2">2</div>
<div class="item item-3">3</div>
<div class="item item-4">4</div>
</div>
/*CSS*/
.container {
display: grid;
border: 1px solid;
grid-template-rows: repeat (4, 100px);
grid-template-columns: repeat (auto-fit, minmax(100px, 1fr));
}
.item {
background-color: red;
color: #6a3030;
font-size: 50px;
border: 1px solid;
}
What I expected was that the grids would be 100px each in a large viewport, and automatically fitting without overflowing.
But instead, the layout ended up like this:
enter image description here
I tried to set the grid-template values to
grid-template-rows: repeat (4, 100px);
grid-template-columns: repeat (4, 100px);
But nothing changes. The grid-template values are even crossed out in the Devtool.
enter image description here
How can I fix it back to the expected layout?

How to make 2 css grid columns into 1 for mobile without media query?

As title says + I need to keep itemX and itemY in one cell on each device. Is media query the only solution? If there is more of a native css grid way I would love to learn it.
See fiddle:
https://jsfiddle.net/forusak/ctg3auh0/
.container {
display: grid;
grid-template: repeat(10, auto) / repeat(auto-fit, minmax(250px, 1fr));
column-gap: 30px;
color: white;
}
.container>* {
background-color: #b90011;
border: 1px solid black;
padding: 5%;
height: 20px;
}
.item1 {
grid-row: 1 / 10;
height: auto;
}
/* comment out part bellow to see mobile responsivity which is missing here */
.itemX,
.itemY {
grid-area: 3 / 2 / 3 / 2;
width: 40%;
}
.itemY {
margin-left: auto;
}
<div class="container">
<div class="item1"> </div>
<div class="item"> </div>
<div class="item"> </div>
<div class="itemX"> itemX </div>
<div class="itemY"> itemY </div>
<div class="item"> </div>
<div class="item"> </div>
</div>
Checkout the below code. At screen-width < 464px itemX and itemY will reassemble vertically.
body {
padding: 1rem;
}
.res-grid-1 {
--min-size: 25rem;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(var(--min-size), 1fr));
grid-gap: 1rem;
}
.res-grid-1 > div {
padding: 5rem 1rem;
text-align: center;
font-size: 2rem;
background: #557571;
color: #ffffff;
}
.res-grid-2 {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(11.5rem, 1fr));
grid-gap: 1rem;
}
<div class="res-grid-1">
<div>Item 1</div>
<div class="res-grid-2">
<div>Item X</div>
<div>Item Y</div>
</div>
</div>
However there is a small bug, between screen-width 1280px and 1328px itemX and itemY are reassembling horizontally(which should be vertically). This is due to nesting of grid;it is possible to achieve responsive CSS grid without media-queries but here you're trying achieve the same for a nested grid without media-queries.
If you wish to use media-queries, you can fix this bug by making following changes to CSS:
In class res-grid-2 replace line:
grid-template-columns: repeat(auto-fill, minmax(11.5rem, 1fr));
with:
grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
and add:
#media only screen and (max-width: 576px) {
.res-grid-2 {
grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
}
}

Grid auto flow column in slider

Trying to implement slider with grid items, using grid layout and grid-auto-flow: column;
Issue that I am having is that grid with column option, does not see my container with, as I would predict it should see, thus last item is show partially.
My goal is to allways show full items in grid container, and overflow: hide other items.
Is it possible using grid?
https://codepen.io/evelina-rim/pen/gOaLQEq
.container {
border: 10px solid red;
background-color: grey;
width: 700px;
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-auto-flow: column;
grid-auto-columns: minmax(300px, 1fr);
grid-column-end: 1
}
.item {
background-color: coral;
border: 3px solid blue;
}
<div class="container">
<div class="item">Vienas</div>
<div class="item">Du</div>
<div class="item">Trys</div>
<div class="item">Keturi</div>
<div class="item">Penki</div>
</div>
Use percentage value to control this and you can decide how many item you want to show and this will define the width of your items:
.container {
border: 10px solid red;
background-color: grey;
width: 700px;
display: grid;
grid-gap: 20px;
grid-auto-flow: column;
grid-auto-columns: calc((100% - 2*20px)/3); /* don't forget to consider the gap */
}
.item {
background-color: coral;
border: 3px solid blue;
}
<div class="container">
<div class="item">Vienas</div>
<div class="item">Du</div>
<div class="item">Trys</div>
<div class="item">Keturi</div>
<div class="item">Penki</div>
</div>
It can be easier using CSS variables
.container {
--n:2;
border: 10px solid red;
background-color: grey;
width: 700px;
display: grid;
grid-gap: 20px;
grid-auto-flow: column;
grid-auto-columns: calc((100% - (var(--n) - 1)*20px)/var(--n)); /* don't forget to consider the gap */
}
.item {
background-color: coral;
border: 3px solid blue;
}
<div class="container">
<div class="item">Vienas</div>
<div class="item">Du</div>
<div class="item">Trys</div>
<div class="item">Keturi</div>
<div class="item">Penki</div>
</div>
<div class="container" style="--n:4">
<div class="item">Vienas</div>
<div class="item">Du</div>
<div class="item">Trys</div>
<div class="item">Keturi</div>
<div class="item">Penki</div>
</div>

Two column layout in column direction with css grid

I'm trying to create a two-column layout with CSS grid with items flow in a column direction and I managed to create the layout but the problem is when the child items are dynamic it breaks. here's the snippet that I've tried. so basically grid-template-rows: repeat(4, auto); <4> should be dynamic it should be half of the total number of items. Is there any way that I can achieve it through CSS. Click here for fiddle
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
</div>
.grid-container {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: repeat(4, auto);
grid-auto-flow: column;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
examples provided in earlier comments with column CSS did not seem to suits your needs : https://jsfiddle.net/hLnvk2b8/ , https://jsfiddle.net/hLnvk2b8/1 .
If you want to stick to the grid flowing into column with an unknown amount of rows Javascript could help you update the row's amount needed.
SASS is unable to access the document, it only compiles CSS selectors and CSS rules ,that is why you would need a script (it can be on server side too) to inspect the DOM:
example via javascript on browser's side :
window.onload = resetgrid('2');
function resetgrid(varcol) {
var colnum = varcol;/* how many columns do you want ? */
var child = document.getElementById("gridrow").childElementCount;/* how many children */
var els = child / colnum;/* how many rows could that make */
var rows = Math.round(els);/* row's number cannot be decimal */
var resetrow = document.createElement("STYLE");/* prepare to insert a style sheet */
var rowstyle = document.createTextNode(".grid-container { grid-template-columns:repeat(" + colnum + ",1fr);grid-template-rows: repeat(" + rows + ",auto);}");/* finalize the rules */
resetrow.appendChild(rowstyle);/* insert the rules inside style tag*/
document.head.appendChild(resetrow);/* insert the style tag inside head */
}
.grid-container {
display: grid;
grid-template-columns: 50% 50%;
grid-auto-flow: column;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container>div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
/*demo purpose*/
[data-table] {
display: table;
text-align: center;
margin: auto;
}
<!--demo purpose-->
<div data-table>
<p>reset numbers of columns </p>
<button onclick="resetgrid(1)">1</button>
<button onclick="resetgrid(2)">2</button>
<button onclick="resetgrid(3)">3</button>
</div>
<!--end demo-->
<div id="gridrow" class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
<div class="item7">7</div>
<div class="item8">8</div>
</div>
.two-columns{
width: 300px;
height: 400px;
border: 1px solid;
display: grid;
grid-template-columns: repeat(2, 1fr);
overflow: auto;
}
Have you tried using grid-auto-flow:row; it should ideally work. Here is a fiddle for the same. I have used your fiddle for the same and made changes to only one property.jsFiddle

Setting two collapsing columns in a row equal in width, with a center column that shrinks and grows to fit content

I'm trying to achieve a certain column layout in CSS. I've explored using Flexbox as well as CSS Grid, but all of my peers I've talked to cannot figure out a way to make this work. I know I can achieve it using JavaScript, but I'd like to avoid that if at all possible.
I want to create a layout of three columns. Column 1 and Column 3 should be matching in width (defined by the content of the longer column), and column 2 should shrink and grow to allow content to fit (but not expand to fill the parent container).
It's a bit difficult to paint a picture of what I'm going for, so please take a look at this CodePen where I've broken down the rudimentary code and showed a mocked up example of what I'm going for.
Here's the HTML structure
<div class="container">
<div class="col col--Z">Let's match cols, but also collapse!</div>
<div class="col col--Y">No! 😣 Let me shrink!</div>
<div class="col col--Z">Yes!</div>
</div>
And here's the SCSS structure
.container {
display: grid;
justify-content: center;
grid-template-columns: 1fr auto 1fr;
.col {
padding-left: 6px;
padding-right: 6px;
&.col--Z {
background: rgba(0,255,55,0.2);
}
&.col--Y {
background: rgba(0,0,255,0.2);
}
}
}
* {
font-family: 'Arial', Sans-Serif;
}
.container {
display: grid;
justify-content: center;
grid-template-columns: 1fr auto 1fr;
}
.container .col {
padding-left: 6px;
padding-right: 6px;
}
.container .col.col--Z {
background: rgba(0, 255, 55, 0.2);
}
.container .col.col--Y {
background: rgba(0, 0, 255, 0.2);
}
.container-faked {
display: grid;
justify-content: center;
grid-template-columns: 260px auto 260px;
}
.container-faked .col {
padding-left: 6px;
padding-right: 6px;
}
.container-faked .col.col--Z {
background: rgba(0, 255, 55, 0.2);
}
.container-faked .col.col--Y {
background: rgba(0, 0, 255, 0.2);
}
<!-- This is the actual code -->
<h1>CSS Grid Attempt (actual code)</h1>
<div class="container">
<div class="col col--Z">Let's match cols, but also collapse!</div>
<div class="col col--Y">No! 😣 Let me shrink!</div>
<div class="col col--Z">Yes!</div>
</div>
<br>
<hr>
<br>
<!-- This is the objective, mocked up! -->
<h1>CSS Grid Attempt (faked example)</h1>
<div class="container-faked">
<div class="col col--Z">Let's match cols, but also collapse!</div>
<div class="col col--Y">No! 😣</div>
<div class="col col--Z">Yes!</div>
</div>
And the CodePen containing both the rudimentary code (not working) as well as the mocked up example of what I'd like to achieve, but using fixed pixel values to simulate equal width columns.
https://codepen.io/seanmaisch/pen/MZdqoW
You can use display: inline-grid to make grid container width according to content. To center it you can use some .wrapper block.
* {
font-family: 'Arial', Sans-Serif;
}
.wrapper {
display: flex;
justify-content: center;
}
.container {
display: inline-grid;
grid-template-columns: 1fr auto 1fr;
}
.container .col {
padding-left: 6px;
padding-right: 6px;
}
.container .col.col--Z {
background: rgba(0, 255, 55, 0.2);
}
.container .col.col--Y {
background: rgba(0, 0, 255, 0.2);
}
<div class="wrapper">
<div class="container">
<div class="col col--Z">Let's match cols, but also collapse!</div>
<div class="col col--Y">No! 😣 Let me shrink!</div>
<div class="col col--Z">Yes!</div>
</div>
</div>

Resources