Understanding grid negative values
when using the same value inside grid-column
/grid-row
you will fall into this rule:
If the placement for a grid item contains two lines, and the start line is further end-ward than the end line, swap the two lines. If the start line is equal to the end line, remove the end line.ref
So saying grid-column:-1/-1
means grid-column:-1
which is grid-column:-1/auto
auto
The property contributes nothing to the grid item’s placement, indicating auto-placement or a default span of one. (See § 8 Placing Grid Items, above.)
So basiclly you said to your element to start at the last line and span one column which will create an implicit new column:
A basic example to illustrate:
.box {
display:grid;
grid-template-columns:20px 20px 20px;
grid-auto-columns:100px;
grid-gap:5px;
}
span {
grid-column:-1/-1;
height:40px;
background:red;
}
<div class="box">
<span></span>
</div>
What does negative values for Grid Layout Indicate
Negative values for gridx
and gridy
indicate relative positioning, i.e. the component is placed just to the right of the component added before (for gridx
) or just below the component (for gridy
).
You should not use negative values directly but GridBagConstraints.RELATIVE
instead.
Using negative integers with implicit rows in CSS Grid
You can only use negative integers in an explicit grid.
See the Grid spec here:
7.1. The Explicit
GridNumeric indexes in the grid-placement properties count from the edges
of the explicit grid. Positive indexes count from the start side
(starting from 1 for the start-most explicit line), while negative
indexes count from the end side (starting from -1 for the end-most
explicit line).
and here...
8.3. Line-based Placement: the
grid-row-start
,grid-column-start
,grid-row-end
, andgrid-column-end
propertiesIf a negative integer is given, it instead counts in reverse, starting
from the end edge of the explicit grid.
Making a grid area span an entire column / row, including implicit tracks, when the number of tracks in the grid is unknown, is not possible in CSS Grid Level 1, unless you want to try to hack it.
For CSS grid, is specifying grid-row-start the same number as grid-row-end the same as differing by 1?
From the specification there is some special rules to handle some particular cases:
If the placement for a grid item contains two lines, and the start line is further end-ward than the end line, swap the two lines. If the start line is equal to the end line, remove the end line.
If the placement contains two spans, remove the one contributed by the end grid-placement property.
If the placement contains only a span for a named line, replace it with a span of 1.
Basically, if the end line is the same as the start line, it's not valid so the browser will remove the end line and it will fall to auto
Then you can read:
grid position
The grid item’s location in the grid in each axis. A grid position can be either definite (explicitly specified) or automatic (determined by auto-placement).
grid span
How many grid tracks the grid item occupies in each axis. A grid item’s grid span is always definite, defaulting to 1 in each axis if it can’t be otherwise determined for that axis.
And also
auto
The property contributes nothing to the grid item’s placement, indicating auto-placement or a default span of one. (See § 8 Placing Grid Items, above.)
So if you define grid-column:1/1
it means you defined grid-column-start = grid-column-end = 1
. We remove the end and it's like you only have grid-column-start:1
and by default the span is 1 so visually you will have the same result as doing grid-column:1/2
We can say both are the same but the first one (defining the same number) will be considered as invalid and the Grid Placement Conflict Handling will make it behave as the second one which is the correct way to do.
Pay attention as this is not the same when dealing with negative values. See this related question: Understanding grid negative values
There is propably other particular cases but you should avoid using the same number because it's not logical and you will rely on the browser to correct your mistake.
Why are these Random Numbers slowly gravitating towards a negative value?
Due to the floating point standard that Javascript implements, doing arithmetic with decimal values is buggy to say the least...
One work around is to convert the decimals to integers by multiplying by 100, doing the math then dividing by 100.
This only works well if you have at most 2 decimals places. If you require more precision than that, I would recommend a language other than Javascript for that part.
Related Topics
What Elements Can a Span Tag Contain in HTML5
How to Make a Sticky Footer Using Flexbox in Ie11
Center Navbar Links Without Brand Pushing It to the Right in Bootstrap 4
CSS Show Div Background Image on Top of Other Contained Elements
Space Between Buttons with Bootstrap Class
How to Display Previous and Next Images with a Bootstrap Carousel
Twitter-Bootstrap Closing Alert Does Not Work
In What Conditions We Can Use CSS * Selector
100% Width Minus Margin and Padding
Different CSS for Each Browser
How to Strike Through Obliquely with CSS
Highlighting the Current Page in a List of Links Using CSS/Html
How to Make a 2 Column Layout Where Items Starts from Top to Bottom (Not Left to Right)