Css grid formatting in intellij idea

Css grid formatting in intellij idea

3 minutes 15s

How to get css grid stylesheets to keep their format in intellij idea products.

Css grid is a bit like ascii art. The columns and row represent the layout. Because of this, they are usually formatted carefully.

Often, the code formatter puts all the rules in one line. For regular css rules this is usually no problem. For css grid rules, it removes all the intuition about how the rules look like (the ascii art component)

We will se a solution to this below.

The problem with formatting css grid stylesheets

Formatting tools like intellij, transform this:

display: grid;
grid-template-areas:
    'header login'
    'sidebar content'
    'footer footer';
grid-template-rows: min-content 1fr min-content;
grid-template-columns: 1fr 1fr;
    
    
    

Into this

Notice where arranged exactly as they would show on the screen (For example, header and login were clearly the elements in the first row, and sidebar was the leftmost element of the second row)

Now they all show in a line. We need to check which are inside each pair of single apostrophes (')

display: grid;
grid-template-areas: 'header login' 'sidebar content' 'footer footer';
grid-template-rows: min-content 1fr min-content;
grid-template-columns: 1fr 1fr;
    
    

The solution: mark sections as not-to-format

We instruct intellij not to format between the

@formatter:off
and
@formatter:on
markers.

display: grid;
/*@formatter:off*/
grid-template-areas:
    'header login'
    'sidebar content'
    'footer footer';
/*@formatter:on*/
grid-template-rows: min-content 1fr min-content;
grid-template-columns: 1fr 1fr;