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

后端 未结 8 990
梦谈多话
梦谈多话 2021-01-30 00:22
相关标签:
8条回答
  • 2021-01-30 00:57

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

    0 讨论(0)
  • 2021-01-30 01:01

    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.

    0 讨论(0)
  • 2021-01-30 01:01

    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>
    
    0 讨论(0)
  • 2021-01-30 01:01

    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
    }
    
    0 讨论(0)
  • 2021-01-30 01:05

    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

    0 讨论(0)
  • 2021-01-30 01:06

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

    0 讨论(0)
提交回复
热议问题