Alignment of Material-UI dialogue buttons on the left side

I have a Dialog containing three buttons as shown below:

https://i.stack.imgur.com/T6o35.png

Here is the JSX code:

        <DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} >
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()} >
            Clear
          </Button>
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()} >
            Cancel
          </Button>
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose(this.state.selectedValue)} >
            Select
          </Button>
        </DialogActions>

The 'buttonRoot' style controls the button coloring. How can I align the 'Clear' button to the left within the dialog? It appears that each button is contained within a div with a MuiDialogActions-action class.

Answer №1

To create a divider element with the CSS style flex: 1 0 0, simply follow this code:

<DialogActions>
  <Button>
    Left
  </Button>
  <Button>
    Buttons
  </Button>
  <div style={{flex: '1 0 0'}} />
  <Button>
    Right
  </Button>
  <Button>
    Buttons
  </Button>
</DialogActions>

Answer №2

Simplify your solutions:


Just adjust the style property of the DialogActions component to

{justifyContent: "space-between"}

<DialogActions style={{ justifyContent: "space-between" }}>
  <Button>
    Left
  </Button>
  <Button>
    Right
  </Button>
</DialogActions>

Answer №3

If you want to achieve this using Flexbox:

DialogActions {
  display: flex; /* arrange children inline with Flexbox */
}

DialogActions > Button:first-child {
  margin-right: auto;
}
<DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} >
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()}>
    Clear
  </Button>
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()}>
    Cancel
  </Button>
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose(this.state.selectedValue)}>
    Select
  </Button>
</DialogActions>

Note: For a better view, check it in full screen mode.

Answer №4

Note: The response provided does not directly address the original question. It appears that all buttons will be aligned to the left based on this code snippet.

The DialogActions API specifies that the classes you have the option to customize are root and spacing.

Upon reviewing the implementation details of the component, it seems that the root class includes a rule for justifyContent set to 'flex-end'.

To achieve alignment to the left, I added the style justifyContent: 'flex-start' using another class:

<DialogActions classes={{ root: classes.leftAlignDialogActions }}>
   ....
</DialogActions>

Ensure that you define the class leftAlignDialogActions with makeStyles like so:

const useStyles = makeStyles(theme => ({
leftAlignDialogActions: {
    justifyContent: 'flex-start'
  }
}))

Answer №5

To achieve my desired layout, I first made sure to disableSpacing and then applied the necessary flex attributes and MUI style overrides using the sx prop. Specifically, I needed to adjust the spacing (gap) from 8px to 10px.

<DialogActions disableSpacing sx={{gap: '10px'}}>
  <Button>Cancel</Button>
  <Button>Submit</Button>
</DialogActions>

If you encounter a similar situation, consider enclosing the 2nd and 3rd buttons within a Box component and setting justifyContent: space-between on the container. This arrangement will ensure that the two flex items within the container are aligned to the left and right edges respectively. The first button will be positioned on the left, while the Box containing the 2nd and 3rd buttons will be on the right side.

Answer №6

If you want to align the first button in the 'DialogActions' tag to the left, you can use the first-child selector.

DialogActions button:first-child {
  float:left;
}

Alternatively, you can adjust the positioning by setting the position, left, and bottom properties.

DialogActions{
  position:relative;
  width:100%;
}
DialogActions button:first-child {
  position:absolute;
  left:10px;
  bottom:10px;
}

I hope this information is helpful for you!

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

Clickable functionality disabled for form elements

I am encountering an issue with my system development task. The form elements appear to be unclickable, preventing any data entry in the fields. I have attempted moving the form tag above the first div in the code structure below as a troubleshooting step, ...

Disabling keypress function in onKeyPress, yet onChange event still activates

In my ReactJS component, I have implemented a function that is triggered by the onKeyPress event: onKeyPress(e) { if (!isNumeric(e.key) && e.key !== '.') { return false; } } Although this function successfully prevents non-numer ...

Integrating Facebook login with Cordova using the cordovaOauth plugin

Encountering issues while setting up FB login for my cordova mobile app. A tutorial followed: http://www.codeproject.com/Tips/1031475/How-to-Integrate-Facebook-Login-into-a-Cordova-App#_comments <script src="js/angular.js"></script> <scrip ...

The style of MUI Cards is not displaying properly

I've imported the Card component from MUI, but it seems to lack any styling. import * as React from "react"; import Box from "@mui/material/Box"; import Card from "@mui/material/Card"; import CardActions from "@mui/m ...

Populating a span tag with data retrieved through Ajax

I encountered an issue with updating the totalDraftSalePrice HTML tag after a successful AJAX call. The data returned includes a field called SubtotalBasePrice, which I can visualize in JSON format, but for some reason, I am unable to update the HTML tag w ...

Trigger event when user ceases to click

I have successfully implemented a click event using jQuery. Here is the code: $('#myButton').click(function(){ // perform desired actions }); However, I am facing an issue where multiple intermediate events are triggered if the user clicks on ...

Exploring the Crossroads of JSP and External Javascript Documents

I am new to using external JavaScript files (*.js). I have my JSP file ready, but my manager wants me to incorporate graphics into it. After searching, I found some *.js files. However, I am unsure of how to connect them with my JSP page. I need a way to ...

Building a Skybox Material using Shader Graph in Unity3D

I have successfully created a material using ShaderGraph and I am trying to assign it as a skybox material. However, I am facing an issue where when I assign it to the environment -> skybox material in the light settings, it only gets assigned to one si ...

Verifying whether every value in the X array is present in the Y array or not

Currently, I am working with two arrays: const arrA = [1, 2, 3, 4, 5]; const arrB = [1, 2, 3, 4, 5, 6, 7, 8, 9]; My goal is to determine if all elements in array A exist in array B. Here are a few scenarios for better clarity: const arrA = [1, 2, 3, 4, ...

Replace the single text area with multiple div elements and update the image within each div

Within the 'Pinctitle' area, there is text that I want to change each time a Pselectorbutton is hovered over with a fading effect. Additionally, I would like the image inside the 'Pselectorbutton' to change at the same time. Currently, ...

Unexpected next() error occurred

Currently, I am working on a project using node.js along with express and MongoDB. I have encountered an error that I cannot seem to understand. When I remove the next() function call, everything works perfectly fine. However, when I include next(), it tr ...

Is there a way to programmatically create multiple MUI Select components with their corresponding options?

Through processing existing data, I am dynamically generating multiple MUI "<Select>" elements. However, when interacting with one of the "<Select>" elements, the selected value is being applied to all other "<Select>" elements. For inst ...

Generating dynamic input field values using jQuery in a CodeIgniter PHP framework application

I am facing an issue where I am unable to display the value from a dynamically created input field in the page controller. Below is the jQuery code used to append the dynamic input fields: var tableRow='<tr>'; tableRow+=' ...

Using TypedArray as a parameter in a Cordova plugin

I've been working on a plugin specifically for Cordova 3.3. Within my JavaScript code, I'm in the process of downloading a PDF file which is being stored as a uInt8Array. download: function ( ) { var xhr = new XMLHttpRequest(); ...

Splicing using only one parameter will make changes to the array without deleting the entire array

let myArray = ['a','b','c','d','e']; console.log(myArray.splice(1)); console.log(myArray); Looking at the splice documentation, it mentions that not providing a delete parameter would remove all array item ...

What's causing the problem with my custom filter?

I've encountered an issue with a custom filter I'm currently working on. The challenge lies in the fact that I'm using Angular 1.3.6 and am unable to upgrade at the moment. Therefore, I truly need your assistance with this matter. Check out ...

Material UI: Easily adjusting font size within Lists

When developing forms with react js and material UI, I encountered an issue with changing the font size within lists to achieve a more compact layout. The code fontSize={10} didn't seem to have any effect regardless of where I added it. Is there a wa ...

Encountering difficulties accessing the array in the controller through ajax

Having trouble receiving an array of data from AJAX to the controller. $.ajax({ type: "POST", url: "/Home/List", traditional: true, contentType: 'application/json', data: { "Query&quo ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

Using Python's Beautiful Soup library to retrieve text from h1 and id tags

I've been attempting to retrieve the text from an HTML id called "itemSummaryPrice," but I'm having trouble figuring it out. html = "" <div id="itemSummaryContainer" class="content"> <div id=&quo ...