Fang Fang - 17 days ago 7
CSS Question

Centering button on website

<body>
<div class="navbar-collapse collapse inverse" id="navbar-header">
<div class="container-fluid">
<div class="about">
<h4>text</h4>
<p class="text</p>
<div class="imgWrapper">
<img id="photo" src="path/to/file"/>
</div>
</div>
<div class="social">
<p>text</p>
</div>
</div>
</div>

<div class="navbar navbar-static-top navbar-dark bg-inverse">
<div class="container-fluid">
<a id="logoutButton" href="logout.php" class="btn btn-lg btn-
primary">Logout</a>
<button id="navbar-button" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-header" aria-controls="navbar-header" aria-expanded="false" aria-label="Toggle navigation"></button>
<a href="#" class="navbar-brand">Media Manager</a>
</div>
</div>

<section class="jumbotron text-xs-center">
<div class="container">
<h1 class="jumbotron-heading">Album list</h1>
<p id="jumbotron-header-text" class="lead text-muted">text</p>
<p>
<div id="fakeButton" class="col-sm-4 col-xs-4">
<label for="files" class="btn btn-primary btn-lg">Select Image</label>
<input id="files" style="visibility:hidden;" type="file">
</div>
</p>
</div>
</section>


...

I'm trying to center the element with the id "fakeButton" on my Jumbotron, and I'm failing miserably. Things I've tried:

1) CSS Selector

#fakeButton {
margin: auto !important;
float: none !important;
}


2) Using bootstrap class text-center:

<div class="span7 text-center"></div>


I'm using Bootstrap 4. Most of the code is taken from this Bootstrap example:

http://v4-alpha.getbootstrap.com/examples/album/

I've made sure Bootstrap is installed correctly, but I can't seem to figure out how to center that element.

Answer

Use Bootstrap's text-center class with col-xs-12 on #fakeButton.

Use:

<div id="fakeButton" class="col-xs-12 text-center">
    <label for="files" class="btn btn-primary btn-lg">Select Image</label>
    <input id="files" style="visibility:hidden;" type="file">
</div>

Instead of:

<div id="fakeButton" class="col-sm-4 col-xs-4">
    <label for="files" class="btn btn-primary btn-lg">Select Image</label>
    <input id="files" style="visibility:hidden;" type="file">
</div>

Have a look at the snippet below:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
   <div class="navbar-collapse collapse inverse" id="navbar-header">
      <div class="container-fluid">
         <div class="about">
            <h4>text</h4>
            <p class="text"></p>
            <div class="imgWrapper">
                <img id="photo" src="path/to/file"/>
            </div>
        </div>
           <div class="social">
            <p>text</p>
           </div>
       </div>
   </div>

    <div class="navbar navbar-static-top navbar-dark bg-inverse">
    <div class="container-fluid">
        <a id="logoutButton" href="logout.php" class="btn btn-lg btn-     
          primary">Logout</a>
        <button id="navbar-button" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-header" aria-controls="navbar-header" aria-expanded="false" aria-label="Toggle navigation"></button>
        <a href="#" class="navbar-brand">Media Manager</a>
            </div>
    </div>

    <section class="jumbotron text-xs-center">
      <div class="container">
        <h1 class="jumbotron-heading">Album list</h1>
        <p id="jumbotron-header-text" class="lead text-muted">text</p>
          <p>
              <div id="fakeButton" class="col-xs-12 text-center">
                  <label for="files" class="btn btn-primary btn-lg">Select Image</label>
                  <input id="files" style="visibility:hidden;" type="file">
              </div>
          </p>
      </div>
    </section>

Hope this helps!

Comments