skip to Main Content

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


  1. You can use flexbox. For the container of the image write the following code

    .yourContainer{
      display:flex;
      justify-content:center;
    

    }

    Login or Signup to reply.
  2. Since you’re using Bootstrap, you can add d-flex align-items-center to your col-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.

    Login or Signup to reply.
  3. if you want to do it yourself, you can try this.
    First you have to add other class for the container of the image

    <div class="container">
        <div class="row">
            <div class="col-md-7 wrapper-img" 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>
    
    

    in style.css

    .wrapper-img{   
       display:flex;
       align-items: center;
    }
    
    
    Login or Signup to reply.
  4. bootstrap comes with buil-in class that you can use , d-flex on the col and m-auto on img will do :

    .row {border:solid}
    <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 d-flex " style="line-height:100%">
                <img src="http://dummyimage.com/100&text=sample.jpg" alt="Sample" class="img-fluid m-auto">
            </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>

    doc : https://getbootstrap.com/docs/4.0/utilities/spacing/ & https://getbootstrap.com/docs/4.0/utilities/display/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search