【问题标题】:How to add button inside input [closed]如何在输入中添加按钮[关闭]
【发布时间】:2013-02-25 04:51:24
【问题描述】:

我正在努力实现这一目标:

我只想在输入元素中显示一个按钮,就像上图一样

【问题讨论】:

  • 你用css应该可以做到这一点,你试过什么?
  • 我们不能自动重新打开有 X 个赞的问题(除了重复问题)吗?社区显然已经决定了它的用处

标签: html css html-input


【解决方案1】:

按钮不在输入内。这里:

input[type="text"] {
    width: 200px;
    height: 20px;
    padding-right: 50px;
}

input[type="submit"] {
    margin-left: -50px;
    height: 20px;
    width: 50px;
}

示例:http://jsfiddle.net/s5GVh/

【讨论】:

  • 我就是这样做的,但是我不知道如何防止文本与提交按钮重叠。
  • 如果padding-rightmargin-left 使用相同的宽度,则不会发生这种情况
  • +1 用于右填充输入。解决标准浏览器(IE10、iOS 等)输入清除按钮与搜索按钮重叠的任何问题。
  • 这没有回答问题。问题是如何将其放入输入中。我来这里是为了寻找一种将其放入内部并根据值变化切换按钮的可见性的方法。
  • @JohanTidén 它确实回答了这个问题。对于用户而言,按钮位于字段内。当然,因为我们是程序员,所以我们可以看出它并不真正在里面……但这不是重点。重点是为用户提供愉快的体验。
【解决方案2】:

我为你找到了一个很棒的代码:

HTML

<form class="form-wrapper cf">
    <input type="text" placeholder="Search here..." required>
    <button type="submit">Search</button>
</form>

CSS

/*Clearing Floats*/
.cf:before, .cf:after {
    content:"";
    display:table;
}

.cf:after {
    clear:both;
}

.cf {
    zoom:1;
}    
/* Form wrapper styling */
.form-wrapper {
    width: 450px;
    padding: 15px;
    margin: 150px auto 50px auto;
    background: #444;
    background: rgba(0,0,0,.2);
    border-radius: 10px;
    box-shadow: 0 1px 1px rgba(0,0,0,.4) inset, 0 1px 0 rgba(255,255,255,.2);
}

/* Form text input */

.form-wrapper input {
    width: 330px;
    height: 20px;
    padding: 10px 5px;
    float: left;   
    font: bold 15px 'lucida sans', 'trebuchet MS', 'Tahoma';
    border: 0;
    background: #eee;
    border-radius: 3px 0 0 3px;     
}

.form-wrapper input:focus {
    outline: 0;
    background: #fff;
    box-shadow: 0 0 2px rgba(0,0,0,.8) inset;
}

.form-wrapper input::-webkit-input-placeholder {
   color: #999;
   font-weight: normal;
   font-style: italic;
}

.form-wrapper input:-moz-placeholder {
    color: #999;
    font-weight: normal;
    font-style: italic;
}

.form-wrapper input:-ms-input-placeholder {
    color: #999;
    font-weight: normal;
    font-style: italic;
}   

/* Form submit button */
.form-wrapper button {
    overflow: visible;
    position: relative;
    float: right;
    border: 0;
    padding: 0;
    cursor: pointer;
    height: 40px;
    width: 110px;
    font: bold 15px/40px 'lucida sans', 'trebuchet MS', 'Tahoma';
    color: #fff;
    text-transform: uppercase;
    background: #d83c3c;
    border-radius: 0 3px 3px 0;     
    text-shadow: 0 -1px 0 rgba(0, 0 ,0, .3);
}  

.form-wrapper button:hover {    
    background: #e54040;
}  

.form-wrapper button:active,
.form-wrapper button:focus {  
    background: #c42f2f;
    outline: 0;  
}

.form-wrapper button:before { /* left arrow */
    content: '';
    position: absolute;
    border-width: 8px 8px 8px 0;
    border-style: solid solid solid none;
    border-color: transparent #d83c3c transparent;
    top: 12px;
    left: -6px;
}

.form-wrapper button:hover:before {
    border-right-color: #e54040;
}

.form-wrapper button:focus:before,
.form-wrapper button:active:before {
        border-right-color: #c42f2f;
}     

.form-wrapper button::-moz-focus-inner { /* remove extra button spacing for Mozilla Firefox */
    border: 0;
    padding: 0;
}    

演示:On fiddle 来源:Speckyboy

【讨论】:

  • 有趣的事实:-9999px 是你能走的最远距离(afaicr)
  • Alfo,当您在输入中获得大量文本时,文本会与 IE 中的按钮重叠。这就是为什么这是公认的答案。
  • 如果您声明了不同的字体大小(较大的),文本将不可见。
  • 全部代码,根本没有解释......解决方案的主要思想是什么?为什么它一直有效?什么是可重复使用的部件?
【解决方案3】:

.flexContainer {
    display: flex;
}

.inputField {
    flex: 1;
}
<div class="flexContainer">
    <input type="password" class="inputField">
    <button type="submit"><img src="arrow.png" alt="Arrow Icon"></button>
</div>

【讨论】:

    【解决方案4】:

    使用 Flexbox,并将边框放在表单上。

    现在(2019 年)最好的方法是使用 flexbox。

    • 将边框放在包含元素上(在本例中,我使用了表单,但您可以使用 div)。
    • 使用 flexbox 布局将输入和按钮并排排列。允许输入拉伸以占用所有可用空间。
    • 现在通过移除边框来隐藏输入。

    运行下面的 sn-p 看看你会得到什么。

    form {
      /* This bit sets up the horizontal layout */
      display:flex;
      flex-direction:row;
      
      /* This bit draws the box around it */
      border:1px solid grey;
    
      /* I've used padding so you can see the edges of the elements. */
      padding:2px;
    }
    
    input {
      /* Tell the input to use all the available space */
      flex-grow:2;
      /* And hide the input's outline, so the form looks like the outline */
      border:none;
    }
    
    input:focus {
      /* removing the input focus blue box. Put this on the form if you like. */
      outline: none;
    }
    
    button {
      /* Just a little styling to make it pretty */
      border:1px solid blue;
      background:blue;
      color:white;
    }
    <form>
      <input />
      <button>Go</button>
    </form>

    为什么这很好

    • 它将拉伸到任何宽度。
    • 按钮将始终与需要的一样大。宽屏时不会拉伸,窄屏时不会收缩。
    • 输入文本不会放在按钮后面。

    注意事项和浏览器支持

    IE9 对 Flexbox 的支持有限,因此按钮不会位于表单的右侧。 IE9 多年来一直没有得到 Microsoft 的支持,所以我个人对此很满意。

    我在这里使用了最少的样式。我已经留在填充中以显示事物的边缘。显然,您可以制作这种外观,但是您希望它看起来带有圆角、阴影等。

    【讨论】:

    • 什么指示按钮向右对齐?
    • @Neowizard 您应该通过在他的问题中添加评论来询问 OP。您也可以对已接受的答案投反对票(作为纠正问题的一个小贡献)
    • 此解决方案不会在外部元素周围的焦点上显示蓝色边框。在这里查看如何实现这一点:stackoverflow.com/questions/38644773/…
    • @RoyPrins - 好点。如果您愿意,在表单上使用 :focus-within 伪类可以让您恢复蓝色焦点框。
    • 请避免多余的编辑。
    【解决方案5】:

    您可以使用 CSS background:url(ur_img.png) 在输入框内插入图片 但是对于创建点击事件,您需要合并您的箭头图像和输入框。

    【讨论】:

      【解决方案6】:

      这是bootstrap v3 中最干净的方法。

      <div class="form-group">
          <div class="input-group">
              <input type="text" name="search" class="form-control" placeholder="Search">
              <span><button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button></span>
           </div>
      </div>
      

      【讨论】:

        【解决方案7】:

        这可以使用 inline-block 来实现 JS fiddle here

        <html>
        <body class="body">
            <div class="form">
                <form class="email-form">
                    <input type="text" class="input">
                    <a href="#" class="button">Button</a>
                </form>
            </div>
        </body>
        </html>
        
        
        <style>
        * {
            box-sizing: border-box;
        }
        
        .body {
            font-family: Arial, sans-serif;
            font-size: 14px;
            line-height: 20px;
            color: #333;
        }
        
        .form {
            display: block;
            margin: 0 0 15px;
        }
        
        .email-form {
            display: block;
            margin-top: 20px;
            margin-left: 20px;
        }
        
        .button {
            height: 40px;
            display: inline-block;
            padding: 9px 15px;
            background-color: grey;
            color: white;
            border: 0;
            line-height: inherit;
            text-decoration: none;
            cursor: pointer;
        }
        
        .input {
            display: inline-block;
            width: 200px;
            height: 40px;
            margin-bottom: 0px;
            padding: 9px 12px;
            color: #333333;
            vertical-align: middle;
            background-color: #ffffff;
            border: 1px solid #cccccc;
            margin: 0;
            line-height: 1.42857143;
        }
        </style>
        

        【讨论】:

          猜你喜欢
          • 2019-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-15
          • 2021-01-25
          • 2021-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多