如何减小 flex MUI 网格之间的间隙?

huangapple go评论61阅读模式
英文:

How do I reduce the gap between flex MUI grids?

问题

// Custom TextField to avoid styling repetitions
  width: 650px;
// Custom Typography to avoid styling repetitions
  font-weight: 700;

const asterisk = <span style={{ color: 'red' }}>*</span>;

const MailThread = () => {
  // ...
  <Container maxWidth={false} component="main">
    <CssBaseline />
    <Box component="form" noValidate onSubmit={onSubmit} sx={{ mt: 3, display:'flex' }}>
      <Grid  container spacing={2}>
        <Grid item xs={12}>
          Thread Name{asterisk}
          <Field
            name="threadName"
            required
            value={threadName}
            onChange={onChange}
          />
        </Grid>
        // ...
      </Grid>
      <Grid container spacing={2} sx={{marginLeft:'10px'}}>
        <Grid item xs={12}>
          Template
          <Field placeholder="Enter subject here" onChange={onChange} />
        </Grid>
      </Grid>
    </Box>
  </Container>
};
英文:

I am building a form using MUI. I have 6 fields and want to place them 3 of them on the left and the rest on the right, with a minimal gap. This is what I want to achieve: 如何减小 flex MUI 网格之间的间隙?

This is what I have currently:
如何减小 flex MUI 网格之间的间隙?

So far I have tried the gap property but no luck. I might be using the flex incorretly but again I'm not sure. This is the code I have:

import styled from &quot;@emotion/styled&quot;;
import {
  Box,
  Button,
  Container,
  CssBaseline,
  Grid,
  Link,
  TextField,
  Typography,
} from &quot;@mui/material&quot;;
import React, { useState } from &quot;react&quot;;
const Field = styled(TextField)`
  // Custom TextField to avoid styling repetitions
  width: 650px;
`;
const FieldName = styled(Typography)`
  // Custom Typography to avoid styling repetitions
  font-weight: 700;
`;

const asterisk = &lt;span style={{ color: &quot;red&quot; }}&gt;*&lt;/span&gt;;

const MailThread = () =&gt; {
  const [formData, setFormData] = useState({
    threadName: &quot;&quot;,
    from: &quot;Qmeter or 2354&quot;,
    customerName: &quot;&quot;,
    subject: &quot;&quot;,
    dropdownOption: &quot;QNP-102 Template&quot;,
    to: [],
    startSending: new Date(),
  });

  const {
    threadName,
    from,
    customerName,
    subject,
    dropdownOption,
    to,
    starSending,
  } = formData;

  const onChange = (e) =&gt; {
    setFormData((prevState) =&gt; ({
      ...prevState,
      [e.target.name]: e.target.value,
    }));
  };

  const onSubmit = (e) =&gt; {
    e.preventDefault();
  };

  return (
    &lt;Container maxWidth={false} component=&quot;main&quot;&gt;
      &lt;CssBaseline /&gt;
     
        &lt;Box component=&quot;form&quot; noValidate onSubmit={onSubmit} sx={{ mt: 3, display:&#39;flex&#39; }}&gt;
          &lt;Grid  container spacing={2}&gt;
            &lt;Grid item xs={12}&gt;
              &lt;FieldName&gt;Thread Name{asterisk}&lt;/FieldName&gt;
              &lt;Field
                name=&quot;threadName&quot;
                required
                value={threadName}
                onChange={onChange}
              /&gt;
            &lt;/Grid&gt;

            &lt;Grid item xs={12}&gt;
              &lt;FieldName&gt;From&lt;/FieldName&gt;
              &lt;Field
                sx={{
                  backgroundColor: &quot;#f8f4f4&quot;,
                }}
                disabled
                value={from}
                name=&quot;from&quot;
                onChange={onChange}
              /&gt;
            &lt;/Grid&gt;
            &lt;Grid item xs={12}&gt;
              &lt;FieldName&gt;If Customer name is empty&lt;/FieldName&gt;
              &lt;Field onChange={onChange} /&gt;
            &lt;/Grid&gt;
            &lt;Grid item xs={12}&gt;
              &lt;FieldName&gt;Subject&lt;/FieldName&gt;
              &lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
            &lt;/Grid&gt;
            &lt;Grid item xs={12}&gt;&lt;/Grid&gt;
          &lt;/Grid&gt;
          &lt;Grid container spacing={2} sx={{marginLeft:&#39;10px&#39;}}&gt;
            &lt;Grid item xs={12}&gt;
              &lt;FieldName&gt;Template&lt;/FieldName&gt;
              &lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
            &lt;/Grid&gt;
          &lt;/Grid&gt;
        &lt;/Box&gt;
      
    &lt;/Container&gt;
  );
};

export default MailThread;

答案1

得分: 2

The only thing you need to use gap property is display grid or flex.
After you can apply gap in proper units: px, %, vw, vh...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form action=&quot;&quot; style=&quot;display: flex; gap: 20px&quot;&gt;
  &lt;div style=&quot;display: flex; flex-direction: column; gap: 10px&quot;&gt;
    Name: &lt;input/&gt;
    Surname: &lt;input/&gt;
    Age: &lt;input/&gt;
  &lt;/div&gt;
  &lt;div style=&quot;display: flex; flex-direction: column; gap: 10px; align-items: start&quot;&gt;
    Addres: &lt;input/&gt;
    Email: &lt;input/&gt;
    Whatever: &lt;input/&gt;
    &lt;p style=&quot;margin: 0&quot;&gt;Human? &lt;input type=&quot;radio&quot; checked/&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

You can also use margin-right (apply on the left block).

英文:

The only thing you need to use gap property is display grid or flex.
After you can apply gap in proper units: px, %,vw,vh...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form action=&quot;&quot; style=&quot;display: flex; gap: 20px&quot;&gt;
  &lt;div style=&quot;display: flex; flex-direction: column; gap: 10px&quot;&gt;
    Name: &lt;input/&gt;
    Surname: &lt;input/&gt;
    Age: &lt;input/&gt;
  &lt;/div&gt;
  &lt;div style=&quot;display: flex; flex-direction: column; gap: 10px; align-items: start&quot;&gt;
    Addres: &lt;input/&gt;
    Email: &lt;input/&gt;
    Whatever: &lt;input/&gt;
    &lt;p style=&quot;margin: 0&quot;&gt;Human? &lt;input type=&quot;radio&quot; checked/&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

You can also use margin-right(apply on the left block).

答案2

得分: 1

我个人不喜欢在网格组件中设置容器和间距,因为它会通过边距调整布局。

请检查以下代码,它使用弹性布局和间距设置布局。

如果你不想使用网格组件的属性,可以用盒子组件替换它。

祝好运!
英文:

I personally don't like the idea of setting container and spacing in the Grid component because it adjusts the layout with margins.

Please check the following code, which sets up a layout using flex and grap.

If you don't want to use the properties of the Gird component, you may replace it with the Box component.

Good luck!

return (
    &lt;Container maxWidth={false} component=&quot;main&quot;&gt;
      &lt;CssBaseline /&gt;

      &lt;Box component=&quot;form&quot; noValidate onSubmit={onSubmit} sx={{ mt: 3, display: &#39;flex&#39;, gap: 4 }}&gt;
        &lt;Grid sx={{ display: &#39;flex&#39;, flexDirection: &#39;column&#39;, gap: 4 }}&gt;
          &lt;Grid item xs={12}&gt;
            &lt;FieldName&gt;Thread Name{asterisk}&lt;/FieldName&gt;
            &lt;Field name=&quot;threadName&quot; required value={threadName} onChange={onChange} /&gt;
          &lt;/Grid&gt;

          &lt;Grid item xs={12}&gt;
            &lt;FieldName&gt;From&lt;/FieldName&gt;
            &lt;Field
              sx={{
                backgroundColor: &#39;#f8f4f4&#39;,
              }}
              disabled
              value={from}
              name=&quot;from&quot;
              onChange={onChange}
            /&gt;
          &lt;/Grid&gt;
          &lt;Grid item xs={12}&gt;
            &lt;FieldName&gt;If Customer name is empty&lt;/FieldName&gt;
            &lt;Field onChange={onChange} /&gt;
          &lt;/Grid&gt;
          &lt;Grid item xs={12}&gt;
            &lt;FieldName&gt;Subject&lt;/FieldName&gt;
            &lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
          &lt;/Grid&gt;
          &lt;Grid item xs={12}&gt;&lt;/Grid&gt;
        &lt;/Grid&gt;

        &lt;Grid&gt;
          &lt;Grid item xs={12}&gt;
            &lt;FieldName&gt;Template&lt;/FieldName&gt;
            &lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
          &lt;/Grid&gt;
        &lt;/Grid&gt;
      &lt;/Box&gt;
    &lt;/Container&gt;
  )

答案3

得分: 1

I just updated your code. here you can see the live example

请也阅读此文档 Grid

import styled from "@emotion/styled";
import {
  Box,
  Button,
  Container,
  CssBaseline,
  Grid,
  Link,
  TextField,
  Typography,
} from "@mui/material";
import React, { useState } from "react";
const Field = styled(TextField)`
  // 自定义的 TextField,避免样式重复
  width: 100%;
`;
const FieldName = styled(Typography)`
  // 自定义的 Typography,避免样式重复
  font-weight: 700;
`;

const asterisk = <span style={{ color: "red" }}>*</span>;

const MailThread = () => {
  const [formData, setFormData] = useState({
    threadName: "",
    from: "Qmeter or 2354",
    customerName: "",
    subject: "",
    dropdownOption: "QNP-102 Template",
    to: [],
    startSending: new Date(),
  });

  const {
    threadName,
    from,
    customerName,
    subject,
    dropdownOption,
    to,
    starSending,
  } = formData;

  const onChange = (e) => {
    setFormData((prevState) => ({
      ...prevState,
      [e.target.name]: e.target.value,
    }));
  };

  const onSubmit = (e) => {
    e.preventDefault();
  };

  return (
    <Container maxWidth={false} component="main">
      <CssBaseline />
     
        <Box component="form" noValidate onSubmit={onSubmit}>
          <Grid  container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
            <Grid item xs={6}>
              <FieldName>Thread Name{asterisk}</FieldName>
              <Field
                name="threadName"
                required
                value={threadName}
                onChange={onChange}
              />
            </Grid>

            <Grid item xs={6}>
              <FieldName>From</FieldName>
              <Field
                sx={{
                  backgroundColor: "#f8f4f4",
                }}
                disabled
                value={from}
                name="from"
                onChange={onChange}
              />
            </Grid>
            <Grid item xs={6}>
              <FieldName>If Customer name is empty</FieldName>
              <Field onChange={onChange} />
            </Grid>
            <Grid item xs={6}>
              <FieldName>Subject</FieldName>
              <Field placeholder="Enter subject here" onChange={onChange} />
            </Grid>
            <Grid item xs={6}>
              <FieldName>Template</FieldName>
              <Field placeholder="Enter subject here" onChange={onChange} />
            </Grid>
            <Grid item xs={6}>
              <FieldName>Start Sending</FieldName>
              <Field placeholder="Enter subject here" onChange={onChange} />
            </Grid>
          </Grid>
        </Box>
      
    </Container>
  );
};

export default MailThread;
英文:

I just updated your code. here you can see the live example

please also read this documentation Grid

import styled from &quot;@emotion/styled&quot;;
import {
Box,
Button,
Container,
CssBaseline,
Grid,
Link,
TextField,
Typography,
} from &quot;@mui/material&quot;;
import React, { useState } from &quot;react&quot;;
const Field = styled(TextField)`
// Custom TextField to avoid styling repetitions
width: 100%;
`;
const FieldName = styled(Typography)`
// Custom Typography to avoid styling repetitions
font-weight: 700;
`;
const asterisk = &lt;span style={{ color: &quot;red&quot; }}&gt;*&lt;/span&gt;;
const MailThread = () =&gt; {
const [formData, setFormData] = useState({
threadName: &quot;&quot;,
from: &quot;Qmeter or 2354&quot;,
customerName: &quot;&quot;,
subject: &quot;&quot;,
dropdownOption: &quot;QNP-102 Template&quot;,
to: [],
startSending: new Date(),
});
const {
threadName,
from,
customerName,
subject,
dropdownOption,
to,
starSending,
} = formData;
const onChange = (e) =&gt; {
setFormData((prevState) =&gt; ({
...prevState,
[e.target.name]: e.target.value,
}));
};
const onSubmit = (e) =&gt; {
e.preventDefault();
};
return (
&lt;Container maxWidth={false} component=&quot;main&quot;&gt;
&lt;CssBaseline /&gt;
&lt;Box component=&quot;form&quot; noValidate onSubmit={onSubmit}&gt;
&lt;Grid  container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;Thread Name{asterisk}&lt;/FieldName&gt;
&lt;Field
name=&quot;threadName&quot;
required
value={threadName}
onChange={onChange}
/&gt;
&lt;/Grid&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;From&lt;/FieldName&gt;
&lt;Field
sx={{
backgroundColor: &quot;#f8f4f4&quot;,
}}
disabled
value={from}
name=&quot;from&quot;
onChange={onChange}
/&gt;
&lt;/Grid&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;If Customer name is empty&lt;/FieldName&gt;
&lt;Field onChange={onChange} /&gt;
&lt;/Grid&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;Subject&lt;/FieldName&gt;
&lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
&lt;/Grid&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;Template&lt;/FieldName&gt;
&lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
&lt;/Grid&gt;
&lt;Grid item xs={6}&gt;
&lt;FieldName&gt;Start Sending&lt;/FieldName&gt;
&lt;Field placeholder=&quot;Enter subject here&quot; onChange={onChange} /&gt;
&lt;/Grid&gt;
&lt;/Grid&gt;
&lt;/Box&gt;
&lt;/Container&gt;
);
};
export default MailThread;

huangapple
  • 本文由 发表于 2023年4月19日 17:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定