简体   繁体   English

样式材质 UI 按钮

[英]Styling Material UI Button

Hi I just started using Material UI and am having a hard time styling the components.嗨,我刚刚开始使用 Material UI,并且很难为组件设计样式。 I am building a sign in page and would like my Submit button to be all the way to the bottom right.我正在构建一个登录页面,并希望我的提交按钮一直位于右下角。 If someone can help me out that would be greatly appreciated because it seems to be inheriting styles from everywhere else but where I would like to!如果有人可以帮助我,那将不胜感激,因为它似乎从其他地方继承了样式,但我想要的地方! I have tried adding我试过添加

textAlign: 'right'

to buttonStyle and that does not work.到 buttonStyle ,这不起作用。 I have also tried adding我也试过添加

text-align: right;

to my .form-button CSS.到我的 .form-b​​utton CSS。 The only thing that affects anything is removing the .App Login.js唯一影响任何事情的是删除 .App Login.js

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

App.css应用程序.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;
}

Another main goal would be to avoid inline styling because I do prefer keeping it within my style sheet.另一个主要目标是避免内联样式,因为我更喜欢将它保留在我的样式表中。 But if not possible or too overly difficult, I will inline style (as I did with the background-color).但如果不可能或过于困难,我将内联样式(就像我对背景颜色所做的那样)。

As keikai has mentioned in the comment, you may check the Documentation in this link material-ui.com/styles/basics for overriding style.正如 keikai 在评论中提到的,您可以查看此链接 material-ui.com/styles/basics 中的文档以了解覆盖样式。

For 'it seems to be inheriting styles from everywhere else'因为“它似乎继承了其他地方的风格”

I will suggest you to use styled-components instead of global css import, which mess up everywhere.我会建议你使用 styled-components 而不是 global css import,它到处都是乱七八糟的。 Try this,尝试这个,

npm install --save styled-components npm install --save styled-components

It creates a css class that only apply to the component.它创建了一个仅适用于组件的 css 类。 Sample code:示例代码:

 import styled from 'styled-components'

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

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

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

  // add more class if you have any

  `

Then wrap your component with然后用你的组件包裹

// your newly created styled div  
<MyDiv>

   // component that needs your style
   <MyComponent />

</MyDiv>

Your style will only be applied to MyDiv and MyComponent, and nothing else.您的样式将仅应用于 MyDiv 和 MyComponent,而不会应用于其他任何东西。 It may took awhile to get used to it, but it is extremely useful.习惯它可能需要一段时间,但它非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM