【问题标题】:vertically center a button垂直居中按钮
【发布时间】:2021-10-06 21:42:03
【问题描述】:

我试图将包含<button><form> 标记居中

我的视图是什么样的:

  • <p> 标签和 '' 在 flex 中。
  • <p> 标签垂直居中对齐
  • 按钮位于底部

我尝试了什么:

  • class="align-middle"<form>

我知道我可以设置像素下边距,但这似乎是一种草率的编码方式,因为如果我想稍后添加或删除<p>,我将不得不回来重新调整边距


<div class="d-flex align-items-center">
    <p>some random text placeholder</p>

    <form class="align-items-center" asp-controller="Dashboard" asp-action="Remove" asp-route-ParticipantId="@mmLeagueParticipant.child.ParticipantId" method="POST">
        <button class="btn btn-outline-danger mt-5 bg-light">remove</button>
    </form>
</div>

使用引导程序 4.6

【问题讨论】:

  • Bootstrap 具有 justify-content-center 类,您可以将其应用于表单。 Bootstrap doc
  • 我试过用&lt;div class="justify-content-center"&gt;包围&lt;form&gt;,但它没有任何改变。也试过&lt;div class="d-flex align-items-center"&gt;

标签: css bootstrap-4 margin vertical-alignment


【解决方案1】:

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-v4-rtl/4.6.0-2/css/bootstrap.min.css" rel="stylesheet"/>

<div class="d-flex justify-content-center align-items-center">
    <span class="d-flex align-items-center">some random text placeholder</span>

    <form class="" asp-controller="Dashboard" asp-action="Remove" asp-route-ParticipantId="@mmLeagueParticipant.child.ParticipantId" method="POST">
        <button class="btn btn-outline-danger bg-light">remove</button>
    </form>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-v4-rtl/4.6.0-2/js/bootstrap.min.js"></script>

【讨论】:

  • &lt;p&gt; 和按钮应该水平相邻。并且按钮需要居中而不是垂直停留在底部。
  • 实际上你应该 align-center p 而不是 button 并且 p 元素默认会得到一个边距底部,所以你可以 margin-0 p 元素或使用 span 然后居中对齐
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 2016-08-15
  • 2012-09-14
  • 2012-02-05
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
相关资源
最近更新 更多