Fang
Fang

Reputation: 2419

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.

Upvotes: 0

Views: 91

Answers (3)

Ricky Ruiz
Ricky Ruiz

Reputation: 26731

Your markup is incorrect, change the following:

This:

  <p class="text</p>

To this:

  <p class="text"></p>

And this:

 <div id="fakeButton" class="col-sm-4 col-xs-4">

To this:

 <div id="fakeButton" class="col-xs-12">

The class text-xs-center in your .jumbotron element is already centering the button.

The button has display: inline-block, that is why it is centered with text-align: center declared in its ancestor.


EDIT:

You actually have more things to correct:

Wrap your #fakeButton in a .row not in a <p> tag.

Change this:

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

To this:

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

And then add this to your CSS:

input[type="file"] {
    display: block;
}

input[type="file"] {
  display: block;
}
<link href="https://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />
<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>
    <div class="row">
      <div id="fakeButton" class="col-xs-12">
        <label for="files" class="btn btn-primary btn-lg">Select Image</label>
        <input id="files" style="visibility:hidden;" type="file">
      </div>
    </div>
  </div>
</section>

Upvotes: 2

Francis Sunday
Francis Sunday

Reputation: 77

Try adding the text-center class to the button itself :

<label for="files" class="btn btn-primary btn-lg text-center">Select Image</label>

or change your html to this instead:

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

Upvotes: 1

Saurav Rastogi
Saurav Rastogi

Reputation: 9731

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!

Upvotes: 2

Related Questions