Buttons styled with Material-UI positioned to the right

Is there a way to align buttons on the right using Material-UI's makeStyles function? I have tried using CSS properties like margin-right: 0, but encountered errors when using '-' with makeStyles. I even tried renaming it as 'marginRight' without success. Additionally, using properties like mr: 0 is not valid (Using Material-UI's spacing).

The goal is to create a UI layout similar to stackOverflow's title layout.

import React from 'react';
import { makeStyles } from "@material-ui/core/styles";
import { Box, Button } from "@material-ui/core";

const useStyles = makeStyles({
  titleItemRight: {
    color: 'white',
    backgroundColor: 'blue',
    top: '50%',
    height: 30,
    textAlign: 'right',
    position: 'relative',
    transform: 'translateY(-50%)',
  }
});

const App = () => {
  const classes = useStyles();

  return (
    <div>
      <Box className={classes.titleBar}>
        <Button variant='text' className={classes.titleItemRight}>Sign In</Button>
      </Box>
    </div>
  );
};

Answer №1

Modify,

align: 'right'

To,

float: 'right'

Replace the code with the following,

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import { Box, Button } from "@material-ui/core";

const style = makeStyles({
  titleItemRight: {
    color: "white",
    backgroundColor: "blue",
    top: "50%",
    height: 30,
    float: "right",
    position: "relative",
    transform: "translateY(-50%)"
  }
});

const App = () => {
  const classes = style();

  return (
    <div>
      <Box className={classes.titleBar}>
        <Button variant="text" className={classes.titleItemRight}>
          Sign In
        </Button>
      </Box>
    </div>
  );
};

Check out the live example on CodeSandbox

Answer №2

If you're looking for a solution, I recommend utilizing a flexbox or the AppBar component provided by material ui

https://material-ui.com/components/app-bar/#app-bar

If you prefer using Box, adjust the titleBar styles accordingly and include a spacer element to separate elements to the far right or far left

const style = makeStyles({
  titleBar: {
    display: 'flex',
    width:'100%',
    flexFlow: 'row',
  },
  spacer: {
   flex: '1 1 auto'
  }
});

Then implement your component like this:

  <Box className={classes.titleBar}>
    <LogoHere/>
    <div className={classes.spacer}/>
    <Button variant="text">
      Sign In
    </Button>
  </Box>

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

Changing the visibility of a div with an onClick event in React

Can anyone help me figure out how to toggle the display of this div on click? I've been struggling with it for a while now. Any suggestions are welcome. https://i.sstatic.net/lPZtN.png https://i.sstatic.net/8Pybg.png ...

Reveal the Content-Disposition header in NextJS

I am attempting to retrieve the Content-Disposition header from a response received via axios from an external API. Even though I can see the header in Chrome DevTools Network Response, I am unable to access it from the server. I came across this article ...

What is the best way to align my Dropdown text to the left?

Is there a way to align my Dropdown and parent submenu text to the left, while keeping my tab text always justified to the right? Below is the code snippet: <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ ...

React form submission not triggering Express endpoint for sending emails (Nodemailer)

I'm dealing with a form that submits data such as name, email, and text. Within the onSubmit function of my React component: onSubmit = e => { const { name, email, text } = this.state; axios.post('/feedback', { name, email, ...

Establishing a connection to an Oracle database using ReactJS

I am currently working on a database project that involves using React.js for the frontend and Oracle database for the backend connection. While there are numerous resources available for connecting React with MySQL, I have struggled to find information ...

State in React is not always defined when making a fetch request

Just started using React and encountered an issue while working on my fetch request: I am receiving a formatted response with Axios, but when I try to set the state using the same variable like this: setSelectedStatParams(formattedResponse), it shows up as ...

The images on iPad2's Safari browser are appearing blurry due to low-quality

I'm encountering some challenges trying to display a large jpg file properly on an iPad 2. The image is 9577x600 pixels and is being used as a background image on a div. I have specified -webkit-background-size on that div and set it to the same dimen ...

What is the best way to align a circular image using CSS?

Having an issue with positioning a rounded image to the left of a webpage. Despite trying various methods, the image ends up flat with its corners missing and positioned on the right side instead of the intended left. I've scoured the internet for ans ...

What is the best way to show instructions immediately upon receipt of a response?

I'm currently working on developing a website similar to ChatGpt using the OpenAI API for GPT. However, there is a delay in generating responses with GPT taking a few seconds. As a result, my code only displays the instruction once the response from G ...

Unable to adjust image opacity using jQuery

I am attempting to change the opacity of an image based on a boolean flag. The image should have reduced opacity when the var pauseDisabled = true, and return to full opacity when pauseDisabled = false. To demonstrate this, I have created a fiddle below. ...

Is it possible to create a button that can bring in a .css file?

Is there a way to create a button that imports styles from a .css file, or is it possible to change multiple CSS properties with buttons to create different website themes? This is the CSS file I have: .body { background-image: url("example.png"); } ...

Positioning items in a CSS grid layout involves ensuring that item 1 remains at the bottom of its column, while item 2 is centered within its column

I am trying to create a grid layout using CSS grid: The main section should have two columns. The first column will contain an image that sticks to the bottom of the grid, while the second column will display a centered card. Below are my HTML/CSS codes: ...

inconsistencies observed in flex layout between webkit browsers (Chrome and Safari) compared to moz (Firefox)

I am experiencing unexpected behavior with flex on Mozilla (-moz-) and Chrome/Safari (-webkit-) Referenced the Mozilla tutorial to understand flex layout /** { border: solid; border-width: 0 1px; }*/ html, body { width: 100%; height: 1 ...

Struggling to align menu items horizontally with CSS? Using the inline property just isn't cutting it

I'm struggling to get my menu to display horizontally, no matter what I try. What am I doing wrong? I think the issue might be with the inline option, but I'm not certain. If someone could help correct my code, I would be very grateful. Thank you ...

The date-picker element cannot be selected by html2canvas

I'm encountering an issue with Html2canvas while trying to capture a screenshot of my page. Specifically, the date shown in the date-picker on the page is not appearing in the screenshot. Do you have any insights into why this might be happening and h ...

word-wrap: break-word; repair or substitute

Is there a way to prevent line breaks before words that are going to break anyway when using word-wrap: break-word;? It seems unnecessary and unattractive. Any suggestions? For Example: Consider a div with word-wrap: break-word;, set at a width equal to ...

Obtain WordPress image using its name

I am interested in creating a personalized background image that changes daily, with a URL linking back to the original source. For instance: img name 346.jpg $dayofyear = date('z'); $dayofyear = Is there a way to retrieve an image by name in W ...

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 ...

Issue with React iframe YouTube: Object.defineProperty being invoked on a non-object

I successfully created a component for embedding YouTube videos in my React app and it's working perfectly. My implementation involves using an iframe. <div className="embed-responsive embed-responsive-16by9"> <iframe className="embed-res ...

React Native Bottom tab bar glitching with a sudden jump on every refresh

i am experiencing a similar issue as described in this question: React Native header / bottom tabbar jumping on first app load Every time the app loads, the bottom bar jumps up and down inexplicably. I have tried wrapping the root app in SafeAreaProvider ...