【问题标题】:How to align two Buttons to their respective Form Input?如何将两个按钮与各自的表单输入对齐?
【发布时间】:2019-05-29 16:21:32
【问题描述】:

试图弄清楚如何将一些按钮与一些输入字段对齐,这非常棘手。我想不通。

我在网上找到了很多东西: Align button to input with float? Align button with input forms with labels Make form button/text field same height in all browsers?

我知道我会遇到很多缺点,但我无法理解这一点,我需要帮助。

我已经浪费了一整天的时间来修改值,而且我不知道如何在 CSS 中定位东西。看不懂。

.big-box {
  position: absolute;
  width: 60%;
  top: 40%;
  left: 50%;
  text-align: left;
  transform: translate(-50%, -50%);
  text-align: center;
}

.box-head {
  width: 100%;
  display: grid;
  border-bottom: 6px solid red;
  margin-bottom: 50px;
  margin-top: 40px;
  text-align: center;
}

.textbox {
  display: block;
  float: left;
  width: 75%;
  overflow: hidden;
  font-size: 20px;
  padding: 5px 0;
  margin: 10px 0;
  border-bottom: 1px solid red;
}

.textbox input {
  border: none;
  outline: none;
  background: none;
  color: white;
  font-size: 18px;
  width: 97%;
  float: left;
  margin: 5px 5px;
}

#button {
  display: grid;
  width: 25%;
  background: none;
  border: 2px solid red;
  color: white;
  padding: 5px;
  font-size: 18px;
  cursor: pointer;
}
<div id="logo"><img url="logo.png"></div>
<div class="big-box">
  <div class="box-head">
    <div class="title">
      <h1>Configuration Page</h1>
    </div>
  </div>
  <form method="post" action="/url" onSubmit="return saveValue();">
    <div class="textbox">
      <input type="url" placeholder="Enter URL" name="getURL" value="">
    </div>
    <input id="button" type="submit" value="Enter">
    <div class="textbox">
      <input type="number" placeholder="Brightness" name="getBrightness" value="">
    </div>
    <input id="button" type="submit" value="Enter">
  </form>
</div>

编辑:这就是我修复它的方法:

  • 我已从班级更改为 ID:#textbox

  • 我已将#textbox#button 放入div:#box-box;

  • 我已将 display: flex; 添加到 #box-boxdisplay: grid;#textbox#button

  • margin-left: 25px;添加到#button

这里是result

感谢@Flavio Caruso 的启发。

<body>
    <div id = "logo"><img url="logo.png"></div>
    <div class = "big-box">
        <div class = "box-head">
            <div class = "title"><h1>Configuration Page</h1></div>
        </div>
        <form method="post" action="/url">
            <div id="box-box">
                <div id = "textbox" >
                    <input type="url" placeholder="Enter URL" name="getURL" value="">
                </div>
                <input id ="button" type="submit" value="Enter">
            </div>
            <div id="box-box">
                <div id = "textbox" >
                    <input type="url" placeholder="Enter URL" name="getURL" value="">
                </div>
                <input id ="button" type="submit" value="Enter">
            </div>
            <div id="box-box">
                <div id = "textbox" >
                    <input type="url" placeholder="Enter URL" name="getURL" value="">
                </div>
                <input id ="button" type="submit" value="Enter">
            </div>
        </form>
    </div>
</body>
#logo {
    width: 94%;
    height: 50px;
    background: url(logo.png) left no-repeat;
    background-size:contain;
    margin: 30px auto;
}

**#box-box {
    display:flex;
}**

#textbox {
    **display: grid;**
    width: 70%;    
    overflow: hidden;
    font-size: 20px;
    padding 10px 0;
    margin: 10px 0;
    border-bottom: 1px solid red;
}

#textbox input {
    border: none;
    outline: none;
    background: none;
    color: white;
    font-size: 18px;
    width: 97%;
    float: left;
    margin: 5px 5px;
}

#button {
    **display: grid;**
    width: 25%;
    background: none;
    border: 2px solid red;    
    color: white;
    padding: 5px;
    font-size: 18px;
    cursor: pointer;
    margin: 10px 0;
    **margin-left: 25px;**
    margin-top: 30px;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    您必须做一些更改,您必须在 div 类“文本框”中插入输入按钮并添加一个 display:flex,然后将 css 从按钮调整为内联块并向右浮动。 像这样:

    .big-box {    
        position: absolute;
        width: 60%;
        top: 40%;
        left: 50%;
        text-align: left;
        transform: translate(-50%, -50%);
        text-align: center;
    }
    
    .box-head {    
        width: 100%;   
        display: grid;
        border-bottom: 6px solid red;
        margin-bottom: 50px;
        margin-top: 40px;
        text-align: center;
    }
    
    .textbox {
        display: flex;
        float: left;
        width: 75%;
        overflow: hidden;
        font-size: 20px;
        padding: 5px 0;
        margin: 10px 0;
        border-bottom: 1px solid red;
    }
    
    .textbox input {
        border: none;
        outline: none;
        background: none;
        color: white;
        font-size: 18px;
        width: 97%;
        margin: 5px 5px;
    }
    
    #button {
        display: inline-block;
        float: right;
        width: 25%;
        background: none;
        border: 2px solid red;    
        color: white;
        padding: 5px;
        font-size: 18px;
        cursor: pointer; 
    }
    <body>
        <div id = "logo"><img url="logo.png"></div>
        <div class = "big-box">
            <div class = "box-head">
                <div class = "title"><h1>Configuration Page</h1></div>
            </div>
            
            <form method="post" action="/url" onSubmit="return saveValue();">
                <div class = "textbox" >
                    <input type="url" placeholder="Enter URL" name="getURL" value="">
                
                <input id ="button" type="submit" value="Enter">
                </div>
                <div class = "textbox" >
                    <input type="number" placeholder="Brightness" name="getBrightness" value="">
                    <input id ="button" type="submit" value="Enter"> 
                </div>
            </form>
        </div>
    </body>  

    【讨论】:

    • 这不是我真正想要的。我希望按钮在输入旁边,而不是在里面。这就是我得到的:image。可以看到按钮在.textboxborder-bottom: 1px solid red;上方。我希望他们在他们旁边,在右边,而不是在他们之上。谢谢!
    • display: inline-block; float: right; 添加到按钮不会改变它们的位置。所以我认为这不是我想要的。
    • 我修改了.textbox的边框:image。您可以清楚地看到按钮位于.textbox 内部。我希望他们出来并在他们的右侧。
    • 我已经让它按照我想要的方式工作了。我已经编辑了我的原始帖子,你检查它!谢谢!
    • 对不起@bleah1 我现在才看了你的cmets,但我看到你做得很好!
    猜你喜欢
    • 2016-11-21
    • 2019-05-25
    • 2017-04-14
    • 2012-05-23
    • 2012-10-12
    • 2019-11-21
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多