I wanted to center vertically my text inside a
div class="col-md-6"
and have the same height
Example:
I wanted to center vertically my text inside a
div class="col-md-6"
and have the same height
Example:
this is bootstrap 4:
<html><head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="row">
<div class="col-6">
<img src="https://media.haircutinspiration.com/photos/20181204011855/boy-hairstyle-e1536810585307.jpg" class="img-fluid">
</div>
<div class="col-6 align-self-center">
hello hello
</div>
</div>
</body>
Check my snippet it will work as you want ...
.content_center {
display: flex;
justify-content: center;
align-items: center;
width: 250px;
height: 150px;
border : 1px solid black;
}
<div class="content_center col-md-6">
What I want
</div>
Define a custom class and use as follows:
.vertical-align {
display: flex;
align-items: center;
}
<div class="container">
<div class="row vertical-align">
<div class="col-md-6"><img src=" http://placehold.it/150x50" /></div>
<div class="col-md-6">Dummy text</div>
</div>
</div>
Use margin-top:50%; to the text;
.vertical{
margin-top:50%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6">
<img alt="img" src="http://asianmeditour.com/assets/images/post/1542457653tourism.jpg" class="img-responsive">
</div>
<div class="col-sm-6">
<h3 class="vertical">Column 3</h3>
</div>
</div>
</div>
</body>
</html>
If you know the height, then set line height equal to height of div if there is single line.
If more than one line.
<div class="col-xs-6 floatingDiv">
This should be in the middle
</div>
CSS
.floatingDiv {
display: flex;
align-items:center;
}
Try following css on your text element:
.className{
position: relative;
top:50%;
transform: translateY(-50%);
}
in Bootstrap 4, use the class align-self-center in the parent element.
<div class="align-self-center">
hello hello
</div>
Based on Bootstrap 4, which is now flex-based, you can use the align-items-center
utility on a Bootstrap row
to vertically centre the row's content.
For instance:
<div class="container">
<div class="row align-items-center">
<div class="col-4">
<img alt="img" src="http://asianmeditour.com/assets/images/post/1542457653tourism.jpg" class="img-fluid">
</div>
<div class="col-8">
Text vertically centred!
</div>
</div>
</div>
JSFiddle here.
I found about the align-items-center
keyword here.
<div class="col-md-6" style="height: 450px;background-color: red;">
<div class="v_align">
What i have
</div>
</div>
<style>
.v_align {
top: 50%;
transform: translateY(-50%);
position: absolute;
}
</style>