Css 引导:将输入与按钮对齐

Css 引导:将输入与按钮对齐,css,twitter-bootstrap,Css,Twitter Bootstrap,为什么在引导中按钮和输入不能很好地对齐 <input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> <div style="width:8%;float:left">&nbsp;</div> <button class="btn btn-default" type="button">Go!</button> <di

为什么在引导中按钮和输入不能很好地对齐

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
我尝试了一些简单的方法,比如:

<input type="text"/><button class="btn">button</button>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
按钮
该按钮比chrome/firefox中的输入低约
5px

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

Twitter引导程序4
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
在Twitter Bootstrap 4中,可以使用
input group prepend
input group append
类对齐输入和按钮(请参阅)

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
左侧的组按钮(前置) 右侧的“分组”按钮 正如@OleksiyKhilkevich在他的回答中指出的那样,还有第二种方法可以通过使用
.form horizontal
类对齐
输入
按钮

<div class="form-horizontal">
    <input name="search" id="search"/>
    <button class="btn">button</button>
</div>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

按钮
区别 这两个类之间的区别在于
.input append
按钮
放置在
input
元素的上方(因此它们看起来像是附加的),其中
将在它们之间放置一个空格

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
--注--

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

为了允许
输入
按钮
元素相邻而没有间距,在
.input append
类中将
字体大小
设置为
0
(这将删除
内联块
元素之间的白色间距)。如果您想使用
em
%
度量值覆盖默认值,这可能会对
input
元素中的字体大小产生不利影响。

只是提醒一下,对于这种称为
form horizontal

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

input append还有另一个副作用,它会将字体大小降至零。使用.form inline=这将左对齐标签和内联块控件,以实现紧凑布局

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
例如:

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

示例:

引导5

<div class="input-group">
  <input type="text" class="form-control">
  <button class="btn btn-outline-secondary" type="button">Go</button>
</div>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
引导程序3和4

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
您可以使用将按钮直接应用于输入字段

<div class="input-group">
  <input type="text" class="form-control">
  <span class="input-group-btn">
    <button class="btn btn-default" type="button">Go!</button>
  </span>
</div><!-- /input-group -->
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

走!
更多示例请查看或输入组文档

style="padding-top: 8px"
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

使用此选项可将您所在行的div上移或下移。为我创造奇迹。

首先,我尝试了,但最后没有什么变化,我想与大家分享。以下是适用于我的代码(请查找随附的屏幕截图):

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

走!

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
如果希望看到它工作,只需在编辑器中使用以下代码:

<html>
    <head>
        <link type='text/css' rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css' />
    </head>
    <body>
        <div class="container body-content">
            <div class="form-horizontal">
              <div class="input-group">
                  <input type="text" class="form-control" placeholder="Search text">
                  <span class="input-group-btn" style="width:0;">
                      <button class="btn btn-default" type="button">Go!</button>
                  </span>
              </div>
            </div>
        </div>
    </body>
</html>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

走!
希望这有帮助。


<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputEmail3">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
  </div>
  <button type="submit" class="btn btn-default">Sign in</button>
</form>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
电子邮件地址 登录
我尝试了上述所有代码,但都没有解决我的问题。这是对我有用的东西。我使用了输入组插件

<div class = "input-group">
  <span class = "input-group-addon">Go</span>
  <input type = "text" class = "form-control" placeholder="you are the man!">
</div>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

我也在为同样的问题而挣扎。我使用的引导类不适合我。我想出了一个解决办法,如下所示:

<form action='...' method='POST' class='form-group'>
  <div class="form-horizontal">
    <input type="text" name="..." 
        class="form-control" 
        placeholder="Search People..."
        style="width:280px;max-width:280px;display:inline-block"/>

    <button type="submit" 
        class="btn btn-primary" 
        style="margin-left:-8px;margin-top:-2px;min-height:36px;">
      <i class="glyphicon glyphicon-search"></i>
     </button>
  </div>
</form>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

基本上,我重写了类“form control”的显示属性,并使用其他样式属性来更正大小和位置

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
结果如下:

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

没有直接关系,除非您有类似的代码,但我刚刚注意到形成内联引导3.3.7的一个奇怪行为

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
我没有为此使用行和单元格,因为这是一个桌面项目,如果开始标记位于表下方(在本例中):

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

表单元素将堆叠

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
开关和它正确地排列

<form>
<table class="form-inline">
<tr>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

Safari 10.0.2和最新的Chrome没有任何区别。也许我的布局是错误的,但它不是很宽容。

引导程序4:

<div class="input-group">
  <input type="text" class="form-control">
  <div class="input-group-append">
    <button class="btn btn-success" type="button">Button</button>
  </div>
</div>
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

按钮

将输入浮点值设为左侧。然后按下按钮,使其向右浮动。
<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>
当您选择一个以上的距离时,您可以使用clearfix类

<input style="width:65%;float:left"class="btn btn-primary" type="text" name="name"> 
<div style="width:8%;float:left">&nbsp;</div>
<button class="btn btn-default" type="button">Go!</button>
<div class="clearfix" style="margin-bottom:10px"> </div>

走!

谢谢你的回答,但我觉得这里出了点问题。当然,我不是第一个希望引导组件与表单元素对齐的人。@pguardiario-你完全正确。这看起来确实是错误的,在打了我一巴掌之后,我想起了
.input append
类。奇怪的是,你需要一个包装类来完成这项工作。默认情况下,它们不应该排队吗?@opsb如果它们占用相同的垂直空间,那么它们会排队。然而,twitter引导中的输入字段有一个
边距底部:9px
,但是按钮没有,因此它们不会占用相同的垂直空间。由于两个元素都具有中间垂直对齐,因此按钮会因差异而偏移。使用
.input append
包装器删除此页边距底部值。对,我猜如果其他表单布局默认具有相同的垂直高度,它将破坏其他表单布局。+1好点-我必须了解他们为什么使用
字体大小:0
,并学到了一些新东西:)谢谢!其他的不适合我,但是这一个,正是我想要的方式。谢谢。对我来说,如果创建一个额外的span标记来分配类,它不起作用,但是如果我将该类添加到div标记中,它起作用:
这个答案对于BS4来说已经过时了,看看我的答案。我可以确认
表单内联
对于引导3是有效的。谢谢,太棒了!这正是我需要的麻烦的形式!似乎是这样。form inline只能在至少768px宽的视口中工作:这是哪个引导版本?不使用
style=“width:0;”“
”会导致哪个问题?如果不使用width:0,则这将占用整个页面的宽度。引导方式将使用类似
的col元素进行包装,无需设置
宽度:0
,因为我不希望输入控件占用整个屏幕的宽度