简体   繁体   English

在顶部材质 UI 上垂直对齐文本<Textfield />

[英]Align Text Vertically on top material UI <Textfield />

I have this component我有这个组件

const styles = theme => ({
  height: {
    height: '20rem',
  },
});

class Foo extends React.component { 
    ...

    <TextField
      InputProps={{ classes: { root: this.props.classes.height } }}
      label="Kittens"
      placeholder="Meooow"
      fullWidth={true}
      margin="normal"
      variant="outlined"
      notched={true}
      value={this.state.kittyCat}
      onChange={event => this.onChangeKittens(event)}
      multiline={true} />
}

The height is applied, making the <TextField /> bigger.应用高度,使<TextField />更大。 However, I want to align the text vertically in the <TextField /> .但是,我想在<TextField />垂直对齐文本。

How do I do this?我该怎么做呢?

You can align the text vertically using flex, like:您可以使用 flex 垂直对齐文本,例如:

display:flex;
flex-direction: column;
justify-content: center;

However, Material-UI TextField create a div container, which has two inner elements - another div and a label .然而,Material-UI TextField 创建了一个div容器,它有两个内部元素——另一个div和一个label

The label part has absolute position by default (For the animated label effect), which might make it difficult to achieve what you want without some hairy css overrides. label部分默认具有absolute位置(对于动画标签效果),如果没有一些毛茸茸的 css 覆盖,这可能很难实现您想要的。

A better approach might be to set the styles on the TextField container.更好的方法可能是在TextField容器上设置样式。 You can do this using the material-style system as in your example code:您可以使用示例代码中的材料样式系统执行此操作:

const styles = theme => ({
  container: {
    height: '20rem',
    display: 'flex',
    flexDirection: 'column',
    justifyContent: 'center',
  },
});

class Foo extends React.component { 
    ...
    <form className={classes.container}>
       <TextField
         ... />
    </form>
}

Update: I've added a Demo on sandbox .更新:我在 sandbox 上添加了一个演示 Update: Fixed brackets更新:固定括号

编辑 Material-UI TextField 垂直对齐

Check this out for more about flexbox查看以了解有关 flexbox 的更多信息

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

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