named grid lines not working as expected
Hey, this is one of my first times using grid lines properly for a layout and i can't quite seem to get the layout to look like the sketch in the image with them like i can do when defining the set lines the children go on e.g.
grid-row: 1 / -1;
. The chidren don't seem to be sitting in their defined areas for some reason. For anyone wondering why i can't group the button in with the text, the layout shifts around at a desktop viewport, this layout is for a tablet viewport. Could anyone please point me in the right direction and let me know what i'm doing wrong? Thanks in advance.
https://codepen.io/deerCabin/pen/gONNKLy26 Replies
You may want to give a line more than one name, perhaps it denotes the sidebar-end and the main-start for example. To do this add the names inside the square brackets with whitespace between them [sidebar-end main-start]. You can then refer to that line by either of the names.https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_grid_layout/Grid_layout_using_named_grid_lines#giving_lines_multiple_names
MDN Web Docs
Layout using named grid lines - CSS: Cascading Style Sheets | MDN
In previous guides, we've looked at placing items by the lines created by defining grid tracks and also how to place items using named template areas. In this guide, we are going to look at how these two things work together when we use named lines. Line naming is incredibly useful, but some of the more baffling looking grid syntax comes from th...
I’ll give that a shot, thank you. What would be the difference between putting an end and start in the same bracket pair, vice versa over separate brackets?
I must admit that I really struggle with grid lines, finding grid areas much easier to understand however I do realize that each serves slightly different purposes.
As Alex has pointed out, the main issue that you have here is that you have created separate elements for the same line.
If items need to start or end on the same line, they need to be grouped in the same brackets.
For example your columns need to be defined like this:
I don't think that separate brackets is valid syntax.
Seperate brackets are not valid
Then, if an element is within a single row or column, you can either define
[text-start text-end] 1fr
or, slightly simpler, [text] 1fr
If you put them in separate brackets, it doesn't work, because that's not the syntax
Ah I see now, thank you all for your time and help, I’ve learnt something new there 👍🏻
How does this work? There's no grid area for [text-start text-end]? Doesn't there need to be some sort of length to define the column, even if it's
auto
?
As I say, I really struggle with grid lines. I am going to try to spend more time stuyding them when I finish work later as clearly they are quite powerful.
However I am fairly sure that to achieve what snxxwyy wants to do here (as far as I understand it) can be done by defining this on the grid:
or this which uses both the -start and -end on the text and buttons in the rows:
https://codepen.io/cbolson/pen/LYKKgoq
I got question for your example, where you set grid "area"s with grid row and grid column, is there no one prop that do both? or use grid-area?
I'm afraid that I don't understand your question, sorry.
you used grid-row and grid-column, right?
for ea
so i wanna know if there is one line for it
I see. You are right. It appears that you can use the ´grid-area: text;´ rather than defining each one (I have just modified my codepen)
As I say, I struggle with grid lines and admit that I don't fully understand them.
On a related note, I have added another version to create the same design using grid-template-areas as I personally find these easier to understand than all these lines 😆
i dont really understand grid myself so i wanted to know why you used it as you did, didnt mean to sound criticizing
I wrote it that way because that was how the OP had created the original codepen. My codepen was based on their code and I was really just concentrating on the issue that they had with defining the grid line names. I didn't look too closely at the children definitions as they were working "as is".
that's interesting you managed to do that with grid-template-areas. I thought you couldn't overlap content with those? is that what the dots do?
You can use grid-area with one value as Chris has shown but it’s just a shorthand for grid-column and grid-row so if you had different named areas for it between row and column you could also use something like
grid-area: text content
so you define both its name for column and its name for row in one line.
Interesting I thought it needed a length defined between start and end , didn’t realize you could just name a column [text] without specifying the width of the column. Gosh so much to learn with gridNo, the dots are for "null" cells. ie. they are not named and child content can't be assigned to them using the
grid-area
(though that doesn't mean that they can't have content).
Just to clarify the use of grid-area
rather than defining the start an end points.
From the csswg.org
The grid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end and grid-column-end.
Ah I see, so if you can overlap content with grid template areas, then isn’t grid template areas and grid lines technically the same thing?
The more I research, the more confused I get 🤪
I actually "cheated" by defining the starting and ending rows for the image so I suppose that that is using grid lines, albeit without naming them as such 🤔.
I have modified my pen since yesterday to define the grid area for the end of the image so now I have it like this: There are so many ways to define grid positioning it is quite confusing.
I have modified my pen since yesterday to define the grid area for the end of the image so now I have it like this: There are so many ways to define grid positioning it is quite confusing.
ah that makes sense then. yeah i'm actually quite surprised how many different ways there are haha, what to choose 😅.
i can imagine some methods are really niche though
Despite having used grid for years, I actually went right back to basics yesterday and watched some of Kevins initial videos from 7 years ago where he explained grid. Obviously more things have been added since then but they were still interesting.
that's a good shout actually, i might do the same thing at some point.
I didn’t think you could overlap content with gtareas either , even if you defined the rows and columns; this is very interesting
From what I can work out, grid-template-areas are really just short-hand for grid lines, with automatically defined -start and -end lines.
I did a quick simple test with this HTML:
This CSS:
I then opened the inspector on Chrome (as I can't work out how to show line names on Firefox) and, once I activated the grid display, could see that it had automatically assigned the grid-line names (see attachment).
Probably not a revelation to most, but I didn't realize that this was actually happening.