Bootstrap 4, How do I center-align a button?

CssTwitter BootstrapBootstrap 4

Css Problem Overview


<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-8">
      <div v-for="job in job">
        <div class="text-center">
          <h1>{{ job.job_title }}</h1>
          <p><strong>Google Inc. </strong> - {{ job.location }}</p>
          <h2><u>Job Description</u></h2>
        </div>
        <p v-html="desc"></p>
        <p class="text-center">Posted: {{ formatDate(job.date_created) }}</p>
        <button v-on:click="applyResume()" id="apply-btn" class="btn btn-primary">{{ buttonText }}</button>
      </div>
    </div>
    <div class="hidden-sm-down col-md-4"></div>
  </div>
</div>

Can't figure out how to center this button. In BS 3 I would just use center-block but that's not an option in BS4. Any advice?

Css Solutions


Solution 1 - Css

In Bootstrap 4 one should use the text-center class to align inline-blocks.

NOTE: text-align:center; defined in a custom class you apply to your parent element will work regardless of the Bootstrap version you are using. And that's exactly what .text-center applies.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<div class="container">
  <div class="row">
    <div class="col text-center">
      <button class="btn btn-default">Centered button</button>
    </div>
  </div>
</div>


If the content to be centered is block or flex (not inline-), one could use flexbox to center it:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<div class="d-flex justify-content-center">
  <button class="btn btn-default">Centered button</button>
</div>

... which applies display: flex; justify-content: center to parent.

Note: don't use .row.justify-content-center instead of .d-flex.justify-content-center, as .row applies negative margins on certain responsiveness intervals, which results into unexpected horizontal scrollbars (unless .row is a direct child of .container, which applies lateral padding to counteract the negative margin, on the correct responsiveness intervals). If you must use .row, for whatever reason, override its margin and padding with .m-0.p-0, in which case you end up with pretty much the same styles as .d-flex.

Important note: The second solution is problematic when the centered content (the button) exceeds the width of the parent (.d-flex) especially when the parent has viewport width, specifically because it makes it impossible to horizontally scroll to the start of the content (left-most).
So don't use it when the content to be centered could become wider than the available parent width and all content should be accessible.

Solution 2 - Css

Try this with bootstrap

###CODE:

<div class="row justify-content-center">
  <button type="submit" class="btn btn-primary">btnText</button>
</div>

###LINK:

https://getbootstrap.com/docs/4.1/layout/grid/#variable-width-content

Solution 3 - Css

With the use of the bootstrap 4 utilities you could horizontally center an element itself by setting the horizontal margins to 'auto'.

To set the horizontal margins to auto you can use mx-auto . The m refers to margin and the x will refer to the x-axis (left+right) and auto will refer to the setting. So this will set the left margin and the right margin to the 'auto' setting. Browsers will calculate the margin equally and center the element. The setting will only work on block elements so the display:block needs to be added and with the bootstrap utilities this is done by d-block.

<button type="submit" class="btn btn-primary mx-auto d-block">Submit</button>

You can consider all browsers to fully support auto margin settings according to this answer https://stackoverflow.com/questions/36521797/browser-support-for-margin-auto so it's safe to use.

The bootstrap 4 class text-center is also a very good solution, however it needs a parent wrapper element. The benefit of using auto margin is that it can be done directly on the button element itself.

Solution 4 - Css

Here is the full HTML that I use to center by button in Bootsrap form after closing form-group:

<div class="form-row text-center">
    <div class="col-12">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
 </div>

Solution 5 - Css

Use text-center class in the parent container for Bootstrap 4

Solution 6 - Css

for a button in a collapsed navbar nothing above worked for me so in my css file i did.....

.navbar button {
	margin:auto;
}

and it worked!!

Solution 7 - Css

What worked for me was (adapt "css/style.css" to your css path):

Head HTML:

 <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />

Body HTML:

 <div class="container">
  <div class="row">
    <div class="mycentered-text">
      <button class="btn btn-default"> Login </button>
    </div>
  </div>
</div>

Css:

.mycentered-text {
    text-align:center
}

Solution 8 - Css

thing is you need to wrap your button in a parent element :

   ` <div class="text-center">
              <a href="./templatemo_558_klassy_cafe/index.html" class="btn btn- 
   outline-light me-5 ">Lavande Restaurant PH</a>`
             </div>

Solution 9 - Css

you can also just wrap with an H class or P class with a text-center attribute

Solution 10 - Css

Its Easy

<div class="align-self-center mx-auto">
              <button type="button" class="btn btn-primary">
                  Click Me!
              </button>
 </div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbbennett36View Question on Stackoverflow
Solution 1 - CsstaoView Answer on Stackoverflow
Solution 2 - Cssabo-jwad22 AhmdView Answer on Stackoverflow
Solution 3 - CssBob SiefkesView Answer on Stackoverflow
Solution 4 - CssJaskaran SinghView Answer on Stackoverflow
Solution 5 - CssSerginhoView Answer on Stackoverflow
Solution 6 - CssCartoonZzyView Answer on Stackoverflow
Solution 7 - CssRexcirusView Answer on Stackoverflow
Solution 8 - CssManuel FashView Answer on Stackoverflow
Solution 9 - CssBradView Answer on Stackoverflow
Solution 10 - CssMahmud HasanView Answer on Stackoverflow