-1

How to vertically center span with float:right in div?

<div class="card">
  <div class="card-header">
    <h5>TEST</h5>
    <span class="fa fa-info"></span>
  </div>
  <div class="card-body">
    <div class="card-text">
      TEXT<br />
      TEXT<br />
      TEXT<br />
      TEXT<br />
    </div>
  </div>
</div>

.card {
  width: 200px;  
}

.card-header h5 {
  display: inline;
}

.card-header span {
  float: right;
}

In this example fa icon in span is align to top.

How can I vertically align this span?

jsfiddle: http://jsfiddle.net/zj3cubtd/

tinex
  • 1
  • 1
  • If you know you ever gonna use only one H5 line, you can simply add line-height to the element: .card-header span { line-height: 1.6875; }. – Mirous Sep 20 '18 at 19:27

2 Answers2

1

This is a good use case for flexbox. Add the following css and it should work as expected:

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-header h5 {
  margin: 0; /* It doesn't need 'display: inline'. Just get rid of its margin */
}
Christopher Bradshaw
  • 2,615
  • 4
  • 24
  • 38
-2

Hope this will work.

.card {
  width: 200px;  
}

.card-header h5 {
  display: inline;
}

.card-header span {
  float: right;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
<div class="card">
  <div class="card-header">
    <h5>TEST</h5>
    <span class="fa fa-info"></span>
  </div>
  <div class="card-body">
    <div class="card-text">
      TEXT<br />
      TEXT<br />
      TEXT<br />
      TEXT<br />
    </div>
  </div>
</div>
Ankit Jaiswal
  • 274
  • 2
  • 8