# Changing the background color of one element in a Grid

Consider the following code:

Grid[Table[x, {4}, {7}], Background -> {4 -> LightRed}]


This highlights column 4:

Grid[Table[x, {4}, {7}], Background -> {None, {4 -> LightRed}}]


This highlights row 4:

How do you highlight an individual cell, say row 3, column 5?

-
Documentation Center: Grid[Table[x, {4}, {7}], Background -> {None, None, {{1, 1} -> Pink, {3, 3} -> Red}}] (section: Background) – Pinguin Dirk Aug 26 '13 at 17:15

As Pinguin Dirk comments there is an example of exactly this in the documentation:

Grid[Table[x, {4}, {7}],
Background -> {None, None, {{1, 1} -> Pink, {3, 3} -> Red}}]


Those who value brevity may wish to note that implicit Null may be used in place of None:

Background -> {, , {{1, 1} -> Pink, {3, 3} -> Red}}


You can also use Item and MapAt:

tbl = Table[x, {4}, {7}];

MapAt[Item[#, Background -> Pink] &, tbl, {{3, 3}, {2, 6}}] // Grid


You could also use Style but that won't fill the cell (can be finetuned by using Pane though):

{Grid[{{1, 2}, {3, Style[4, Background -> Pink]}}, Frame -> All],
Grid[{{1, 2}, {3, Item[4, Background -> Pink]}}, Frame -> All]}


-
Bleh - I'm normally really good about finding it in the documentation. I Just missed that one example when I was looking through it. Thanks for adding the alternate way using Item and MapAt. – tjm167us Aug 26 '13 at 17:38
I hope you don't mind! Inferior in general, but useful sometimes. – István Zachar Aug 26 '13 at 20:06
@István No I don't mind. It's a good note, but as you say usually not ideal. – Mr.Wizard Aug 26 '13 at 20:15
@Kuba Oh, that answer? I probably just assumed you were smoking crack. ;^) Seriously if that works now you should post a new answer to the question I just linked in the comment above. – Mr.Wizard Aug 26 '13 at 20:39
@Kuba Why don't you add mention of that to your answer? The poor computational complexity of MapAt is a long-known problem. This enhancement is going to significantly extend the practical applications of MapAt. – Mr.Wizard Aug 26 '13 at 21:21