如何对齐此文本区域右侧的按钮

时间:2018-12-10 09:59:05

标签: html css bootstrap-4

我有一个使用Bootstrap 4的网站。在我的设计中,联系表格如下:

contact form design

但是在我的实际网站中,它看起来像这样:

contact form actual

我尝试使用浮点数,还尝试创建行和列以使其正确对齐,但是没有用。

footer input, textarea {
    background: #fff;
    border: none;
    -webkit-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    -moz-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    border-radius: 15px;
    display: inline-block;
} 
footer button.btn {
    display: flex;
    justify-self: flex-end;
    padding-left: 20px;
    padding-right: 20px;
    border-radius: 15px;    
}
<div class="col-md">
    <form action="" method="post">
      <div class="row">
        <label for="name">Nombre</label>
        <input type="text" name="name">
        <label for="email">Email</label>
        <input type="text" name="email"><br>
        <div class="form-group row">
            <label for="message">Asunto</label>
            <textarea name="message" id="" cols="30" rows="10"></textarea>   
            <button type="submit" class="btn btn-info">Enviar</button>        
        </div>
      </div>
    </form>
</div>

我所有的代码:https://codepen.io/Wibblefish/pen/ZVEdqz

谢谢。

3 个答案:

答案 0 :(得分:0)

请将按钮放在form-group div的外侧

 footer input, textarea {
    background: #fff;
    border: none;
    -webkit-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    -moz-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    border-radius: 15px;
    display: inline-block;
} 
footer button.btn {
    display: flex;
    justify-self: flex-end;
    padding-left: 20px;
    padding-right: 20px;
    border-radius: 15px;    
}
<div class="col-md">
                <form action="" method="post">
                    <div class="row">
                    <label for="name">Nombre</label>
                    <input type="text" name="name">
                    <label for="email">Email</label>
                    <input type="text" name="email"><br>
                    <div class="form-group row">
                        <label for="message">Asunto</label>
                        <textarea name="message" id="" cols="30" rows="10"></textarea>   
                             
                    </div>
                    <button type="submit" class="btn btn-info">Enviar</button>   
                    </div>  
                </form>
            </div>

答案 1 :(得分:0)

read the docs并使用Bootstrap,而不是使用自定义CSS进行对齐和间距。

<form action="" method="post">
    <div class="form-row mb-2">
        <label for="name" class="col-md-3 text-right">Nombre</label>
        <input type="text" name="name" class="col-md-7 form-control">
     </div>
    <div class="form-row mb-2">
        <label for="email" class="col-md-3 text-right">Email</label>
        <input type="text" name="email" class="col-md-7 form-control">
     </div>
    <div class="form-row mb-2">
        <label for="message" class="col-md-3 text-right">Asunto</label>
        <textarea name="message" id="" cols="30" rows="10"  class="col-md-7 form-control"></textarea>
        <button type="submit" class="btn btn-info col-md-2 align-self-end">Enviar</button>
    </div>
</form>

演示:https://www.codeply.com/go/nJ0HHdEB1V

使用这种方法,您需要所有额外的CSS。

答案 2 :(得分:0)

您可以尝试使用bootstrap的flexbox实用程序:)

input, textarea {
    background: #fff;
    border: none;
    -webkit-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    -moz-box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    box-shadow: 0px 1px 4px 2px rgba(0,0,0,0.16);
    border-radius: 15px;
} 

footer button.btn {
    display: flex;
    padding-left: 20px;
    padding-right: 20px;
    border-radius: 15px;    
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container p-5 d-flex justify-content-center align-items-center" 
  <form>
    <div class=" d-flex justify-content-center align-items-center flex-column">
    <div class="form-group align-self-start">
      <label for="name" class="mx-1">Nombre</label>
      <input type="text" name="name">
    </div>
    <div class="form-group align-self-start">
      <label for="email" class="mx-3">Email</label>
      <input type="text" name="email">
    </div>
    <div class="form-group d-flex justify-content-center">
      <label for="message" class="mx-2">Asunto</label>
      <textarea name="message" class="mx-2" id="" cols="30" rows="10"></textarea>
      <button type="submit" class="btn btn-info align-self-end mx-2">Enviar</button>
    </div>
  </div>
  </form>
</div>

另外,这是working example:)