How to center this image with Bootstrap

Asked

Viewed 4,831 times

2

I’m trying to center the image but I was not successful, I was able to center, but I don’t know how to put in the middle of the div

    <!doctype html>
    <html lang="pt-br">
    <head>

    <title>Bootstrap - Alinhamento do grid</title>
    <style type="text/css">
      .marcador {
        background: #fe9d9d;
        border: 1px solid #f94646;
        height: 420px;
      }
      .marcador-container {
        background: #5bf946;
        border: 1px solid #36ae26;
      }
      .borda{
        border: 1px solid blue;
      }

    </style>

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">

     </head>

     <body>

    <div class="container">

      <h2>Alinhamento vertical (align-items-ALINHAMENTO: start, center, end)</h2>
      <div class="row marcador align-items-center">
        <div class="col">
          <img class="img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTN65ED_ansEu-oVmEbiRV2a06nsb1SYsWG2x8RwcJ9bUnFmcXNCw">
        </div>
      </div>

    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

3 answers

4


Use in the mx-auto text-center class that will center on the screen. Note: I put a new class in the image to make it responsive: img-Responsive. Follow the code:

<div class="container">

  <h2>Alinhamento vertical (align-items-ALINHAMENTO: start, center, end)</h2>
  <div class="row marcador align-items-center">
    <div class="col mx-auto text-center">
      <img class="img-responsive" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTN65ED_ansEu-oVmEbiRV2a06nsb1SYsWG2x8RwcJ9bUnFmcXNCw">
    </div>
  </div>

</div>

inserir a descrição da imagem aqui

3

That class .align-items-center works in Bootstrap 4 (which has native flexbox support), not 3.

To center vertically on Bootstrap 3, add the properties to the class in CSS:

.align-items-center{
   display: flex;
   align-items: center;
}

Behold:

.marcador {
    background: #fe9d9d;
    border: 1px solid #f94646;
    height: 420px;
  }
  .marcador-container {
    background: #5bf946;
    border: 1px solid #36ae26;
  }
  .borda{
    border: 1px solid blue;
  }
  
.align-items-center{
   display: flex;
   align-items: center;
}
<div class="container">

  <h2>Alinhamento vertical (align-items-ALINHAMENTO: start, center, end)</h2>
  <div class="row marcador align-items-center">
    <div class="col">
      <img class="img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTN65ED_ansEu-oVmEbiRV2a06nsb1SYsWG2x8RwcJ9bUnFmcXNCw">
    </div>
  </div>

</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="js/bootstrap.min.js"></script>

2

This type of alignment is currently done with flexbox. I added a div between the col and the image, follows code:

<div class="container">
    <h2>Alinhamento vertical (align-items-ALINHAMENTO: start, center, end)</h2>
        <div class="row marcador align-items-center">
            <div class="col">
                <div style="display: flex;width: 100%;justify-content: center;">
                    <img class="img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTN65ED_ansEu-oVmEbiRV2a06nsb1SYsWG2x8RwcJ9bUnFmcXNCw">
                </div>
            </div>
        </div>
    </div>

If you’re using Bootstrap 4, the CSS I’ve added can only be done using the flex classes, which you find here: https://getbootstrap.com/docs/4.0/utilities/flex/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.