【问题标题】:Button inside input HTML CSS [duplicate]输入HTML CSS内的按钮[重复]
【发布时间】:2017-09-04 19:40:45
【问题描述】:

我正在尝试使用内部按钮创建一些输入,如下所示:

我阅读了一些教程,这段代码应该可以工作,但我不知道为什么它不工作。 这是 CSS:

#content input[class="small"] {
  font-weight: 400;
  color: #909090;
  outline: none;
  margin-top: 15px;
  width: auto;
    height: 50px;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    border: solid 2px #e5e5e5;
  padding-left: 30px;
}

#content button[type="submit"] {
  display: inline-block;
  width: 50px;
    height: 50px;
  padding-right: 10px;
    -webkit-border-radius: 0 10px 10px 0;
    -moz-border-radius: 0 10px 10px 0;
    border-radius: 0 10px 10px 0;
    background-color: #23a298;
    border: solid 2px #e5e5e5;
}

代码:https://jsfiddle.net/erxsqovh/

【问题讨论】:

  • 你在用bootstrap吗
  • 不,我正在编写所有代码
  • 使用内联块和相对位置来定位按钮。希望有些人已经给出了答案。我建议导入引导程序并在必要时使用。它可以节省时间。很多东西都已经写好了
  • 请在问题本身中包含您的 HTML ......不是外部墨水。最好还是使用编辑器中的<>按钮来创建一个SO sn-p,这就像一个小提琴,但是嵌入到了问题中。
  • 另一种方法,请参阅:stackoverflow.com/questions/45930419/…

标签: html css button input


【解决方案1】:

在您的代码col-3>div has width 33% 中。元素没有空间适合。因此它转到下一行。那是输入框旁边的按钮,不在输入框内。

同时使用媒体查询和宽度以获得更好的输出。

<input type=text><button>Submit</button>

看看这个。按钮和输入框的对齐无需编写特殊的 CSS。

plunker 用于您的代码 - 已修复

【讨论】:

    【解决方案2】:

    这不是输入内的按钮,而是 div 内的输入旁边的按钮,看起来像输入,您可以通过执行以下操作来实现它

    $(document).ready(function(){
        $(".search-confirm").on('click', function(){
             var keyword = $("#search-field").val();
             alert("You searched - "+keyword)
        })
    })
    .input-outer-padding {
      position: relative;
      width: 280px;
      border: 1px solid #3c0;
      border-radius: 3px;
      overflow: hidden;
    }
    
    .input-inner-padding {
      width: auto;
      height: auto;
      padding: 5px 60px 5px 5px;
    }
    
    .input-field {
      width: 100%;
      display: block;
      /*resets for hiding the input boundaries */
      border: 0px;
      margin: 0px;
      padding: 0px;
      outline: none;
      font-family: sans-serif;
      font-size: 16px;
      color: #3c0;
    }
    
    .search-confirm {
      padding: 7px 10px;
      background-color: #3c0;
      font-family: sans-serif;
      font-size: 14px;
      font-weight: bold;
      color: white;
      cursor: pointer;
      /* here the magic begins */
      position: absolute;
      z-index: 1;
      top: 0;
      right: 0;
    }
    
    .search-confirm:hover {
      background-color: #09f;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="input-outer-padding">
      <div class="input-inner-padding">
        <input type="text" id="search-field" class="input-field" placeholder="Search" />
      </div>
      <div class="search-confirm">SEARCH</div>
    </div>

    【讨论】:

    • jquery/javascript 在这里完全没有必要。
    • @JonP js 是为了好玩,与问题完全无关
    【解决方案3】:

    #content {
      position: absolute;
      width: 100%;
      margin-top: 100px;
      background-color: #fcfcfc;
      height: 1080px;
    }
    
    #content .box-name {
      margin-top: 30px;
      margin-left: 120px;
      margin-bottom: 0px;
      font-size: 20px;
      color: #b4b4b4;
      margin-left:
    }
    
    #content .box {
      position: absolute;
      margin-top: 15px;
      margin-left: 90px;
      margin-right: 90px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      background-color: #fff;
      -webkit-box-shadow: 0 0 20px rgba(0, 0, 0, .05);
      -moz-box-shadow: 0 0 20px rgba(0, 0, 0, .05);
      box-shadow: 0 0 20px rgba(0, 0, 0, .05);
    }
    
    #content .item:first-of-type {
      margin-top: 60px;
      margin-left: 60px;
      margin-right: 60px;
      margin-bottom: 30px;
    }
    
    #content .item {
      margin-top: 0px;
      margin-left: 60px;
      margin-right: 45px;
      margin-bottom: 60px;
    }
    
    #content label {
      color: #6c6c6c;
      font-weight: 600;
      font-size: 15px;
      width:100%
    }
    
    #content input[type=text] {
      font-weight: 400;
      color: #909090;
      outline: none;
      margin-top: 15px;
      width:100%
      height: 50px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      border: solid 2px #e5e5e5;
      padding-left: 30px;
    }
    
    #content input::placeholder {
      font-weight: 400;
      color: #e5e5e5;
    }
    
    #content input[type=text]:focus {
      outline: none;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      border: solid 2px #91d0cb;
      -webkit-transition: border-color 0.4s ease-out;
      -moz-transition: border-color 0.4s ease-out;
      -o-transition: border-color 0.4s ease-out;
      -ms-transition: border-color 0.4s ease-out;
      transition: border-color 0.4s ease-out;
    }
    
    #content input[class="small"] {
       font-weight: 400;
        color: #909090;
        outline: none;
        margin-top: 15px;
        width: 100%;
        height: 50px;
        -webkit-border-radius: 10px;
        -moz-border-radius: 10px;
        border-radius: 10px;
        border: solid 2px #e5e5e5;
        padding-right: 30px;
        position: relative;
        display: inline-flex;
    }
    
    #content button[type="submit"] {
         display: inline-block;
        height: 55px;
        margin: 0;
        position: relative;
        right: 44px;
        top: 15px;
        padding-right: 10px;
        -webkit-border-radius: 0 10px 10px 0;
        -moz-border-radius: 0 10px 10px 0;
        border-radius: 0 10px 10px 0;
        background-color: #23a298;
        border: solid 2px #e5e5e5;
    }
    
    #content input::placeholder {
      font-weight: 400;
      color: #e5e5e5;
    }
    .b{ position: relative;
        width: 100%;
        display: inline-flex; }
    #content input[type=text]:focus {
      outline: none;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      border: solid 2px #91d0cb;
      -webkit-transition: border-color 0.4s ease-out;
      -moz-transition: border-color 0.4s ease-out;
      -o-transition: border-color 0.4s ease-out;
      -ms-transition: border-color 0.4s ease-out;
      transition: border-color 0.4s ease-out;
    }
    
    #content select[type=text] {
      font-weight: 400;
      color: #909090;
      outline: none;
      margin-top: 15px;
      width: 100%;
      height: 50px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      border: solid 2px #e5e5e5;
      padding-left: 30px;
    }
    
    #content button {
      font-size: 15px;
      font-weight: 600;
      color: #fff;
      height: 50px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      background-color: #23a298;
      padding: 10px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      margin-top: 10px;
      margin-left: 30px;
      border: none;
      cursor: pointer;
    }
    
    #content button:hover {
      background-color: #22dccd;
      -webkit-transition: 0.4s ease-out;
      -moz-transition: 0.4s ease-out;
      -o-transition: 0.4s ease-out;
      -ms-transition: r 0.4s ease-out;
      transition: 0.4s ease-out;
      -webkit-transform: scale(1.1);
      -ms-transform: scale(1.1);
      transform: scale(1.1);
    }
    
    input,
    button {
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
    
    #col-3 > div {
      width: 33%;
      height: 100%;
      float: left;
      margin-bottom: 30px;
    }
    
    #col-3 div div {
      height: 100%;
      margin-bottom: 30px;
    }
    
    #col-3 div div.b {
      margin: 0 30px;
      margin-bottom: 30px;
    }
    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <link rel="stylesheet" type="text/css" href="square/red.css">
    <body>
    <section id="content">
        <div class="container">
            <div class="item">
              <div id="col-sm-6">
                <div>
                    <div>
                    <label for="name">Person</label>
                        <div class="b">
                          <input class="small" type="text" id="name" name="name" placeholder="Type something...">
                          <button type="submit"></button>
                        </div>
                    </div>
                  <div>
                  </div>
                </div>
              </div>
            </div>
        </div>
    </section>
    
    </body>
    </html>

    这是我认为的正确方式

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 2011-06-28
      • 2018-04-07
      • 2012-01-27
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 2021-11-06
      相关资源
      最近更新 更多