Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Button] Text not aligned vertically with icon #19584

Open
NZainchkovskiy opened this issue Feb 6, 2020 · 10 comments
Open

[material-ui][Button] Text not aligned vertically with icon #19584

NZainchkovskiy opened this issue Feb 6, 2020 · 10 comments
Assignees
Labels
component: button This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted

Comments

@NZainchkovskiy
Copy link

When button has an icon, text is not aligned vertically.
You can see it at example page.
image
I made an screenshot and check at graphics designer - button "delete" has 12px from top to text and 14px from text to bottom. While icon has exactly 10px from top and from bottom.

@oliviertassinari

This comment was marked as outdated.

@oliviertassinari oliviertassinari added the component: button This is the name of the generic UI component, not the React module! label Feb 6, 2020
@hmillison
Copy link

Can someone look into this? It feels wrong that the default icons provided with Material UI used with the components aren't aligned properly.

@irfancankalelikargo
Copy link

Still an issue, button with icons looks vertically aligned on Firefox but not in Chrome

@forgo
Copy link

forgo commented Dec 23, 2021

We rely on flexbox vertical alignment. I doubt we can do much about it.

I agree MUI should not be responsible for centering the font itself, as fonts vary wildly in how they handle ascender/descender dimensions; however, there still seems to be something off that I can't perfectly quantify.

A wrapper for the text doesn't actually help much because the height of a button is unconstrained and fits to what you put inside. So, if I change the font-size to 30px in a "small" variant button, all the "small" really relates to is the padding and icon size. This does not actually constrain the height of the button itself. Having a constrained height of the button might make this an easier problem to solve, but I realize that could potentially break a lot of assumptions in a lot of code for a lot of people.

A "small" button with 30px font size:
Screen Shot 2021-12-23 at 4 08 53 PM

I'm more or less just pointing out the issue at hand, and why it's so frustrating.

Screen Shot 2021-12-23 at 3 48 01 PM

When focusing the browser on the text alone, at least some of the misalignment issue is seen to come from the font itself -- as the gap below the text is part of the highlighted area:

Screen Shot 2021-12-23 at 3 49 30 PM

Yet... that's not the whole picture. The icon is not completely center because the SVG path itself may not be centered perfectly in the 24x24 viewbox, even if the SVG conforms to the recommended 24x24 pattern.

Regardless, something seems off in how the icon and text are aligned within the button. They seem more off-center than the offsets of either the SVG or the text would lead you to believe, which makes me think there could be some CSS improvements on the MUI side, regardless.

Screen Shot 2021-12-23 at 3 51 11 PM

I'm honestly not sure how to address this issue without completely re-writing the structure and styles of all the button parts and pieces -- which is not ideal.

Ideally, because there are so many kind of fonts out there, the best thing MUI could do is provide some kind of "verticalOffsetContent" and "verticalOffsetIcon" prop that does not actually affect the size of the button itself, but simply shifts the content of the button. I have not been very successful doing this manually, so I'm not confident it would be an easy fix on the MUI side.

@bradstdev
Copy link

I found this helped in the theme:

      MuiButton: {
        styleOverrides: {
          root: {
            lineHeight: 0
          }
        }
      },

@YakuBrangJa
Copy link

YakuBrangJa commented Jun 25, 2022

Here is my solution
<Button disableElevation variant="contained"> <span>Text</span> <YourIcon /> </Button>

MuiButton: { styleOverrides: { root: { gap: '0.5rem', } } },

@owenashurst
Copy link

It seems like the lineHeight like @brstewart mentioned. Removing this seems to align them perfectly.

@aaron-mota
Copy link

aaron-mota commented Aug 6, 2022

@brstewart answer also overwrites other size buttons (e.g. "medium"), and breaks those buttons aesthetically. Is there a way to apply this to only size="small" buttons?

@DiegoAndai
Copy link
Member

DiegoAndai commented Sep 5, 2024

We have a new report about this bug, now showing in Windows but not macOS: #43545.

I can confirm this is present in the latest versions of v5 and v6, so I'm reopening this one and adding the ready-to-take label.

Finally, I'm adding it to the v7 milestone so we remember to cover it when implementing Material Design 3. This doesn't mean we'll have to wait until v7; hopefully, we'll get a fix sooner. It's only a reminder, so we fix it once and for all by, at most, v7.

The live repro link is still the demo: https://mui.com/material-ui/react-button/#buttons-with-icons-and-label

@DiegoAndai DiegoAndai reopened this Sep 5, 2024
@DiegoAndai DiegoAndai self-assigned this Sep 5, 2024
@DiegoAndai DiegoAndai added this to the Material UI: v7 (draft) milestone Sep 5, 2024
@DiegoAndai DiegoAndai added ready to take Help wanted. Guidance available. There is a high chance the change will be accepted package: material-ui Specific to @mui/material labels Sep 5, 2024
@DiegoAndai DiegoAndai changed the title Text not aligned vertically at button with icon [material-ui][Button] Text not aligned vertically with icon Sep 5, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 5, 2024

It looks very close to #29965 for macOS. Do we consider those duplicates or we use #19584 for Windows?

The descent-override CSS property works on Windows too: #29965 (comment) but it's hard to deploy. A simpler solution seems to be:

diff --git a/packages/mui-material/src/styles/createTypography.js b/packages/mui-material/src/styles/createTypography.js
index 277808d2a7..11210f652f 100644
--- a/packages/mui-material/src/styles/createTypography.js
+++ b/packages/mui-material/src/styles/createTypography.js
@@ -69,7 +69,7 @@ export default function createTypography(palette, typography) {
     subtitle2: buildVariant(fontWeightMedium, 14, 1.57, 0.1),
     body1: buildVariant(fontWeightRegular, 16, 1.5, 0.15),
     body2: buildVariant(fontWeightRegular, 14, 1.43, 0.15),
-    button: buildVariant(fontWeightMedium, 14, 1.75, 0.4, caseAllCaps),
+    button: buildVariant(fontWeightMedium, 14, 1.7, 0.4, caseAllCaps),
     caption: buildVariant(fontWeightRegular, 12, 1.66, 0.4),
     overline: buildVariant(fontWeightRegular, 12, 2.66, 1, caseAllCaps),
     // TODO v6: Remove handling of 'inherit' variant from the theme as it is already handled in Material UI's Typography component. Also, remember to remove the associated types.

which works with the couple of fonts that I could test on Windows and macOS. I have no idea why this works 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: button This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted
Projects
None yet
Development

No branches or pull requests

10 participants