0

struggling to centre my icon above the text, the height of the icon is fine but it should sit like so:

tried using flex but couldn't find the correct solution.

enter image description here

.container {
  display: block;
  width: 400px;
  height: 100px;
  border: 2px solid black;
}

.icon {
  width: 30px;
  height: 30px;
  background-color: red;
}

.text {
  text-align: center;
}
<div class="container">
  <div class="icon"></div>
    <p class="text">We couldn't find any matches to your search. <br></br>Please try again.</p>
</div>  
isherwood
  • 58,414
  • 16
  • 114
  • 157
cts
  • 908
  • 1
  • 9
  • 30
  • You haven't applied any centering to the icon. Put your text alignment on the parent element instead of just the text. – isherwood Jan 18 '22 at 14:11

1 Answers1

0

You can use CSS flexbox: but remember that the default direction is row. If you switch it to flex-direction: column, then you will achieve what you want:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 400px;
  height: 100px;
  border: 2px solid black;
}

.icon {
  width: 30px;
  height: 30px;
  background-color: red;
}

.text {
  text-align: center;
}
<div class="container">
  <div class="icon"></div>
    <p class="text">We couldn't find any matches to your search. <br />Please try again.</p>
</div>
Terry
  • 63,248
  • 15
  • 96
  • 118