How to align vertical center image in bootstrap?
April 21, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, I am going to show you. How to align vertical center image in bootstrap with code example.
Here, we will use .align-items: center
class to make a image in vertical center.
For vertical alignment, using .display: flex
is again really helpful.
Consider a case where our container has a height of 700px, but the height of the image is only 500px.
Now, in this case, adding a single line of code to the container, .align-items: center
, does the trick.
Example:
Let’s look at the following example to understand how it basically works:
<!doctype html>
<html lang="en" class="h-100%">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Bootstrap</title>
<style>
div {
display: flex;
align-items: center;
height: 700px;
}
img {
width: 60%;
height: 500px;
}
</style>
</head>
<body class="h-100%">
<div class="container-flued h-100%">
<div class="image">
<img src="images/pexels-photo-257360.jpeg">
</div>
</div>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
The align-items property can position elements vertically if used together with display: flex.
Check the output of the above code example.
All the best 👍