简体   繁体   English

在 AntD 中更改 form.item 标签字体大小

[英]Change form.item label font size in AntD

I'm using styled components something like this我正在使用类似这样的样式组件

const FormItem = styled(Form.Item)`
     font-size: 16px;
`;

Using Form Item something like this使用类似这样的表单项

    <FormItem label="System Pressurized">
    {getFieldDecorator('systemPressurized')(
      <Select defaultValue="" placeholder="Select Type">
        <Option value="Yiminghe">yiminghe</Option>
      </Select>,
    )}
  </FormItem>

I've tried changing font size of AntD but it doesn't seem to workout我试过改变 AntD 的字体大小,但它似乎没有锻炼

There are multiple options to override style element.有多个选项可以覆盖样式元素。

  1. You can directly override label element from global css file like您可以直接覆盖全局 css 文件中的标签元素,例如
 label { 
   font-size:16px;
}
  1. Individual element by adding script to label element.通过将脚本添加到标签元素来实现单个元素。
   <FormItem label={ 
<p style={{fontSize:"16px"}}>"System Pressurized"</p>
}>
{getFieldDecorator('systemPressurized')(
<Select defaultValue="" placeholder="Select Type">
   <Option value="Yiminghe">yiminghe</Option>
</Select>
)
}
</FormItem>

you have two way to style the formItem label您有两种设置 formItem 标签样式的方法

//way one :
//You can override the default css by override below selectors
.form-section .form-group label{
  font-size : 20px
  //YOUR CUSTOM STYLE
}
// way two : 
// You can pass custom component like below : 
  <FormItem label={<p style={YOURCUSTOMSTYLEOBJECT}>System Pressurized</p>}>
    {getFieldDecorator('systemPressurized')(
      <Select defaultValue="" placeholder="Select Type">
        <Option value="Yiminghe">yiminghe</Option>
      </Select>,
    )}
  </FormItem>

add css:添加css:

.ant-form-item-label label{
   font-size: 16px;
}

Please override CSS in your code like请在您的代码中覆盖 CSS,例如

$ .ant-form-item-label>label {
     font-size: 16px;
 }
  1. You can directly override the CSS in global.less file like您可以直接覆盖 global.less 文件中的 CSS,例如

     & .ant-form-item-label { & > label { font-size: 16px; } }
  2. You can write JSX for this你可以为此编写 JSX

     <FormItem label = { <p style={{fontSize: "16px"}}>System Pressurized</p>}> {getFieldDecorator('systemPressurized')( <Select defaultValue="" placeholder="Select Type"> <Option value="Yiminghe">yiminghe</Option> </Select>, )}

then use it like this


<!-- language: lang-js -->

    const formItemLayout =
      formLayout === 'horizontal' ?
      {
        labelCol: {
          span: 4,
          style: {
            "text-align": "left",
            "font-size": "12px"
          }
        },
        wrapperCol: {
          span: 30,
        },
      } :
      null;

<!-- end snippet -->





  



  
//then use it like this
<Form
  {...formItemLayout}
  layout={formLayout}
  form={form}
  initialValues={{
      layout: formLayout,
  }}
>
  <Form.Item
    label={"Full Name"}
    name="username"
    id="name"
    style={{ display: "flex", flexDirection: "column" }}
    defaultValue={name}
    onChange={handleChange()}
    rules={[
        {
            required: true,
            message: 'Please input your name!',
        },
    ]}
    >
    <Input />
  </Form.Item>
</Form>

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

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