使用Bootstrap 4模态框实现标签右对齐

7

如何将这个标签右对齐?我只能将其左对齐。我必须覆盖Bootstrap 4的默认值justify-content: centerjustify-content: right !important;但它仍然左对齐。如何将其右对齐?

<div class="modal-body">
    <form class="form-horizontal">
        <div class="form-inline">
            <label class="col-md-4">Type</label>
            <input class="col-md-8" type="email" class="form-control">
        </div>
    </form>
</div>

CSS:

div.form-inline label.col-md-4 {
    justify-content: right !important; 
}

enter image description here

4个回答

16

向标签添加"class = text-right"

<div class="modal-body">
    <form class="form-horizontal">
        <div class="form-inline">
            <label class="col-md-4 text-right">Type</label>
            <input class="col-md-8" type="email" class="form-control">
        </div>
    </form>
</div>

7
您需要将它更改为:
div.form-inline label.col-md-4{
    justify-content: flex-end;
}

justify-content: right is not a valid value.


谢谢。我该如何使标签与输入组对齐到中心? - Joseph
我上传了一张图片。我的意思是如何使得“类型”和“输入表单”的空间相同,以便它们看起来正确对齐,左右两侧看起来平衡。 - Joseph
我会为输入表单设置一个较小的宽度列。尝试使用.col-md-5.col-md-6代替.col-md-8。由于输入占据整个容器的宽度,因此没有确切的方法来解决这个问题。 - Florin Pop

1

justify-content: flex-end 对我没有用(Bootstrap 4.1.0),但是 text-align: right 可以使用:

<style>
label { text-align: right }
</style>

或者在 .css 文件中:

.col-form-label { text-align: right }

0

Bootstrap 4从block布局转移到了flex布局。您需要使用justify-content: flex-end来适应您的情况。

$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').focus()
})
div.form-inline label.col-md-4{
  justify-content: flex-end; 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
         <form class="form-horizontal">
            <div class="form-inline">
              <label class="col-md-4">Type</label>
              <input class="col-md-8" type="email" class="form-control">
            </div>
          </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接