Bootstrap 4 Center Vertical and Horizontal Alignment

前端 未结 11 2812
借酒劲吻你
借酒劲吻你 2020-11-21 04:37

I have a page where only form exists and I want form to be placed in the center of the screen.

相关标签:
11条回答
  • 2020-11-21 05:14

    I ended up here because I was having an issue with Bootstrap 4 grid system and an Angular *ngFor loop. I fixed it by applying a col justify-content-center class to the div implementing the ngFor:

    <div class="row" style="border:1px solid red;">
      <div class="col d-flex justify-content-center">
        <button mat-raised-button>text</button>
      </div>
      <div *ngFor="let text of buttonText" class="col d-flex justify-content-center">
        <button mat-raised-button>text</button>
      </div>
    </div>
    

    which gives the result:

    0 讨论(0)
  • 2020-11-21 05:15

    Bootstrap has text-center to center a text. For example

    <div class="container text-center">
    

    You change the following

    <div class="row justify-content-center align-items-center">
    

    to the following

    <div class="row text-center">
    
    0 讨论(0)
  • 2020-11-21 05:16

    Update 2019 - Bootstrap 4.3.1

    There's no need for extra CSS. What's already included in Bootstrap will work. Make sure the container(s) of the form are full height. Bootstrap 4 now has a h-100 class for 100% height...

    Vertical center:

    <div class="container h-100">
      <div class="row h-100 justify-content-center align-items-center">
        <form class="col-12">
          <div class="form-group">
            <label for="formGroupExampleInput">Example label</label>
            <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input">
          </div>
          <div class="form-group">
            <label for="formGroupExampleInput2">Another label</label>
            <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input">
          </div>
        </form>   
      </div>
    </div>
    

    https://codeply.com/go/raCutAGHre

    the height of the container with the item(s) to center should be 100% (or whatever the desired height is relative to the centered item)

    Note: When using height:100% (percentage height) on any element, the element takes in the height of it's container. In modern browsers vh units height:100vh; can be used instead of % to get the desired height.

    Therefore, you can set html, body {height: 100%}, or use the new min-vh-100 class on container instead of h-100.


    Horizontal center:

    • text-center to center display:inline elements & column content
    • mx-auto for centering inside flex elements
    • offset-* or mx-auto can be used to center columns (.col-)
    • justify-content-center to center columns (col-*) inside row

    Vertical Align Center in Bootstrap 4
    Bootstrap 4 full-screen centered form
    Bootstrap 4 center input group
    Bootstrap 4 horizontal + vertical center full screen

    0 讨论(0)
  • 2020-11-21 05:17

    This work for me:

    <section class="h-100">
      <header class="container h-100">
        <div class="d-flex align-items-center justify-content-center h-100">
          <div class="d-flex flex-column">
            <h1 class="text align-self-center p-2">item 1</h1>
            <h4 class="text align-self-center p-2">item 2</h4>
            <button class="btn btn-danger align-self-center p-2" type="button" name="button">item 3</button>
          </div>
        </div>
      </header>
    </section>
    
    0 讨论(0)
  • 2020-11-21 05:18

    None has worked for me. But his one did.

    Since the Bootstrap 4 .row class is now display:flex you can simply use the new align-self-center flexbox utility on any column to vertically center it:

    <div class=”row”>
       <div class=”col-6 align-self-center”>
          <div class=”card card-block”>
          Center
          </div>
       </div>
       <div class=”col-6">
          <div class=”card card-inverse card-danger”>
          Taller
          </div>
       </div>
    </div>
    

    I learned about it from https://medium.com/wdstack/bootstrap-4-vertical-center-1211448a2eff

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