-1

How do I vertically center an img and an h1 in a div? Right now I have a png and an h1 in a header but the content is automatically in the top left. How do I put it in the middle and keep it responsive? Here's my HTML:

header {
  text-align: center;
  max-height:680px;

}
<header>
  <h1 id="title">Lorem Ipsum</h1>
  <img id="arrows" src="images/arrows.png">
  <h3 id="sub-title">Lorem ipsum</h3>
</header>

edit: Sorry I should have mentioned that the header has a specific height. How do I put the text and image right in the middle of the header, both vertically and horizontally?

pat
  • 117
  • 1
  • 1
  • 10

3 Answers3

2

I know two ways to align it vertically.

First way: using table

header {
    text-align: center;
    display: table-cell;
    vertical-align: middle;
}
.wrapper {
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
}
<div class="wrapper">
    <header>
         <h1 id="title">Lorem Ipsum</h1>
         <img id="arrows" src="images/arrows.png">
         <h3 id="sub-title">Lorem ipsum</h3>
    </header>
</div>

Second way: using relative position

body, html {
    height: 100%;
}
div.wrapper {
    height: 100%;
}
header {
    text-align: center;
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}
<div class="wrapper">
    <header>
         <h1 id="title">Lorem Ipsum</h1>
         <img id="arrows" src="images/arrows.png">
         <h3 id="sub-title">Lorem ipsum</h3>
    </header>
</div>
aleksandar
  • 2,293
  • 2
  • 11
  • 22
0

If you want them in a line and centered:

header{
  text-align: center;
}

h1, h3, img{
  display: inline-block;
  margin: 0 10px;
}
Paul Redmond
  • 3,101
  • 1
  • 24
  • 50
0

Centering it horizontally and vertically using Flexbox.

html,
body {
  height: 100%;
}
.flex-wrap {
  display: flex;
  justify-content: center;
  height: 100%;
}
header {
  align-self: center;
}
<div class="flex-wrap">
  <header>
    <h1 id="title">Lorem Ipsum</h1>
    <img id="arrows" src="http://placehold.it/300x300">
    <h3 id="sub-title">Lorem ipsum</h3>
  </header>
</div>
m4n0
  • 25,434
  • 12
  • 57
  • 77