Berisko Berisko - 2 months ago 11
CSS Question

Center Bootstrap Button

I'm having trouble with centering Bootstrap button in panel. Margin right and left auto doesn't work.
Is there a way to properly center the button? The Button needs to be centered because of the mobile scale.

enter image description here

Code:

<div class="n_monitor">
<div class="panel panel-default">
<div class="panel-body">
<div class="btn-group">
<button type="button" class="btn btn-default">Server 1</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
</ul>
</div>
</div>
</div>
</div>


CSS:

.n_monitor .panel {
width: 628px;
border: solid 1px #ebebeb;
border-top-left-radius: 0px;
border-top-right-radius: 0px;
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
height: 94px;
box-shadow: none;
margin: 0;
background-color: #f4f4f4;
margin-left: auto;
margin-right: auto;
}

.n_monitor .btn {
width: 126px;
height: 54px;
background-color: #3baed8;
border: none;
color: #fff;
font-size: 14px;
border-radius: 3px;
}

Answer

Add Bootstrap's Alignment Class text-center to .panel-body.

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');

.n_monitor .panel {
  width: 100%;
  border: solid 1px #ebebeb;
  border-top-left-radius: 0px;
  border-top-right-radius: 0px;
  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
  height: 94px;
  box-shadow: none;
  margin: 0;
  background-color: #f4f4f4;
  margin-left: auto;
  margin-right: auto;
}

.n_monitor .btn {
  width: 126px;
  height: 54px;
  background-color: #3baed8;
  border: none;
  color: #fff;
  font-size: 14px;
  border-radius: 3px;
}
<div class="n_monitor">
  <div class="panel panel-default">
    <div class="panel-body text-center">
        <div class="btn-group">
          <button type="button" class="btn btn-default">Server 1</button>
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
          </ul>
      </div>
    </div>
  </div>
</div>