简体   繁体   English

如何使用引导程序或 css 响应输入字段?

[英]How to make responsive an input field with bootstrap or css?

import axios from "axios"; import React from "react"; import { useAuthState } from "react-firebase-hooks/auth"; import { useForm } from "react-hook-form"; import { toast, ToastContainer } from "react-toastify"; import auth from "../../firebase.init"; const AddInventoryItems = () => { const { register, handleSubmit } = useForm(); const [user] = useAuthState(auth); const onSubmit = (data) => { axios.post("http://localhost:5000/item", data).then((res) => { const { data } = res; if (data) { toast("You have added a new item, Yeah!!!"); } }); }; return ( <div className="row w-25 mx-auto"> <form className="d-flex flex-column my-5 col-sm-12 col-md-6" onSubmit={handleSubmit(onSubmit)} > <input placeholder="Enter Name" className="mb-2" value={user.displayName} type="text" {...register("user name")} required /> <input placeholder="Enter Email" className="mb-2" type="email" value={user.email} {...register("email")} /> <input placeholder="Image Url" className="mb-2" type="text" {...register("image")} /> <input placeholder="Item name" className="mb-2" {...register("name", { required: true, maxLength: 20 })} /> <input placeholder="Item description" className="mb-2" {...register("description")} /> <input placeholder="Item price" className="mb-2" type="number" {...register("price", { min: 18, max: 99 })} /> <input placeholder="Item quantity" className="mb-2" type="number" {...register("quantity", { min: 18, max: 99 })} /> <input placeholder="Supplier name" className="mb-2" {...register("supplier name")} /> <input className="btn btn-outline-primary" type="submit" value="Add new item" /> </form> <ToastContainer /> </div> ); }; export default AddInventoryItems;

To make responsive form elements, you have to wrap all the inputs in form-group class and in their inputs you have to add class form-control要制作响应式表单元素,您必须将所有输入包装在form-group class 中,并且在它们的输入中您必须添加 class form-control

example:例子:

<div class="form-group">
  <label for=""></label>
  <input type="email" class="form-control" />
</div>

take a look on Bootstrap documentation:看看 Bootstrap 文档:

https://getbootstrap.com/docs/5.1/forms/overview/ https://getbootstrap.com/docs/5.1/forms/overview/

Use form-control class in your form input field.在表单输入字段中使用表单控件 class。 So your form field will be responsive.所以你的表单域将是响应式的。

  <form className="d-flex flex-column my-5 col-sm-12 col-md-6 form-control" onSubmit={handleSubmit(onSubmit)}>

You can also see bootstrap official documentation for more detail您还可以查看 bootstrap 官方文档以获取更多详细信息

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

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