gap above the first box when trying to align two boxes in sequence using display: inline-block;

Viewed 34

I am trying to make those boxes sit next to each other, but for some reason, it creates a huge gap above the blue box.

Also, how would you remove the gap between the blue and the green box?

Thank you in advance :)

body,
header,
h1,
div {
  margin: 0;
  padding: 0;
}

header {
  width: 100%;
  height:100px;
  background-color: red;
}

h1 {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: blue;
}

div {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: green;
}
<body>
  <header>
    <h1>text</h1>
    <div></div>
  </header>
</body>

1 Answers

Add vertical-align: middle; to h1 and div. I used float to remove the gap between the blue and the green box.

body,
header,
h1,
div {
  margin: 0;
  padding: 0;
}

header {
  width: 100%;
  height: 100px;
  background-color: red;
}

h1 {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: blue;
  vertical-align: middle;
  float: left;
}

div {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: green;
  vertical-align: middle;
  clear:left;
}
<header>
  <h1>text</h1>
  <div></div>
</header>

Also you can use flex for header

body,
header,
h1,
div {
  margin: 0;
  padding: 0;
}

header {
  width: 100%;
  height: 100px;
  background-color: red;
  display:flex;
}

h1 {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: blue;
}

div {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: green;
}
<header>
  <h1>text</h1>
  <div></div>
</header>

Related