How To Do Grid Template Areaas In React
Filigree template areas
In the previous guide we looked at grid lines, and how to position items against those lines. When you utilise CSS Grid Layout yous always have lines, and this tin can be a straightforward way to place items on your grid. However, there is an alternate method to apply for positioning items on the grid which you can utilise alone or in combination with line-based placement. This method involves placing our items using named template areas, and we will find out exactly how this method works. You will run across very chop-chop why we sometimes call this the ascii-fine art method of grid layout!
Naming a grid area
You lot accept already encountered the filigree-area property. This is the property that tin take as a value all four of the lines used to position a grid area.
.box1 { grid-area : 1 / i / four / two; } What we are doing hither when defining all iv lines, is defining the area by specifying the lines that enclose that surface area.
We can as well define an area past giving it a name and and then specify the location of that area in the value of the grid-template-areas belongings. Y'all can cull what yous would like to proper name your expanse. For case, if I wish to create the layout shown below I tin can identify 4 main areas.
- a header
- a footer
- a sidebar
- the main content
With the grid-area property I can assign each of these areas a name. This will not nevertheless create any layout, but nosotros now have named areas to use in a layout.
.header { filigree-area : hd; } .footer { grid-expanse : ft; } .content { grid-area : principal; } .sidebar { filigree-expanse : sd; } Having defined these names I then create my layout. This fourth dimension, instead of placing my items using line numbers specified on the items themselves, I create the whole layout on the filigree container.
.wrapper { display : filigree; filigree-template-columns : echo (ix, 1fr) ; grid-machine-rows : minmax (100px, auto) ; grid-template-areas : "hd hd hd hard disk hard disk drive hard disk hd hd hd" "sd sd sd main main primary main principal main" "ft ft ft ft ft ft ft ft ft" ; } <div form = "wrapper" > <div form = "header" > Header </div > <div form = "sidebar" > Sidebar </div > <div class = "content" > Content </div > <div class = "footer" > Footer </div > </div > Using this method we do not demand to specify anything at all on the individual grid items, everything happens on our grid container. We tin see the layout described as the value of the grid-template-areas holding.
Leaving a grid cell empty
We accept completely filled our filigree with areas in this instance, leaving no white space. However you can go out grid cells empty with this method of layout. To leave a cell empty utilize the full stop character, '.'. If I want to merely brandish the footer directly under the chief content I would need to leave the three cells underneath the sidebar empty.
.header { grid-expanse : hd; } .footer { grid-area : ft; } .content { filigree-expanse : main; } .sidebar { filigree-surface area : sd; } .wrapper { display : filigree; grid-template-columns : repeat (9, 1fr) ; grid-auto-rows : minmax (100px, auto) ; grid-template-areas : "hd hard disk drive hd hd hd hd hd hd hd" "sd sd sd main main main main principal main" ". . . ft ft ft ft ft ft" ; } <div class = "wrapper" > <div class = "header" > Header </div > <div class = "sidebar" > Sidebar </div > <div class = "content" > Content </div > <div grade = "footer" > Footer </div > </div > In order to brand the layout neater I can use multiple . characters. As long as there is no white infinite between the full stops information technology will exist counted as one cell. For a complex layout there is a benefit to having the rows and columns neatly aligned. It ways that you can actually see, correct there in the CSS, what this layout looks like.
Spanning multiple cells
In our instance each of the areas spans multiple grid cells and we reach this past repeating the name of that filigree area multiple times with white space between. You tin can add together extra white space in guild to go on your columns neatly lined up in the value of grid-template-areas. You can encounter that I have done this in gild that the hd and ft line up with main.
The surface area that yous create by chaining the area names must be rectangular, at this point there is no way to create an 50-shaped surface area. The specification does note that a futurity level might provide this functionality. Y'all tin can however bridge rows only every bit easily as columns. For instance nosotros could make our sidebar bridge downwardly to the end of the footer by replacing the . with sd.
.header { grid-surface area : hd; } .footer { grid-area : ft; } .content { filigree-area : master; } .sidebar { filigree-area : sd; } .wrapper { display : filigree; grid-template-columns : repeat (ix, 1fr) ; grid-auto-rows : minmax (100px, machine) ; grid-template-areas : "hd hd hard disk drive hard disk hd hd hd hd hard disk drive" "sd sd sd main principal main master main main" "sd sd sd ft ft ft ft ft ft" ; } The value of grid-template-areas must prove a consummate grid, otherwise information technology is invalid (and the property is ignored). This means that y'all must have the same number of cells for each row, if empty with a full stop graphic symbol demonstrating that the cell is to exist left empty. Y'all will too create an invalid grid if your areas are not rectangular.
As our layout is now contained in 1 function of the CSS, this makes information technology very easy to make changes at dissimilar breakpoints. You lot tin practice this by redefining the grid, the position of items on the grid, or both at in one case.
When doing this, define the names for your areas exterior of whatsoever media queries. That style the content surface area would e'er be called principal no matter where on the grid it is placed.
For our layout above, we might similar to have a very simple layout at narrow widths, defining a single cavalcade grid and stacking up our items.
.header { filigree-area : hd; } .footer { grid-surface area : ft; } .content { grid-area : main; } .sidebar { grid-area : sd; } .wrapper { display : filigree; filigree-auto-rows : minmax (100px, automobile) ; filigree-template-columns : 1fr; grid-template-areas : "hard disk drive" "primary" "sd" "ft" ; } We can then redefine that layout inside media queries to get to our ii columns layout, and peradventure accept it to a 3 cavalcade layout if the available infinite is even wider. Annotation that for the wide layout I keep my nine column track grid, I redefine where items are placed using grid-template-areas.
@media ( min-width : 500px) { .wrapper { grid-template-columns : repeat (9, 1fr) ; filigree-template-areas : "hard disk drive hd hd hd hard disk drive hd hd hd hard disk drive" "sd sd sd master principal primary main main main" "sd sd sd ft ft ft ft ft ft" ; } } @media ( min-width : 700px) { .wrapper { filigree-template-areas : "hard disk drive hard disk hd hd hard disk hd hd hd hd" "sd sd main main master main main ft ft" ; } } Using grid-template-areas for UI elements
Many of the grid examples you will observe online make the assumption that y'all will employ filigree for main page layout, however grid can be just as useful for pocket-sized elements as those larger ones. Using grid-template-areas can be peculiarly nice as it is easy to see in the code what your element looks like.
Media object case
As a very unproblematic example we can create a "media object". This is a component with space for an paradigm or other media on one side and content on the other. The prototype might exist displayed on the right or left of the box.
Our grid is a two-column rails grid, with the column for the prototype sized at 1fr and the text 3fr. If y'all wanted a stock-still width image expanse, then y'all could prepare the paradigm column as a pixel width, and assign the text area 1fr. A single column rails of 1fr would then take up the rest of the space.
Nosotros give the prototype area a grid area name of img and the text area content, then we tin lay those out using the grid-template-areas holding.
* { box-sizing : border-box; } .media { border : 2px solid #f76707; edge-radius : 5px; groundwork-color : #fff4e6; max-width : 400px; } .media { display : grid; filigree-template-columns : 1fr 3fr; filigree-template-areas : "img content" ; margin-bottom : 1em; } .media .image { grid-surface area : img; background-color : #ffd8a8; } .media .text { grid-area : content; padding : 10px; } <div class = "media" > <div class = "image" > </div > <div form = "text" > This is a media object example. We can utilize grid-template-areas to switch around the prototype and text part of the media object. </div > </div > Displaying the prototype on the other side of the box
Nosotros might want to be able to display our box with the image the other mode around. To do this, we redefine the filigree to put the 1fr track last, and flip the values in filigree-template-areas.
* { box-sizing : border-box; } .media { border : 2px solid #f76707; border-radius : 5px; groundwork-color : #fff4e6; max-width : 400px; } .media { display : grid; grid-template-columns : 1fr 3fr; grid-template-areas : "img content" ; margin-bottom : 1em; } .media.flipped { filigree-template-columns : 3fr 1fr; grid-template-areas : "content img" ; } .media .image { grid-expanse : img; background-color : #ffd8a8; } .media .text { grid-area : content; padding : 10px; } <div class = "media flipped" > <div class = "epitome" > </div > <div class = "text" > This is a media object example. We can apply grid-template-areas to switch effectually the image and text part of the media object. </div > </div > Filigree definition shorthands
Having looked at various ways of placing items on our grids and many of the backdrop used to define grid, this is a adept time to have a await at a couple of shorthands that are available for defining the grid and many things virtually it all in i line of CSS.
These tin can speedily go difficult to read for other developers, or even your future self. All the same they are part of the specification and it is likely you lot will come beyond them in examples or in use by other developers, fifty-fifty if you cull not to employ them.
Earlier using whatever shorthand information technology is worth remembering that shorthands non simply enable the setting of many properties in ane become, they too act to reset things to their initial values that you do not, or cannot set in the shorthand. Therefore if yous use a shorthand, be aware that it may reset things you accept applied elsewhere.
The two shorthands for the grid container are the Explicit Filigree Shorthand grid-template and the Filigree Definition Autograph grid.
grid-template
The grid-template property sets the post-obit properties:
-
grid-template-rows -
grid-template-columns -
grid-template-areas
The property is referred to as the Explicit Grid Shorthand because it is setting those things that you control when you define an explicit grid, and non those which bear on whatsoever implicit row or column tracks that might be created.
The post-obit lawmaking creates a layout using grid-template that is the same equally the layout created earlier in this guide.
.wrapper { display : grid; filigree-template : "hd hard disk hd hd hard disk drive hard disk hard disk hd hd" minmax (100px, machine) "sd sd sd main chief main main primary main" minmax (100px, auto) "ft ft ft ft ft ft ft ft ft" minmax (100px, auto) / 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr ; } The first value is our grid-template-areas value only nosotros too declare the size of the row at the end of each row. This is what the minmax(100px, auto) is doing.
Then later on grid-template-areas nosotros take a forward slash, afterwards that is an explicit runway listing of column tracks.
grid
The grid autograph goes a step further and also sets backdrop used past the implicit filigree. So y'all will be setting:
-
grid-template-rows -
filigree-template-columns -
grid-template-areas -
grid-motorcar-rows -
grid-auto-columns -
grid-auto-flow
You can apply this syntax in the exact same mode equally the grid-template autograph, only exist aware than when doing then y'all will reset the other values set by the property.
.wrapper { display : grid; filigree : "hd hd hd hard disk drive hd hard disk drive hd hd hd" minmax (100px, auto) "sd sd sd chief principal main main main main" minmax (100px, automobile) "ft ft ft ft ft ft ft ft ft" minmax (100px, motorcar) / 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr ; } We will revisit the other functionality offered by this shorthand subsequently in these guides when we take a look at auto placement and the filigree-auto-flow belongings.
If you have worked through these initial guides you at present should exist in a position to create grid layouts using line-based placement or named areas. Take some time to build some common layout patterns using grid, while there are lots of new terms to larn, the syntax is relatively straightforward. As you lot develop examples, you lot are likely to come up with some questions and apply cases for things we haven't covered yet. In the residuum of these guides we will be looking at some more of the particular included in the specification – in gild that you can begin to create avant-garde layouts with information technology.
How To Do Grid Template Areaas In React,
Source: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Grid_Template_Areas
Posted by: cobblacce1986.blogspot.com

0 Response to "How To Do Grid Template Areaas In React"
Post a Comment