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

时间:2017-07-19 05:48:22

标签: html css twitter-bootstrap layout frontend

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

我能够按照我想要的方式对齐文本,但按钮与位置和翻译的工作方式不同。我尝试使用bootstraps列,但这不起作用。我不确定我完全理解相对于他们的父元素或位置的定位事物。另外,我试过

vertical-align:bottom
vertical-align:baseline

这些都没有奏效。

在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>

6 个答案:

答案 0 :(得分:1)

您可以使用position: absolute;进行设置。 updated codepen

<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;
}

答案 1 :(得分:1)

您也可以尝试一下。

.parent{
  text-align:center;
}

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

答案 2 :(得分:0)

这会对你有帮助。

的CSS:

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

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

答案 3 :(得分:0)

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

position: absolute;
bottom:   0;

CSS:

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

答案 4 :(得分:0)

稍微清理一下代码 - 您不需要为每个元素提供class="text-center"。将text-align: center;放在父div中,这样子元素也会将该属性降低。

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

答案 5 :(得分:0)

您不需要使用position:absolute;

使代码复杂化

您只需使用display:table;

即可

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

<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;
}