Keep an icon and text centered in a button that has no set width

Howdy there everyone. I have a button that has an icon on the left and text to the right of it. The icon + text group should always be centered within the button no matter how long the text is.

This works fine for a normal button using horizontal layout. However, for this particular button I want to be able to stretch it’s width to take up the full width of the artboard, but also keep the icon and text centered.

It doesn’t seem to work with any group + layout + pinning combo I’ve tried, so I wanted to see if anyone has figured out a solution?

1 Like

Hey Jason!

Would you mind sharing a Document with your Component (it can be just it!) so we can take a look?

This is possible with Smart Layout, but with some caveats:

  • If you manually stretch (or expand) the button, and you decide to change the Override (Text), then Smart Layout will kick-in and resize the button to fit its contents. This means that if the button’s width was wider than the result of Smart Layout it will shrink it.
  • Pinning and Resizing options work so elements in the Canvas, react to their “parent”. So whenever you resize an Artboard (or even a Group) that have these options enabled, they will act accordingly and keep their edges pinned to whatever options you have set.

In any case, we’d really like to take a shot at this and see if we can help!

Hi Jason.
Does this do what you’re after?

2 Likes

Ohh I got to the same solution as @Brett ! I have a sample file If it helps :smiley:
centered button.sketch (106.9 KB)

2 Likes

I got it! @dfmedrano your sketch file wasn’t quite what I was trying to do, but I modified it slightly and it actually did what I needed. So I applied the changes to my file and it works now. Thanks!

So, the end goal was that I wanted to put instances of these buttons into another symbol, and the buttons would always stretch to be 100% of the width of the artboard. That was all fine if they were placed in an artboard, but when they were nested in another symbol, then overriding the text would kick in the smart layout resizing.

1 Like

centered button.sketch (112.5 KB)
Here’s the modified version of @dfmedrano 's file which does what I was trying to do.

3 Likes

Ohh!!

Sketch team coming in clutch! :face_holding_back_tears:

Thank you for sharing that Document, Jason! I’m sure it will be of help to other users facing the same challenge!

2 Likes

Ohh thanks so much for sharing this @jkilp ! It’ll definitely come in handy for others, much appreciated! :smile:

1 Like