Designing Material UI Button Styles

Hello, I recently started using Material UI and I'm facing some challenges in styling the components. Right now, I'm working on a sign-in page and trying to position the Submit button all the way to the bottom right corner. Any help or advice would be highly appreciated because it seems like the button is inheriting styles from unexpected sources. I've attempted adding

textAlign: 'right'

to buttonStyle but that didn't yield the desired result. I also tried adding

text-align: right;

to my .form-button CSS. The only noticeable change occurred when I removed the .App from Login.js

<div className='form-container'>
  ...
  <Button
    style={buttonStyle}
    className='form-button'
    variant='contained'>
    Log-In
  </Button>
</div>
...
const buttonStyle = {
  backgroundColor: '#527354'
};

In App.css

.App {
  text-align: center;
}

.form-button {
  width: 83px;
  height: 36px;

  box-shadow: 0px 1px 3px #00000033;
}

.MuiButton-label {
  color: var(--primary-white);
  font-family: 'Open Sans', sans-serif;
}

.form-container {
  max-width: 400px;
  margin: 2rem auto;
  overflow: hidden;
  padding: 0 2rem;
}

My main aim is to avoid inline styling as much as possible and keep everything within my style sheet. However, if it's too complicated or not achievable, I'll resort to inline styling (similar to how I did with the background-color).

Answer №1

As previously mentioned by keikai in the comments, you can refer to the Documentation available at material-ui.com/styles/basics for information on how to override styles.

If you're experiencing issues with styles being inherited from multiple sources,

I recommend using styled-components instead of importing global CSS, which can cause conflicts. Here's how you can do it:

First, install styled-components by running

npm install --save styled-components

This will create a CSS class that only applies to the specific component.

import styled from 'styled-components'

const MyDiv = styled.div`// this can be span, section, etc.

// add your style here for the div
your div style (optional)

// CSS class specific to the div
.form-container {
    max-width: 400px;
    margin: 2rem auto;
    overflow: hidden;
    padding: 0 2rem;
}

// add more classes if needed

`

Next, wrap your component with the newly created styled div like so:

// your newly created styled div  
<MyDiv>

   // component that requires styling
   <MyComponent />

</MyDiv>

The styles will now only be applied to MyDiv and MyComponent, ensuring no interference with other elements. It may take some time to adjust, but the benefits are worth it.

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

CSS HTML parent div cannot contain image properly

Creating a website using Bootstrap: <div class="row padding_zero"> <div class="col section px-0"> <img class="img-trans padding_zero" src="./svg/clouds-00.svg" style="margin-top: -150px& ...

Styled-components causing issues with conditional rendering

Within my React component, I have multiple properties and I want styles to only apply if a property has a value. I attempted the following code: export const Text = ({text, color, size, fontFamily}) => { const StyledParagraph = styled.p` m ...

Explore an illustration of a website with a hover effect

As I delve into the world of web development, I have taken up the challenge of replicating another website's layout to hone my skills. However, there is one aspect of this site that has me stumped =/ <div class="a"> <span>Teste</span&g ...

Guide on aligning text beneath and to the side of an image, and positioning them in the same row using CSS Bootstrap 4

I am looking to organize two different texts alongside and under an image within a single article. I also want these two articles to be displayed in the same row when the window size is col-md or larger. https://i.sstatic.net/ZjigH.jpg The second article ...

Having trouble with the <img> tag on CodeSandbox.io? Image not loading properly

I've been attempting to insert an image using the <img> tag on CodeSandbox.io. However, each time I try to do so, it fails to display and defaults to the alt tag (Shows Mountain). The goal is to load an image from the same folder as the file th ...

The withRouter function in React Router does not automatically inject the router

Desiring to implement withRouter on my primary React component named 'App'. You can view the documentation here. This is how I utilize it: import React from "react"; import { render } from "react-dom"; import {Router, Link, hashHistory, Rout ...

Tips for incorporating a transition effect on group hover using Tailwind CSS

I've been working on creating a cool animated transition effect for my button where a chevron icon appears when the user hovers over it. I'm currently developing the website using NextJS and TailwindCSS, and I've implemented a group hover fe ...

The correct way to update component state when handling an onChange event in React using Typescript

How can I update the state for 'selectedValues' in a React component called CheckboxWindow when the onChange() function is triggered by clicking on a checkbox? export const CheckboxWindow: React.FC<Props> = props => { const [selected ...

Tips for updating the hover effect color on Material UI select component options in a React JS application

I've been trying to customize the hover effect for the options in the mui Auto Complete component drop-down menu, but I'm having trouble finding the right method to do so. Here is the hover effect that I want to change: Image I'd like to u ...

Switch up your code and toggle a class on or off for all elements that share a specific class

I've been attempting to create a functionality where, upon clicking a switch, a specific class gets added to every element that is assigned the class "ChangeColors". Unfortunately, I have encountered some difficulties in achieving this task. The error ...

Using Javascript to trigger a setTimeout function after the user's mouse leaves

There is a div that pops up when the user's mouse exits the webpage, containing a survey pertaining to my website. I want to avoid prompting users to take the survey if they move their cursor out of the page within the first few minutes of browsing. ...

Showing undefined or null values in React and JavaScript

My goal is to format undefined or null values by italicizing them. If the value is an empty string, it should be displayed as is. If it has a value, that value should also be displayed as is. However, I am encountering an issue where null or undefined val ...

The theme color in mui is not being implemented as expected

I have been working on a new component and trying to style it using the makeStyles() function. // Welcome.js import { makeStyles } from "@mui/styles"; const useStyles = makeStyles((theme) => { return { root: { width: "100%", maxWidth ...

Setting up ReactJS and TypeScript in a fresh MVC5 project from scratch

After extensively trying out various tutorials, I have yet to successfully run a basic MVC5 project using TypeScript and ReactJS. For reference, I have created these projects from scratch in Visual Studio 2015 with .NET 4.6.1, using the ASP.NET Web Applic ...

What could be causing the spinner (Bootstrap 5.x) to not show up on the screen

I'm having trouble getting a spinner to show up when a user clicks a form submit button. Below is the HTML code to add a <div> for the spinner: <div class="pageloader"> <img src="../images/loader-large.gif" alt ...

In React Native, the concept of "null" does not exist within component state as an

I'm currently working on integrating the react-native-modal-datetime-picker functionality into my application Code Snippet: import React, { Component } from "react"; import { View, ScrollView, DatePickerIOS, TouchableOpa ...

Swiper - tips for managing navigation visibility based on screen size in React

I have implemented a React Typescript swiper slider styled with tailwind CSS. The slider functions correctly, but I am facing an issue when trying to hide and show the navigation buttons on different breakpoints. Initially, I use the 'hidden' cla ...

Unable to locate the iframe in the test scenario

I encountered this particular test case: Select Frame id=coach_frame63454108.cf1 Wait Until Element Is Visible ${ap.gui.header.appname} Page Should Contain Element ${ap.gui.header.appname} Page Should Contain Element ${ap.gui.hea ...

Text inputs are not expanding properly when placed within the same Grid container as a react-select dropdown component

Recently, I encountered an issue that only occurs when using Chrome (Version 76.0.3809.132). The problem arises in a simple dialog box consisting of a material ui (v4.3.3) Grid, several TextFields, and two react-select (v3.0.4) Select boxes. The container ...

Customize scripts dynamically in Next.js based on the current environment during runtime

I need to dynamically insert a script into the <head> of my application, with the src attribute depending on a runtime environment variable from my hosting server (OpenShift). If process.env.ENVIRONMENT === "test", I want to add <script ...