【问题标题】:Align input with labels to button without label (vertically)将输入与标签对齐到没有标签的按钮(垂直)
【发布时间】:2019-02-16 02:20:52
【问题描述】:

我想对齐输入,它有两个标签和一个没有任何标签的按钮,以便它们都在同一级别。

这是想要的效果:

这是我目前得到的:

我已经设法通过在按钮前添加带有空格的标签来使其看起来像我想要的那样,这并不理想。

代码如下:

<Form>
<Form.Group widths='four'>
    <Form.Field>
        <Form.Input type='number' label='Input'/>
            <label>We can't find any matches.</label>
    </Form.Field>
    <Form.Field>
        <Button primary>Search</Button>
    </Form.Field>
</Form.Group>

感谢您的帮助

编辑:这是 CodeSandbox 的链接:https://codesandbox.io/s/v6kkmyzyr0

【问题讨论】:

标签: reactjs semantic-ui semantic-ui-react


【解决方案1】:

我认为这是一个样式问题,它取决于您使用的设计框架(如果您正在使用一个)。

回答您的问题,您可以使用 flex 水平对齐项目 这是代码:https://codesandbox.io/s/1oro0o6943

import React from "react";
import { render } from "react-dom";
import { Button, Form } from "semantic-ui-react";

const App = () => (
  <Form>
    <Form.Group widths="four" style={{ display:"flex", flexDirection:"row", alignItems:"center",  }}>
      <Form.Field>
        <Form.Input type="number" label="Input" />
        <label>We can't find any matches.</label>
      </Form.Field>
      <Form.Field>
        <Button primary>Search</Button>
      </Form.Field>
    </Form.Group>
  </Form>
);

render(<App />, document.getElementById("root"));

我已经使用内联样式实现了,但您可以通过多种方式进行管理!

【讨论】:

  • 当我删除带有文本“我们找不到任何匹配项”的可选标签时,您知道有什么方法可以使这项工作正常进行吗? ?
  • 这种情况我是这样做的:codesandbox.io/s/1oro0o6943 只需将标签从 Form.Field 中取出,并将其定位为 Form.Group 下的一行。并将样式 alignContents: 更改为 flex-end。要检查其工作方式,请更改代码顶部的标志 noMatches
  • 感谢您的回答。我试图弄清楚如何使这项工作也适用于更多领域。我想我可能会使用网格来布置我想要的所有内容。
  • 是的,试着按行和列来组织一切,行是列的容器,这将更容易处理移动设备中的布局。
  • 我尝试了这种方法,它有点工作。但是按钮和输入没有正确垂直对齐 PS:删除 alignItems:"center" 使它们对齐
【解决方案2】:

尝试使用弹性

   <Form.Group widths='four'>
      <div style="display: flex;  flex-direction:row; height 
         auto;  align-items: 
      center">
           <Form.Field>
               <Form.Input type='number' label='Input'/>
               <label>We can't find any matches.</label>
           </Form.Field>
           <Form.Field>
               <Button primary>Search</Button>
            </Form.Field>
        </div>
    </Form.Group>

【讨论】:

  • 对不起,这似乎将按钮移动到输入字段下方并使所有内容居中。如果您想进行实验,我已在问题中添加了指向 CodeSandbox 的链接。
  • @fuudge 抱歉,我刚刚更新了对align-items: center 的回答以垂直对齐,抱歉 justify- 内容是水平对齐我犯了一个错误
猜你喜欢
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 2012-01-23
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多