twitter-bootstrap 如何在div的底部和中心定位按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45181899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:56:13  来源:igfitidea点击:

How to position a button at the bottom and center of div?

htmlcsstwitter-bootstraplayoutfrontend

提问by ayeteo

I'm having a real issue trying to position a button at the bottom and center of a div. I ended up finally getting it to look right in full screen mode but I know the methodology is totally wrong.

我在尝试将按钮放置在 div 的底部和中心时遇到了真正的问题。我最终让它在全屏模式下看起来正确,但我知道这种方法是完全错误的。

I was able to align the text the way I wanted but the button not working the same with position and translate. I tried using bootstraps columns but that did not work. I'm not sure I fully understand positioning things relative to their parent elements or position at all. In addition, I tried

我能够按照我想要的方式对齐文本,但按钮与位置和翻译的工作方式不同。我尝试使用引导列,但没有奏效。我不确定我是否完全理解相对于它们的父元素或位置的定位。另外,我试过

vertical-align:bottom
vertical-align:baseline

Neither of these worked.

这些都没有奏效。

What is the best practice for positioning a button or any element at the bottom and center of a div

将按钮或任何元素定位在 div 的底部和中心的最佳做法是什么

.navbar-brand {
  font-family: 'Great Vibes', cursive;
  font-size: 30px;
  margin: 5px 0px 5px 1px;
}

.navbar-brand {
  color: #F05F40 !important;
}

.jumbotron {
  background: url('http://i65.tinypic.com/1213l9y.jpg') no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  height: 100vh;
  width: 100vw;
  margin-left: -15px;
}

.jumbotron h1 {
  font-family: 'Great Vibes', cursive;
  font-size: 75px;
  color: white;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.jumbotron h4 {
  font-family: 'Great Vibes', sans-serif;
  font-size: 25px;
  color: white;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.btn-primary {
  color: white;
  background-color: #F05F40;
  border-color: #F05F40;
  -webkit-transition: all 0.35s;
  -moz-transition: all 0.35s;
  transition: all 0.35s;
  margin-top: 25%;
}

.btn-primary:hover,
.btn-primary:focus,
.btn-primary.focus,
.btn-primary:active,
.btn-primary.active,
.open>.dropdown-toggle.btn-primary {
  color: white;
  background-color: #ee4b28;
  border-color: #ed431f;
}

.btn-primary:active,
.btn-primary.active,
.open>.dropdown-toggle.btn-primary {
  background-image: none;
}

.btn-primary.disabled,
.btn-primary[disabled],
fieldset[disabled] .btn-primary,
.btn-primary.disabled:hover,
.btn-primary[disabled]:hover,
fieldset[disabled] .btn-primary:hover,
.btn-primary.disabled:focus,
.btn-primary[disabled]:focus,
fieldset[disabled] .btn-primary:focus,
.btn-primary.disabled.focus,
.btn-primary[disabled].focus,
fieldset[disabled] .btn-primary.focus,
.btn-primary.disabled:active,
.btn-primary[disabled]:active,
fieldset[disabled] .btn-primary:active,
.btn-primary.disabled.active,
.btn-primary[disabled].active,
fieldset[disabled] .btn-primary.active {
  background-color: #F05F40;
  border-color: #F05F40;
}

.btn-primary .badge {
  color: #F05F40;
  background-color: white;
}

.btn {
  font-family: 'Open Sans', 'Helvetica Neue', Arial, sans-serif;
  border: none;
  border-radius: 300px;
  font-weight: 700;
  text-transform: uppercase;
}

.btn-xl {
  padding: 15px 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
  <div class="navbar-header">
    <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    <a href="#" class="navbar-brand">The Authentic Gentleman</a>
  </div>
  <nav class="pull-right collapse navbar-collapse bs-navbar-collapse" role="navigation">
    <ul class="nav navbar-nav">
      <li>
        <a href="#">Home</a>
      </li>
      <li>
        <a href="#">Blog</a>
      </li>
      <li>
        <a href="#">Advice</a>
      </li>
      <li class="#">
        <a href="#">About</a>
      </li>
    </ul>
  </nav>
  </div>
</header>

<div class="container-fluid">
  <div class="jumbotron">
    <h1 class="text-center">The Authetnic Gentelman</h1>
    <h4 class="text-center">Refine your life</h4>
    <div class="text-center">
      <a href="#about" class="btn btn-primary btn-xl">Find Out More</a>

    </div>
  </div>
</div>

回答by Super User

You can set it using position: absolute;. updated codepen

您可以使用position: absolute;. 更新代码笔

<div class="text-center btnDiv">
    <a href="#about" class="btn btn-primary btn-xl">Find Out More</a>
</div>


.jumbotron {
    position: relative;
}
.btnDiv {
  width: 100%;
  left: 0;
  bottom: 10px;
  position: absolute;
}

回答by Aayushi

You can also give this a try.

你也可以试试这个。

.parent{
  text-align:center;
}

.button{
  position:fixed;
  bottom:0;  
}
<div class='parent'>
  <button class='button'>Save</button>
</div>

回答by Sunny

You don't need to complicate the code with position: absolute;

您不需要使用 position: absolute; 使代码复杂化;

You can do this simply with display:table;

您可以简单地使用 display:table; 来做到这一点。

Code is mentioned bellow...

代码在下面提到......

<div class="parent">
  <div class="buttonWrap">
      <button>button</button>
  </div>
</div>


.parent {
  height: 200px;
  width: 300px;
  padding-bottom: 10px;
  background: #444;
  display: table;
}

.buttonWrap {
  display: table-cell;
  vertical-align: bottom;
}

button {
  margin: 0 auto;
  display: block;
}

回答by takachou

So add position:relative;to parent div and add the following css to the button:

所以添加position:relative;到父div并将以下css添加到按钮:

position: absolute;
bottom:   0;

CSS:

CSS:

#parent{
   position:relative;
}
#button{
   position:absolute;
   bottom:0;
}

回答by terryeah

Cleaned up your code a little bit - you don't need to give class="text-center"to every element. Put text-align: center;in the parent div instead so the child elements get that attribute down too.

稍微清理一下您的代码 - 您不需要class="text-center"对每个元素都给予帮助。text-align: center;改为放入父 div,以便子元素也可以降低该属性。

https://codepen.io/julysfx/pen/vZqXgj

https://codepen.io/julysfx/pen/vZqXgj

回答by Kay Kap

This will help you.

这会帮助你。

css:

css:

#parent{
     height: 200px;
     width: 100%;
     position: relative;
     border: 1px solid blue;

 }
#parent #child{
        width:100%; 
        margin: auto;
        text-align: center;
        bottom: 0px;
        position: absolute;
}        
<div id="parent">
   <div id="child">
      <button>click here</button>
   </div>
</div>