13

I am trying to align a button to the most right yet not sucessfull. Here is my attempt.

<Button variant="contained" style={{display: 'flex', justifyContent: 'right'}} color="primary" className="float-right" onClick={this.onSend}>

3
  • Are you using bootstrap? Commented May 18, 2019 at 20:52
  • nop I am using material ui Commented May 18, 2019 at 20:54
  • For some reasons, I am restricted not to use bootstrap Commented May 18, 2019 at 20:54

4 Answers 4

19

You need to add display flex to the parent element of your Button.

See sandbox here for example: https://codesandbox.io/s/testproviderconsumer-klcsr

class App extends React.Component {
  render() {
    return (
      <div style={{ display: "flex" }}>
        <button
          style={{ marginLeft: "auto" }}
        >
          Click
        </button>
      </div>
    );
  }
}

{{display: 'flex', justifyContent: 'flex-end'}} are defined in the parent element to align items in the child element.

Sign up to request clarification or add additional context in comments.

3 Comments

this works but I have two buttons that I would like to align to the left and this is causing on of them to be on top of the other
Is it possible to make both buttons on the same line
Can you post the mark-up for your buttons above? Will give me something to work-off of :)
8

We can also use float property to align.

<Button variant="contained" style={{float: 'right'}} color="primary" className="float-right" onClick={this.onSend}>

Comments

4

You have to defined in the parent element to align items in the child element.

<div style={{ display: 'flex', justifyContent: 'flex-end' }}>
   <button>
          Click here
   </button>
</div>

Comments

1
<Button variant="contained" style={{ marginLeft: "auto" }} color="primary" onClick={this.onSend}>
Click
</Button>

1 Comment

Your answer could be improved with additional supporting information. Please edit your answer to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.