Stop the button from spanning the entire width of its container

Utilizing the Material UI button in my application with customizations as styled-components, I encountered an issue where setting the size="small" prop on the button caused it to only take up the width of the button text with some padding. This behavior persisted even within a Flexbox container I created. However, specifying a width for the FlexContainer component resulted in the small button expanding to 100% of that container's width.

I prefer not to set a default width on the button as it should adjust its size based on the button text. I attempted to apply "display: inline-flex" directly on the button, but saw no change in behavior.

Below is a snippet of the container component:

       <RightFields flexDirection="column">
          <FieldSpacing>
            <PasswordDisplay />
          </FieldSpacing>
          <FieldSpacing>
            <PaymentDisplay />
          </FieldSpacing>
          <Button pink
            buttonText="Save Changes"
            buttonSize="small"
          />
        </RightFields>


...

const RightFields = styled(FlexContainer)`
  width: 321px;
`

Button.js

const Button = ({buttonText, buttonSize, ...props}) => {

  return (
    <>
      {buttonSize === 'large' ?
        <LargeButton
          variant="contained"
          fullWidth={true}
          size="large"
          {...props}
        >
          {buttonText}
        </LargeButton> :
        <SmallButton
          variant="contained"
          size="small"
          {...props}
        >
          {buttonText}
        </SmallButton>
      }
    </>
  )

}

const StyledButton = styled(MaterialButton)`
  ...
  `

const LargeButton = styled(StyledButton)`
  ...
`

const SmallButton = styled(StyledButton)`
  display: inline-flex;
  flex-grow: 0;
  font-size: 15px;
  font-weight: 500;
  line-height: 18px;
  border-radius: 16px;
  height: 28px;
  min-width: 50px;
  padding: 0 16px;
`

Answer №1

If you want the text to wrap, try using width: auto;

const SmallButton = styled(StyledButton)`
  display: inline-flex;
  flex-grow: 0;
  font-size: 15px;
  font-weight: 500;
  line-height: 18px;
  border-radius: 16px;
  height: 28px;
  min-width: 50px;
  width: auto;
  padding: 0 16px;
`

This CSS should ensure that the text wraps accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for creating a fixed position div that remains until a certain point?

Is there a way to prevent the position: fixed; property from being applied after a certain point on the screen scroll using only CSS? ...

What specific authentication process does react-aad-msal employ?

I'm curious about the authentication method employed by the react-aad-msal library. Do you know if it utilizes implicit flow? Link to react-aad-msal ...

Ways to dynamically eliminate focus around text inputs

I have created an HTML page and here is the link to it: My main goal is to remove the black border around the text input on this page. The challenge I am facing is that the 'things to do' list is generated dynamically, meaning it is empty when t ...

Comparing React's Styled-components, JSS, and Emotion: Which is

As a CTO, I am faced with the decision of choosing between three popular CSS-in-JS libraries: jss emotion styled-component. I will keep this question focused and avoid straying into subjective territory. If necessary, I will answer it myself by asking sp ...

Empowering Components with React Hooks

I am currently in the process of transitioning from using class components to React hooks with the Context API. However, I am encountering an error and struggling to pinpoint the exact reason for it. Here are my Codes: // contexts/sample.jsx import React ...

What is causing my React/Webpack application to refresh every time I click on a link?

I'm currently working on a react application using webpack to build it in separate chunks. The app is being served through an express server. My goal is to dynamically add a script tag to the index.html file when a certain link is clicked, without hav ...

Show an empty table/data grid with blank rows using Material UI

I am attempting to showcase a table with empty lines and a predefined height, even when there is no data to display initially. By default, the table appears without any visible lines. My goal is to have these empty lines displayed, ensuring that the table ...

How does a database contribute to the functionality of a redux application?

Recently, I started exploring redux and finally understood the architecture behind it. In a single page redux application where all data is stored in a central store and updated through frontend actions, what purpose does the back-end and database serve? ...

Optimal placement and size for the slick slider

I am new to CSS and currently experimenting with the Slick slider on a project: My setup involves a div container that spans 100% of the width of the page. Inside this container, there is another div (housing the slider) that takes up 80% of the width. D ...

Tips for transferring row data to Toolbar using Material UI React with TypeScript

As I work on integrating actions after selecting items in a GridHeader, I came across a helpful tutorial that demonstrates how to combine these functionalities seamlessly: https://mui.com/x/react-data-grid/selection/#controlled-selection Moreover, for man ...

What is the proper way to structure a React component class without any props?

When working with Typescript in a React project, the top level component typically does not receive any props. What is the recommended approach for typing this scenario? I have been using the following coding structure as a template, but I am curious if t ...

Integration of Unlayer EmailEditor into React causes fatal errors in the application

I am attempting to integrate Unlayer into my React Application by using this guide: https://github.com/unlayer/react-email-editor. I have configured Webpack for this purpose. However, when I try to import EmailEditor in one of my modules: import React fr ...

Having trouble retrieving all users from the MySQL database users table using Express in ReactJS

Utilizing react-material-dashboard to display Admin statistics. I am aiming to present all users in a table within the admin dashboard. Fetching users from the database using Express results in a GET http://localhost:3001/api/fetchUsers 404 (Not Found) er ...

Inexplicably bizarre HTML glitch

I am currently utilizing a comment system that involves adding a new row to a SQL database. The system seems to be working fine; however, when I try to display the comments, the formatting of the comment div becomes all jumbled up. You can view the page wh ...

When I hit the back button in my browser, it unexpectedly resets my progress instead of navigating back

As part of my React website, I have implemented a video carousel that allows users to navigate through different videos using "Next" and "Prev" buttons. The current video's URL is stored in the state, so when the user clicks on either button, the stat ...

The MUI-X data grid is mistakenly exporting blank rows instead of the actual content

After clicking on the export button, it seems that some rows are missing in the CSV file that gets exported. An example of the content displayed in the DataGrid can be seen here: You can view the exported CSV file by following this link: Upon inspection ...

Implementing a backdrop while content is floating

I am facing an issue with a div that has 2 columns. The height of the left column is dynamically changing, whereas the right column always remains fixed in height. To achieve this, I have used the float property for both column divs within the container di ...

Having trouble displaying one DIV above another DIV

I'm having trouble getting an image in a DIV to display above text in another DIV. I can't figure out why it's only displaying below. <div> <div style='z-index: 99; width: 160px; height: 120px; box-shadow: 0px 0px 16px 0px r ...

Prevent row stretching in React.js Material-ui DataGrid

Does anyone know if it is feasible to adjust the width of a row? I have rows containing JSON data that would be best displayed within a <pre> tag for easy viewing by users. However, I am facing challenges with this and wondering if there is a way to ...

Unleash the full potential of React and material-ui with the Raised Button feature - find out how to effortlessly keep all

This snippet showcases the code for my custom Buttons component: import React from 'react'; import RaisedButton from 'material-ui/RaisedButton'; const style = { button: { margin: 2, padding: 0, minWidth: 1, }, }; cons ...