Why is my grid item not spanning multiple rows? - html

I have a html template like the following:
<div class="my-grid-container">
<div class="summary-card" ng-repeat="cardData in summaryCardsCtrl.summaryDetails" >
<div ng-include="'.....'"></div>
</div>
</div>
The included html looks like:
<div class="card">
<div class="card-title" id="{{cardData.label}}">{{cardData.label}}</div>
<div class="card-data">
<div class="card-ico">
.....
</div>
<div class="card-value">
<span title="{{cardData.rawValue}}">{{cardData.value}}</span>
<span>%</span>
</div>
</div>
</div>
I want the first card to span for two rows, like:
I am using CSS3 GridBox like the following:
.my-grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
padding: 10px;
}
.my-grid-container > div {
text-align: center;
padding: 20px 0;
font-size: 30px;
max-height: 70px;
}
div.my-grid-container > div.card:first-child {
grid-row: 1 / 2;
}
But it did not work till now. First div did not span two rows.
What am I doing wrong?

Your code:
div.my-grid-container > div.card:first-child {
grid-row: 1 / 2;
}
You're telling the grid item to span from grid row line 1 to grid row line 2. That spans one row.
If you want to span two rows, then use this instead:
div.my-grid-container > div.card:first-child {
grid-row: 1 / 3;
}
or this:
div.my-grid-container > div.card:first-child {
grid-row: 1 / span 2;
}
Keep in mind that in every grid the number of row lines is equal to the number of rows + 1, because the last row has an extra (final) line. The same concept applies to columns.
Firefox offers a useful tool for seeing this.
In Firefox dev tools, when you inspect the grid container, there is a tiny grid icon in the CSS declaration. On click it displays an outline of your grid.
More details here: https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector/How_to/Examine_grid_layouts

Related

Conditionally place item within grid layout

I have a few scenarios in my grid. I have 4 possible grid elements, but depending on a user selection from the previous screen, I might only display three of those.
I'm running into an issue where in a particular circumstance, I want to display only 3 elements (2 per row), with the third element on the bottom row placed to the far right, essentially leaving an empty space where the first element of the second row normally would be.
const MyGrid = styled(OtherGrid)`
#media (max-width: ${BP.SMALL - 1}px) {
div:nth-child(2) {
grid-row-start: 3;
}
div:nth-child(3) {
grid-row-start: 2;
}
}
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 1fr;
`;
My grid is styled from another grid, but all in all, acts like a basic grid.
<MyGrid>
<FormGroup
type={"number"}
name={"initial_moisture"}
label={"Initial Moisture (%)"}
step={0.1}
required
/>
<FormGroup
type={"number"}
name={"initial_temp"}
label={`Initial Temperature (${isMetric ? "C" : "F"})`}
step={1}
required
/>
{operatingMode != "auto_cool" && (
<>
<FormGroup
type={"number"}
name={"final_moisture"}
label={"Target Moisture (%)"}
step={0.1}
required
/>
</>
)}
{operatingMode != "auto_hydrate" && (
<>
<FormGroup
class={"auto-cool"}
type={"number"}
name={"final_temp"}
label={`Target Temperature (${isMetric ? "C" : "F"})`}
step={1}
required
/>
</>
)}
</MyGrid>
In the last possible state here (operatingMode != "auto-hydrate"), I want to place that element on the second row, but in the second column as well, and leave an empty space on the 2nd row, first column. Is there something within my parent container GoalsFieldGrid that I can define that will help me do this?
grid-column: -1; is what I'm looking for, but I'm not sure how to apply it in that specific scenario.
If I understand correctly you only require this when there are three elements only.
That being the case you can use :nth-child(3):last-child to select the element.
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 1fr;
margin-bottom: 1em;
gap: .25em;
}
.item {
display: flex;
justify-content: center;
align-items: center;
padding: 1em;
border: 1px solid grey;
}
.item:nth-child(3):last-child {
grid-column: 2;
}
<div class="grid">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>

Grid column line using Display property GRID

Here,in this code I want to understand what does the grid-column-start and grid-column-end specify ?
This is the HTML part of my code.
<div class="grid-container">
<div class="grid-item1">1</div>
<div class="grid-item2">2</div>
<div class="grid-item3">3</div>
<div class="grid-item3">4</div>
<div class="grid-item5">5</div>
<div class="grid-item6">6</div>
<div class="grid-item7">7</div>
<div class="grid-item8">8</div>
<div class="grid-item9">9</div>
This is the CSS part of my code.
.grid-container
{
display: grid;
grid-template-columns: 100px 100px 100px;
grid-gap : 50px;
background-color: black;
padding: 10px;
}
div
{
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 15px;
font-size: 30px;
text-align: center;
}
.grid-item1
{
grid-column-start : 1 ;
grid-column-end : 3;
}
This is the output of the code.
So, what does grid-column-end specify here ?
The grid-column-start property defines on which column-line the item will start.
and
The grid-column-end property defines how many columns an item will span, or on which column-line the item will end.
Please have a look of this below two examples: 1-grid-column-start 2-grid-column-end
So what it basically does is, When you give grid-column-start : 1| grid-column-end : 3. The column will start from the position one and span till the column 3.
The short-hand method of this will be grid-column: 1 / 3; Which starts from 1 and end at the column 3 which will make other columns to move.
See examples here : https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column
Hope this helps.
To understand what grid-template-column property does, check Chrome DevTools and inspect "grid-item1". Shorter method is: grid-column: 1/3; - div spans across two columns; starting at first line and finishing at third --> | col | col |
Instead of using pixels in grid-template-columns property, I suggest using this to avoid non-responsiveness:
grid-template-columns: repeat(12/1fr);
or
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr);

Offset CSS grid items on explicitly placed rows

I'm trying to achieve the following with CSS Grid Layout:
A A
B B B B B B B B
C C E E E E E
D D D
F F F
I can add classes in the HTML but I cannot re-order the elements or add wrapping elements.
I have almost achieved this by using explicit grid-row values. Is it possible to position the last two rows starting at the fourth column without explicitly specifying the grid-column property individually for each element? (For brevity I've kept it to 6 here but there's more in the actual layout)
Here's what I have so far. As you can see, the two rows for E and F start on column 1.
I can add grid-column: 4 to .c13 but the subsequent elements in the row flow back to column 1.
.grid {
display: grid;
grid-template-rows: repeat(5, 2em);
grid-gap: 0.5em;
text-align: center;
}
.c2 {
grid-column: 8;
}
.c16 {
grid-column: 4;
}
.extra1 {
grid-row: 4;
}
.extra2 {
grid-row: 5;
}
.global1 { background-color: #ccc; }
.intra1 { background-color: lightblue; }
.intra2 { background-color: yellow; }
.intra3 { background-color: purple; color: white; }
.extra1 { background-color: orange; }
.extra2 { background-color: #f66; }
<p>You need a browser that supports CSS Grid Layout for this.</p>
<section class='grid'>
<div class='c1 global1'>A</div>
<div class='c2 global1'>A</div>
<div class='c3 intra1'>B</div>
<div class='c4 intra1'>B</div>
<div class='c5 intra1'>B</div>
<div class='c6 intra1'>B</div>
<div class='c7 intra1'>B</div>
<div class='c8 intra1'>B</div>
<div class='c9 intra1'>B</div>
<div class='c10 intra1'>B</div>
<div class='c11 intra2'>C</div>
<div class='c12 intra2'>C</div>
<div class='c13 extra1'>D</div>
<div class='c14 extra1'>D</div>
<div class='c15 extra1'>D</div>
<div class='c16 intra3'>E</div>
<div class='c17 intra3'>E</div>
<div class='c18 intra3'>E</div>
<div class='c19 intra3'>E</div>
<div class='c20 intra3'>E</div>
<div class='c21 extra2'>F</div>
<div class='c22 extra2'>F</div>
<div class='c23 extra2'>F</div>
</section>
You can add pseudo-elements to the .grid to block out the bits you don't want to use. CSS Grid will treat the pseudo-element as a grid-item and use up the space specified.
In your case these rules should work:
.grid:before{
content:'';
grid-column: 1 / 4;
grid-row: 4 / -1;
}
.grid:after{
content:'';
grid-column: 7 / 10;
grid-row: 4 / -1;
}
Here's an example of it for you to play with: https://codepen.io/chrisboon27/pen/Lddpqa
I added in the text and background-color to make it easier to see what is going on, but you would just remove those.

CSS grid grid-column span issue

I am working on the span size of a grid-column.
I have this code for my grid-columns:
.main_comp:nth-child(n+3) {
//background-color: yellow;
grid-template-columns: repeat(6, 1fr);
}
.main_comp:nth-child(n+3) .bigimg {
grid-row: 1;
grid-column: auto / span 3;
}
.main_comp:nth-childn+(n+3) .blog_art {
grid-row: 2;
grid-column: auto / span 2;
background-color: green;
}
The result I am getting is not quite what I want. I thought that:
grid-column: auto / span 2;
would span each blogart div 2 columns and three of blogart divs would span over all the 6 columns.
What I would like is something like this for all the divs from number three and onwards:
I have setup a codepen on my example and the issue I have mentioned here starts on line 66.
you need to span 2 cols. (edit similar answer as Naga Sai A, but with a different approach/selector)
you may overide your previous rule .blog_art:not(:nth-child(2)) with:
.bigimg + .bigimg ~ .blog_art {
grid-column: auto / span 2;
background: tomato;/* see us */
}
http://codepen.io/gc-nomade/pen/KmzXgx?editors=1100#0
To achieve expected result, use below option
Add below class to the blog_art
<div class="main_comp">
<div class="bigimg">image</div>
<div class="bigimg">image</div>
<div class="blog_art new">blog art</div>
<div class="blog_art new">blog art</div>
<div class="blog_art new">blog art</div>
</div>
.new:not(:nth-child(2)) {
grid-column: auto / span 2;
grid-row: 2;
background: orange;
}
Codepen URL: http://codepen.io/nagasai/pen/NjNaPX?editors=1100

SCSS nth-child grid layout

I'm having trouble with this.
I have a bootstrap 3 site with html like this:
<div class="item-container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="item">
</div>
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="item">
</div>
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="item">
</div>
// and so on...
</div>
</div>
I'd like to make it so that the last "row" (not bootstrap row) of col's get margin-bottom: 0
When device is xs screen size:
.item-container {
.row {
.col-xs-12.col-sm-6.col-md-4 {
margin-bottom: 20px;
#media screen and (max-width: $xs-max) {
&:last-child {
margin-bottom: 0;
}
}
}
}
}
When device is sm screen size:
.item-container {
.row {
.col-xs-12.col-sm-6.col-md-4 {
margin-bottom: 20px;
#media screen and (max-width: $xs-max) {
&:last-child,
&:nth-last-child(2):nth-child(odd) {
margin-bottom: 0;
}
}
}
}
}
That worked fine selecting the last "row".
However, I'm having trouble with the three column rows. Trying to select the last row.
I've made a jsfiddle: https://jsfiddle.net/rx03nan6/
I'm trying to select the red boxes.
<hr /> is just a break line to show you different types of layout it can have depending on the number of boxes.
<br /> please ignore the br tag, I just wanted the show you how it looks like, so please don't look into the HTML in the jsfiddle.
So you need to style only the items on the last row, where each row contains 3 items.
You can't simply select the last 3 items with :nth-last-child(-n + 3) because let's say you have 4 items - that would select items 2,3,4 - when we only want to select item 4.
So we need additional logic here:
1) We always select the last item
2) We only want to select the second last item if it is also item (3n + 2) - 2,5,8,etc.
3) We only want to select the third last item if it is also item (3n + 1) - 1,4,7,etc.
So the selector will look something like this:
li:nth-child(3n + 1):nth-last-child(-n + 3),
li:nth-child(3n + 2):nth-last-child(-n + 2),
li:last-child {
border-color: blue;
}
Here's a Codepen demo
Here's an updated fiddle
Try this solution for number of column like one , two and three column. There is no need to add nth child. this solution depend on parent class like .raw class
.item-container {
.row {
.col-xs-12.col-sm-6.col-md-4 {
margin-bottom: 20px;
}
#media screen and (max-width: $xs-max) {
& > div:last-child {
margin-bottom: 0 !important;
}
}
}
}