I am having a problem to center vertically a responsive image inside its container.
I have tried a few things but those are works on fixed size, not in responsive.
I am using Bootstrap 4.
Below my code and an attempt with line-height
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-md-7 img-container" style="line-height:100%">
<img src="sample.jpg" alt="Sample" class="img-fluid">
</div>
<div class="col-md-5">
<h3>Title 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore aperiam, sit placeat corporis architecto praesentium facere aut rem quis odio ab voluptates sunt, eum harum ipsam, fugit numquam aliquid laboriosam.</p>
<h3>Title 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore aperiam, sit placeat corporis architecto praesentium facere aut rem quis odio ab voluptates sunt, eum harum ipsam, fugit numquam aliquid laboriosam.</p>
<h3>Title 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore aperiam, sit placeat corporis architecto praesentium facere aut rem quis odio ab voluptates sunt, eum harum ipsam, fugit numquam aliquid laboriosam.</p>
</div>
</div>
</div>
4
Answers
You can use flexbox. For the container of the image write the following code
}
Since you’re using Bootstrap, you can add
d-flex align-items-center
to yourcol-md-7
div. This will force the column to use CSS flex box for layout, and align-items-center tells the content to vertically positioned.if you want to do it yourself, you can try this.
First you have to add other class for the container of the image
in style.css
bootstrap comes with buil-in class that you can use , d-flex on the col and m-auto on img will do :
doc : https://getbootstrap.com/docs/4.0/utilities/spacing/ & https://getbootstrap.com/docs/4.0/utilities/display/