I created a grid of size 200,200 and placed a button in it of size 200×200.

Grid[{{Button["Button", Null, ImageSize -> {200, 200}]}}, 
       Background -> Red, ItemSize -> {15, 15}, 
       Alignment -> {Center, Center}, Frame -> True]

Mathematica graphics

When I tried increasing the width of the button, the edges on the 2 sides didn't appear as expected, but when I increased the height of the button, the grid itself gets enlarged to the button's height. I don't want this enlargement to happen. How can I avoid it?

  • $\begingroup$ Can you provide your code? There are probably a few things happening here but best to see your code first before writing answers. $\endgroup$ Dec 22 '12 at 6:41
  • $\begingroup$ Grid[{{Button["Button", Null, ImageSize -> {200, 200}]}}, Background -> Red, ItemSize -> {15, 15}, Alignment -> {Center, Center}, Frame -> True] . Try increasing the width and height of the button. $\endgroup$
    – santhosh
    Dec 22 '12 at 6:46
  • $\begingroup$ If you have the button large than the grid cell what are you wanting to see happen? If the grid cell doesn't expand the alternative is for the button to be partially obscured/cut off. You can achieve that with Pane. It is unclear to me what you objective is. $\endgroup$ Dec 22 '12 at 7:13
  • $\begingroup$ actually, i want a grid within a panel. The panel height also increases when grid height increases.which i don't want. Can i use a pane within a panel. $\endgroup$
    – santhosh
    Dec 22 '12 at 7:26

As the comments suggest, one could use Pane surrounding the Button, but then the alignment doesn't work correctly if the button is too large. So instead I'd use Framed because you don't seem to want the ability to resize the content, just crop it when it's too large:

Grid[{{Framed[Button["Button", Null, ImageSize -> {200, 300}], 
    ImageSize -> {210, 210}, Alignment -> {Center, Center}, 
    FrameMargins -> 0, FrameStyle -> None]}}, Background -> Red, 
 Alignment -> {Center, Center}, Frame -> True]

cropped button

To make this work, I had to specify the size of the item with an explicit ImageSize in Framed, instead of the ItemSize option in Grid. Here the button has been resized to be too large in the vertical direction, and you see that it's still centered but cropped at the top and bottom without changing the cell size.

The alternative of using Pane is especially useful if your plan is to have the content automatically resize to fit the fixed box, because Pane has the option ImageSizeAction -> "ResizeToFit", while Framed doesn't allow that.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.