How to Center an Image Between the Containers
Don’t know how to center the image between two containers? This snippet is just for you. Read it and follow the steps to solve this problem.
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<span class="attribute">class</span>names: "left", "right" and "center". - Copy and paste the link of the image you want to center with the
<span class="attribute">src</span>attribute of the<img>and place it within the third<div>.
How to cretae HTML <div> elements with classes "left", "right" and "center"?
<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
<span class="property">height</span>,<span class="property">background-color</span>for the middle container and set its overflow to "hidden".
How to style an HTML element using CSS width, height, float and background-color properties?
.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:
An example of how to center an image between the containers
<!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
<div class="demo px-2.5 mt-1 mb-5"> XFI5 </div> XFI6 </div> <div class="center">
</div> </div> ### Example of centering an image between containers with the "flex" value of the display property:
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>