logo
logo
logo
logo
logo
logo
logo
logo
logo
logo
logo

CSS Grid: Revolution in Web Design?


Advertisement
CSS Grid: Revolution in Web Design? When it started talking about a new CSS module about a year ago, allowing designers to use a grid on web page, I considered it a "fashion wave" at first. The designers got a new toy and they are excited. When the grid support later appeared in a number of browsers and I tried to use it, I had to admit that this toy could be really useful. After long years of nesting and general markup orgies, something has emerged that makes it possible to make brilliant pieces and at the same time motivates more to simplify the code. The basic idea of a typographic grid is to divide the page area into certain fields, say five columns and seven rows. This division is regular, all columns are equally wide, all rows are the same high, and consistent throughout ..

Join us


By signing-up I agree with your T&C

353 clients
508,320 pings / day
20 server outages today

CSS Grid: Revolution in Web Design?

When it started talking about a new CSS module about a year ago, allowing designers to use a grid on web page, I considered it a "fashion wave" at first. The designers got a new toy and they are excited.

When the grid support later appeared in a number of browsers and I tried to use it, I had to admit that this toy could be really useful. After long years of

nesting and general markup orgies, something has emerged that makes it possible to make brilliant pieces and at the same time motivates more to simplify the code.

The basic idea of a typographic grid is to divide the page area into certain fields, say five columns and seven rows. This division is regular, all columns are equally wide, all rows are the same high, and consistent throughout the publication. When images and text blocks are placed on the site, they always occupy a rectangular area formed by several adjacent fields - like 2 × 3. These areas vary according to need, so each side can look different, but since they respect the boundaries of the same grid, even the differently arranged sides are consistent and hold the book as a whole.

The web grid works the same, again dividing the page into a rectangular network of fields that are then filled with content. There are, of course, differences, for example, often not regular. It's a bit like the woody times of spreading tables, but the grid is smarter, stronger and does not smash HTML tags.

Its specification can be found in the CSS Grid Layout Module Level 1 document, which is still in the candidate status, but it is considered to be practically complete and is widely implemented in its current form. CSS Grid supports the vast majority of current browsers. The problem is with Microsoft, which implemented an early draft, which is very different from the final specification. Internet Explorer is already set to remain in this state, Edge has begun to support the current definition of the grid since version 16. Bottom line: not to worry too much, the grid can already be deployed on production sites.

Create a grid

We will not go through the entire specification and all CSS properties, we focus on the basic principles of functioning and practical application. The starting point is that the element in which you want to create a grid will assign the display: grid property. If you have courage, it can be just . More likely, it will be a wrapper

, but you can basically create a grid in any element, so it can only pop a portion of the page.

Children of the element (ie, direct descendants without any intermediate elements) become grid elements and placed in individual areas. This is important - only direct descendants of the display: grid can be inserted into the grid. You can not drag an element deeply embedded in HTML and paste it into one of the areas.

In addition to the introduction of the grid, you still need to define what it looks like. This is done by using the grid-template-columns and grid-template-rows properties. It shows how many columns and rows the grid will have and what their dimensions will be. The values are spaces separated by dimensions. They can be freely mixed, so for example definitions

grid-template-columns: 200px auto 25%;

means that the grid will have 3 columns, the first one will be a wide 200 pixels, the last 25% of the total width, and the middle will adapt according to its content, usually takes up the remaining free space. An interesting option is the fr. It is flexible and stretches so that it takes up all the free space. If there are more flexible dimensions, divide the space in the ratio by numbers before fr. Definition

grid-template-columns: 200px 3fr 1fr;

introduces three columns again. The first one will be a wide 200 pixels. The location that remains after it is subtracted will be divided into quarters. Three of them fall into the second column and one in the third column. Beware, the same thing as the auto. 1fr takes up all the free space while the auto expands as needed. The most striking difference is when they appear more side by side.

grid-template-columns: 1fr 1fr;

means two equally wide columns while

grid-template-columns: auto auto;

creates two columns whose width may vary depending on the size of their content.

Let us close this part by way of example. Let's say we want to create a page with a header and footer across the width, including three columns - two narrower on the sides with a menu and some side content, then the main content in the middle. So we have a 3 × 3 grid. If we are courageous and create it for the entire body of a page, the definition might look like this:

body {
    display: grid;
    grid-template-columns: 10rem 2fr 1fr;
    grid-template-rows: auto auto auto;
}

In fact, we could forgive the definition of rows because the auto is their starting height and they are automatically added to the grid as needed.

Placing elements

We have a grid, let's put some content into it. This can be done in several ways. Let's start with the most basic of exactly where you determine where to get what element. In the CSS grid, there are numbers (fictitious) lines separating individual columns or rows. The images above are shown even though they will not be rendered on the page. It is numbered from one, so for example the first column extends from line 1 to line 2.

If the header (the <header> element) extends across the entire width of the page, it means that it extends from the column line 1 starting the first column along the line 4 ending the third column. It occupies the first row of the grid, from a line line view of 1 to 2:

header {
    grid-column: 1 / 4;
    grid-row: 1 / 2;
}

Footer is defined similarly, just in the last row:

footer {
    grid-column: 1 / 4;
    grid-row: 3 / 4;
}

In the examples, I assume that the HTML code of the page is very straightforward, and the key elements are the children of the <body> element, that is, the body of the page looks something like this:

    
...
    
    
...
    
    
...

You can go "wild" with placements. For example, it is possible to assign names to individual lines when defining rows or columns, and to work with names when positioning them. Behind the slash can also be the span N value, which means that the element is to be stretched over N columns or rows. We could write the same header location as follows:

header {
    grid-column: 1 / span 3;
    grid-row: 1 / span 1;
}

Negative numbers count from the end. So headers over the full width of the grid, regardless of the number of its columns, can be easily defined using

header {
    grid-column: 1 / -1;
    grid-row: 1 / 2;
}

Beware that an explicit placement can extend an existing grid. If you place the content in columns or rows that do not exist, they will be automatically created. For example, if you place a header in the three-column grid from the above example

header {
    grid-column: 1 / 6;
    grid-row: 1 / 2;
}

the fourth and fifth columns with the width auto are added. If the rest of the design does not count, the content of the page may be scattered. Therefore, pay great attention to the consistency of row and column numbers. Negative numbers do not work for automatically added rows and columns, so beware of them.

An important concept is automatic placement. Assigning an element to the display: grid property means that all of its children will be inserted into its grid. If you do not specify the location yourself (for example, using the grid-column and grid-row), the browser automatically places them in the first free field.

As a result, our definition above is basically enough to create a page based on your original ideas. We placed the first line in the header, we fed the third and the other remained free. When a HTML viewer hits the main menu (<nav> element) whose location is not specified, it places it in the first free field, the first field in the second row, the left column in the main page of the page. <main> is automatically located in the middle and <aside> in the right field.

Using a slot machine for a basic page layout is not happy twice. If you make a minor mistake somewhere and you get an extra element in the site, it's easy that its automatic insertion completely splits the placement of its successors. Therefore, we strongly recommend that you explicitly define their placement for basic page components.

Conversely, for a newspaper page page, the slot is very convenient because it allows you to simply insert texts and images that the slot spreads over the grid. Separate the sections simply by stretching the headings (say

) over all the grid columns to ensure that the section headline always takes a full line will be placed under all the elements in the previous section, and the following elements will find themselves in the grid below it.

Using span can interfere with regularity. Definition in the form of auto / span 2 means that the first column of the element is to be determined automatically, but the element is to be stretched over two columns. If you would like to place the solo cartridges all the way to the left and devote them to a space of 2 × 2 grid array, their definition could include

.yourname {
    grid-column: 1 / span 2;
    grid-row: auto / span 2;
}

Named areas

Another option for deploying elements in a grid is to name the individual areas and the input elements to specify the names of the areas in which they are to be located. This method is my favorite because it makes the design responsive. Depending on the width of the window, you only change the definitions of columns (and possibly rows) and the definition of areas. Everything else remains unchanged.

How does it work? At the level of the element defining the grid, you name areas. This is the grid-template-areas property. The value is a space-separated list of strings, each corresponding to one row (so it's better to type them underneath). In it, the names of areas in each field are separated by spaces. The same name can occur repeatedly - it means that the area reaches across all such fields. It must form a rectangle, the region can not be discontinuous or curved.

In the case of our sample page, the definition would look something like this:

body {
    display: grid;
    grid-template-columns: 10rem 2fr 1fr;
    grid-template-areas:
        "heading heading heading"
        "menu    main  sidebar"
        "footer  footer  footer";
}

It introduces five named areas, from which the heading area occupies all three fields of the first line, footer is the entire last line, and the remaining three areas occupy only one field. The names are quite arbitrary, of course, it is appropriate to be mnemonic.

You then paste the elements into the named area using the grid-area property whose value is the appropriate name. The above-mentioned declaration for placement of individual elements would be replaced by the following:

header     { grid-area: heading; }
.mainmenu  { grid-area: menu; }
main       { grid-area: main; }
.sidebar   { grid-area: sidebar; }
footer     { grid-area: footer; }

All window width responses (or any other factors) will be limited to changes in the definition of the grid and the named areas. Let's say for narrow displays, we'd like to organize the page into a single column where all the areas will be underneath. In addition, we move the menu to below the content. Only when the page width exceeds 45 rem will go to the three-column arrangement. The definition of such behavior would look something like this:

body {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-areas:
        "heading"
        "main"
        "sidebar"
        "menu"
        "footer";
}

 
header     { grid-area: heading; }
.mainmenu  { grid-area: menu; }
main       { grid-area: main; }
.sidebar   { grid-area: sidebar; }
footer     { grid-area: footer; }

 
@media ( min-width: 45rem ) {
    body {
        grid-template-columns: 10rem 2fr 1fr;
        grid-template-areas:
            "heading heading heading"
            "menu    main  sidebar"
            "footer  footer  footer";
    }
}

Pleasant is that dramatic changes to page layout are made by editing only two features (or three if you work with row definitions) and everything is together. So, with a little work, and the risk of forgetting something, creating an inconsistent definition and breaking down your page is quite low.

And even more

As has been said, the grid can extend across the entire page or cover only a portion of it. And of course you can have one grid inside the other. For example, the cover page of the newspaper might have one full-page grid where basic header elements, main menus, and so on would be defined. Its main content section would then define its own grid, where tugs for individual articles would automatically be placed in the tile layout. The definition of the <main> element would include both the properties of the element placed in the grid and the definition of the new grid for its contents:

main {
    grid-area: main;

 
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-column-gap: 0.5rem;
    grid-row-gap: 0.5rem;

}

The grid also fits well with other technologies such as flexbox. It might be possible to solve a menu inserted into one of the areas. The general rule for designing more sophisticated methods of element layout: For a two-dimensional layout, a grid is more suitable for a one-dimensional flexbox. The counter examples will certainly be found.

Repeating the same values in the definition of columns (rows) can be annoying. Therefore, a repeat (number, width) is available, which inserts the appropriate number of given widths. The division into 4 equal columns from the example above could be entered

grid-template-columns: repeat(4, 1fr);

They can be combined with others. If you would like a 10-width column rem, followed by three equally wide columns and a double width at the end, use

grid-template-columns: 10rem repeat(3,1fr) 2fr;

Designing more complex content is usually based on one of two basic ideas - either you have a clear idea of the number of columns, or you have an idea of the width of the columns, and you would like their number to fit the width of the available space.

The first case is clear, the relevant constructions have already been described. For the other, use repeat (), where auto-fit or auto-fill is used as the number of reps. They behave the same, inserting as many columns as possible of the dimensions. It is different that if no material is inserted in the columns, auto-fill will keep them while auto-fit is deleted (they are formally retained, but have a zero width and merge their edges). As many columns of width 15 as possible would ensure the declaration

grid-template-columns: repeat(auto-fit, 15rem);

However, at a fixed column width, a gap is usually created at the end because the width of the space is not an exact multiple of the width of the column. Therefore, the column width can be defined by minmax (the smallest, the largest) and leaving the browser a certain amount of space to expand the columns by stretching the columns. A favorite wage limit is 1 fr, which allows the columns to stretch as needed (but they still try to put as much as possible so that the maximum width does not exceed twice the minimum). The previous definition could be improved to

grid-template-columns: repeat(auto-fit, minmax(15rem,1fr));

Again, you can look at the example. Try changing the width of your browser window and see how the dimensions and number of columns change without the usual @media conditions.

Grid in practice or how is it really working in browsers

Let's go back to Microsoft's patriotism and backward compatibility in general. To ensure that the properties associated with the grid do not confuse clients who do not know it, make sure that they are supported before using them. The first candidate is offered

@supports ( display: grid ) { ... }

But you can come across MSIE (and older versions of MS Edge) that they think supports this feature. In fact, they know the 2011 version. Therefore, it is a good idea to test the support for some of the features that are up to the current specifications. They include grid-template-columns, grid-template-rows, or grid-template-areas. These properties changed names, and Microsoft's browsers did not follow them. A bit shorter is the grid-auto-flow that affects automatic placement. A suitable test may look like that

@supports ( grid-auto-flow: row ) {
    Grid definition and its parts
}

Here, I would end the tasting of the grid CSS. The text is far from exhaustive, it is just a brief introduction to the possibilities. A good source for more detailed study is the A Complete Guide to Grid, where you can find an overview of all CSS features for parents defining the grid and the children inserted into it.

Overall, the CSS Grid appears to be a very promising mechanism. It combines great expressive power with a simple code. After a long time, a CSS concept appears, which tends to simplify HTML code instead of wrapping meaning elements with other and <div> to achieve different effects. It is definitely worth the attention.

ProviderPlan
Cost / mo

Hard drive

Traffic
CountryUptime
LANSOL GmbHzyanWEB ULTIMATEGBPUnlimited100GBGermany Germany99.2%
Description: create cloud server, cloud based server hosting, cloud application server
hostasp.netEconomyGBP1GB5GBUnited States United States99.041%
Description: ruby server monitoring, gfi server monitor, wow mop private server
bookandhost.comWin Book 1GBGBP10Gb1GbUganda Uganda99.595%
Description: cloud based server backup solutions, server monitoring tool, cloud hosted servers
netregistry.com.auStarterGBP500GB20GBAustralia Australia99.503%
Description: dedicated server hosting australia, cloud based server backup, server backups
DomainFactoryResellerDedicated XL7GBP149 Unlimited31,25GBGermany Germany99.857%
Description: cloud backup services for servers, server monitoring cloud, windows server monitoring tools
http://www.sweb.cz/PlusGBPUnlimited2GBCzech Republic Czech Republic99.024%
Description: exchange server monitoring, windows server recovery, server monitoring
Internetagentur WasBasic Host 4GBPUnlimited4.88GBGermany Germany99.082%
Description: running wordpress on windows server, linux server monitoring, windows 2008 server backup
kattare.comCorporateGBP28 100GB1.5GB99.366%
Description: cloud file servers, sql server backup strategy, small business server backup solutions
twosteps GmbHProfiGBPUnlimited1.46GBGermany Germany99.91%
Description: systems management server, windows cloud servers, cloud backup servers
windowwebhosting.comProGBP5GB250MBIndia India99.512%
Description: best server backup solution, server cloud canada, cost of cloud server
Net-Build GmbHNet-Build BusinessGBP17 Unlimited24,41GBGermany Germany99.92%
Description: xen server backup, monitor windows server performance, server backup solution
AAPTWebBizGBP47 2000 MB250 MBAustralia Australia99.802%
Description: back up servers, server on cloud, cloud server setup
goneo.deStartGBP100GB20GB99.704%
Description: cloud server host, cloud server services, server disaster recovery
help99PrivatGBPUnlimited3,91GBGermany Germany99.118%
Description: how to backup server, performance monitor windows server 2008 r2, monitoring server performance
hostcaters.comCaterBitGBP10Gb100MbUnited States United States99.876%
Description: online server backup solutions, monitoring server software, cloud vs server
1&1 Internet AG1&1 Unlimited Pro WindowsGBP18 UnlimitedUnlimitedGermany Germany99.689%
Description: server image backup, sql server backup table, cloud virtual servers
contabo.comLGBPUnlimited100GB99.26%
Description: cloud server solutions, window server backup, cloud backup for servers
onlinerack.comBronzeGBP30GB1GBUnited States United States99.278%
Description: hp server monitoring software, australian dedicated server hosting, servermonitor
zzz.comBusinessGBP1GB99.883%
Description: server network monitoring software, windows server 2003 installation, server network monitoring
mdg-it.com.auDevGBP1GB500MB99.267%
Description: server backup system, online server backups, cloud based mail server
fateback.comLight CloudsGBPUnlimitedUnlimited99.937%
Description: online server backup, windows server backup system state, cloud plex server
http://www.datahousing.cz/UnlimitedGBPUnlimited999GBCzech Republic Czech Republic99.283%
Description: cloud server provider, server monitoring dashboard, simple server monitoring
DM Solutions e.K.Reseller Basic v3GBP31 Unlimited35.00GBGermany Germany99.073%
Description: cloud servers reviews, server 2008 image backup, sql server with check option
STATION55Germany Webhosting - Einsteiger Bronze HostingGBPUnlimited5GBGermany Germany99.781%
Description: backblaze server backup, creating a cloud server, web server monitoring tools
mumbaihosting.comGoldGBP29 10GB2GB99.977%
Description: cloud server costs, windows server 2003 group policy editor, best server backup
ipmirror.comLinuxGBP14 100GB20GBSingapore Singapore99.117%
Description: cloud backup server, datacenter server architecture, online backup servers
icdsoft.comBusinessGBP1000GB100GBBulgaria Bulgaria99.957%
Description: server backup tools, server cloud, server performance monitoring
clansitemanager.comCompleteGBP36 UnlimitedUnlimited99.589%
Description: web server monitoring, build a cloud server, windows server 2008 system restore



Free variant

Free

  • Non-commerce
  • 1 website
  • 10 minutes interval

Basic variant

GBP 9 / Month

  • Professional usage
  • Up to 100 websites
  • 1 minute interval

Unlimited variant

GBP 136 / Month

  • Professional usage
  • Up to 1000 websites
  • 10 seconds interval

Join us


By signing-up I agree with your T&C


↑ Scroll Up ↑