-2

I am trying to make 3 contents appear vertically center inside a div. I used vertical-align:middle but it does not do what i want to do. How can I fix this?

I want the home_post_time to appear at the right corner vertically centered.I've tried float:right; but could not do what i wanted. This is how i want it to appear

Image
Image    text                time
Image

.home_post_sections {
  width: 100%;
  height: 33%;
  background-color: #01ff70;
  text-align: left;
}

.home_post_propic {
  width: 55px;
  height: 55px;
  margin-top: 5px;
  margin-left: 5px;
  margin-bottom: 5px;
}

.home_post_username {
  margin-left: 20px;
  width: 100%;
  vertical-align: middle;
}

.home_post_time {
  float: right;
  vertical-align: middle;
  text-align: right;
  margin-right: 10px;
}
<div className="home_post_sections">

  <img src="http://lorempixel.com/output/people-q-c-55-55-9.jpg" className="home_post_propic" />
  <a className="home_post_username">User name</a>
  <a className="home_post_time">23h</a>

</div>
mplungjan
  • 134,906
  • 25
  • 152
  • 209
CraZyDroiD
  • 5,264
  • 21
  • 64
  • 138

3 Answers3

1

You can use this

<div  style="display:flex;justify-content:center;align-items:center;">
<h1>Text In center</h1>
</div>

for your code

<div style="display:flex;justify-content:center;align-items:center;">

<img src="http://lorempixel.com/output/people-q-c-55-55-9.jpg" className="home_post_propic" />
<a className="home_post_username">User name</a>
 <a className="home_post_time">23h</a>
</div>
Sandeep Bhaskar
  • 300
  • 2
  • 12
1

you can use flex to make it vertical center as below

<style>
 .home_post_sections{
   display : flex;
   align-items : center; //make child items align vertically center
 }
</style>

<div className="home_post_sections">

  <img src="http://lorempixel.com/output/people-q-c-55-55-9.jpg" className="home_post_propic" />
  <a className="home_post_username">User name</a>
  <a className="home_post_time">23h</a>

</div>
Jagajit Prusty
  • 1,852
  • 1
  • 13
  • 35
0

I've made the main container position: relative; The time container I've made position: absolute; with a top of 50% and a margin-top minus half of its height. 10px was just an approximate.

.home_post_sections {
    position: relative;
}
.home_post_time {
    position: absolute;
    top: 50%;
    right: 10px;
    margin-top: -10px;
}

https://jsfiddle.net/1f09hp3f/

You've used mostly static widths and heights so this solution should work for you, if you only want to center align one element. Otherwise you can have a look at the below link to center align all elements vertically. Vertical alignment of elements in a div

Community
  • 1
  • 1
Gezzasa
  • 1,177
  • 6
  • 17