How to Center an Image Between the Containers

It is now a common question “How to make the image responsive and keep it in between the containers”. Follow the given steps to solve this problem.

Create HTML

  • Create three <div> elements with the following class names: "left", "right" and "center".
  • Copy and paste the link of the image you want to center with the src attribute of the <img> and place it within the third <div>.
<div class="left"></div>
<div class="right"></div>
<div class="center">
  <img src="https://images.unsplash.com/photo-1542546068979-b6affb46ea8f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80">
</div>

Add CSS

  • Set the background-color for both the right and left containers.
  • Define the width and the height of containers.
  • Make the elements float to left and right with the float property.
  • Set the height, background-color for the middle container and set its overflow to "hidden".
.left {
  background-color: #de6502;
  width: 150px;
  height: 600px;
  float: left;
}
.right {
  background-color: #1c87c9;
  width: 150px;
  height: 600px;
  float: right;
}
.center {
  height: 600px;
  background-color: #cccccc;
  overflow: hidden;
}

So, here’s the full code

Example of centering an image between containers with the float property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        background-color: #ffffff;
      }
      .left {
        background-color: #de6502;
        width: 150px;
        height: 600px;
        float: left;
      }
      .right {
        background-color: #1c87c9;
        width: 150px;
        height: 600px;
        float: right;
      }
      .center {
        height: 600px;
        background-color: #cccccc;
        overflow: hidden;
        text-align: center;
      }
      img {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <div class="left"></div>
    <div class="right"></div>
    <div class="center">
      <img src="https://images.unsplash.com/photo-1542546068979-b6affb46ea8f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80">
    </div>
  </body>
</html>

Result

Example of centering an image between containers with the "flex" value of the display property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        background-color: #ffffff;
      }
      .center {
        flex-grow: 1;
        background-color: #cccccc;
        overflow: hidden;
        text-align: center;
      }
      .flex {
        flex-basis: 100%;
        display: flex;
      }
      .flex div {
        flex-basis: 150px;
        height: 600px;
      }
      .flex div:first-child {
        background-color: #1c87c9;
      }
      .flex div:last-child {
        background-color: #de6502;
      }
      img {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <div class="flex">
      <div></div>
      <div class="center">
        <img src="https://images.unsplash.com/photo-1542546068979-b6affb46ea8f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80">
      </div>
      <div></div>
    </div>
  </body>
</html>

Example of centering an image between containers with the "inline-block" value of the display property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      body {
        background-color: #ffffff;
      }
      .content {
        width: 100%;
      }
      .content div {
        display: inline-block;
        width: 150px;
        height: 600px;
      }
      .content div:first-child {
        background-color: pink;
      }
      .content div:last-child {
        background-color: purple;
      }
      .content .center {
        width: calc(100% - 300px);
        background-color: lightblue;
        overflow: hidden;
        text-align: center;
        margin: 0 -5px;
      }
      img {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <div class="content">
      <div></div>
      <div class="center">
        <img src="https://images.unsplash.com/photo-1542546068979-b6affb46ea8f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80">
      </div>
      <div></div>
    </div>
  </body>
</html>
Do you find this helpful?

Related articles