How do i create a show rule to change the fill for a specific grid.cell?

I want to style grids using show rules.

I have the following code:

#set page(margin:0pt)
#set grid(fill:gray)
    

//we can apply rules based on x
#show grid.cell.where(x:0): set text(fill:red)
#show grid.cell.where(x:1): set text(fill:white)
#show grid.cell.where(x:2): set text(fill:blue)


//But trying to style the cell fill does not work
// The documentation says its settable
#show grid.cell.where(x:1): set grid(fill:blue)
#show grid.cell.where(x:1): set grid.cell(fill:blue)


#grid(
    columns: (1fr,1fr,1fr),
    rows:1fr,
    [column-0],    
    [   
      = column-1
    ],
    [column-2]
)

The show rules targeting specific columns to change text work fine, but changing the grid or grid.cell fill do not seem to do anything.

According to the documentation the grid.cell fill is settable.

How can i style the grid cell fill using show rules?

Use callback function on the fill field like shown in the guide or docs.

That’s the neat part, you don’t. Just use it on the grid or in a set rule.

1 Like

That works, thanks @Andrew.

to be complete.
This did the trick:

#set grid(
  fill: (x, _) => if x==1 { blue }
  
)
2 Likes

Why do you need this if you change the color of each column anyway?

Settable implies set rule. Show-set rules are different.

I like to keep style out of my content and create a template where i can style all similar grids. This way the color-scheme becomes configurable in 1 place, instead of having to change every grid.

Oh, so there are other grids with global styling after all. Makes sense then. Though the implication is that they are probably more of a table than a grid. Well, unless you actually use a heading inside, then it’s a grid (unless you mean table.header).