简体   繁体   中英

Bootstrap 4 center image but text left aligned

I am using Bootstrap 4 and I have row like this:

 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <!-- Row --> <div class="container"> <div class="row"> <div class="col-lg-4"> <img src="https://cdn.pixabay.com/photo/2018/10/15/18/32/bee-eaters-3749679_960_720.jpg" alt="" class="img-fluid"> <p>My First Text</p> </div> <div class="col-lg-4"> <img src="https://cdn.pixabay.com/photo/2013/04/04/12/34/sunset-100367_960_720.jpg" alt="" class="img-fluid"> <p>My Second Text</p> </div> <div class="col-lg-4"> <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" alt="" class="img-fluid"> <p>My Third Text</p> </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="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> 

This works fine - but as soon as I decrease the browser size I have the images among themselves which is also expected. Nevertheless the images are now left aligned. So in the mobile view I want the images also horizontal centered but the corresponding text left aligned. When I add the "text-center" to the columns, the images are horizontal center but the text as well. So how can I center the image but leave the text left aligned to the left side of the image?

Instead of using text-center , use mx-auto d-block on the images...

<img src="..." alt="" class="img-fluid mx-auto d-block">

https://www.codeply.com/go/UU1RXLwRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM