Search code examples
htmlcsscenterelement

Center a text between to floating elements


I am working on a navigation bar for my website. The navigation bar contains a menu icon, a header 'Reviews' and a search icon. The icons need to have a distance of 3% from the border. The problem is that I can't figure it out how I center the text. I think the problem is with the floating elements and I don't know how I center a floating element between to other elements(in this case two icons). I hope someone could provide me with an answer :)

This is the result:

the navigation bar

* {
  padding: 0px;
  margin: 0px;
}

#nav {
  width: 94%;
  background-color: blue;
  margin: 3%;
}

.menuButton {
  float: left;
}

#nav h1 {
  float: left;
}

.searchButton {
  float: right;
}
<!DOCTYPE html>
<html>

<head>
  <title> Reviews </title>
  <link rel="stylesheet" type="text/css" href="main.css">
  <meta charset="utf-8">
</head>

<div id="dropMenu"></div>

<body>
  <div id="nav">
    <img src="menu.jpg" class="menuButton">
    <h1> REVIEWS. </h1>
    <img src="search.jpg" class="searchButton">

  </div>

  <div id="content"></div>

  <div id="footer"></div>
</body>
</html>


Solution

  • Is this output are you expecting

    Check output in codepen.io

    * {
      padding: 0;
      margin: 0;
    }
    
    #nav {
      width: 100%;
      background-color: blue;
      margin: 3%;
    }
    
    .menuButton {
      float: left;
    }
    
    
    
    .searchButton {
      float: right;
    }
    
    .a {
      text-align: center;
    }
    <head>
      <title> Reviews </title>
      <link rel="stylesheet" type="text/css" href="main.css">
      <meta charset="utf-8">
    </head>
    
    
    <div id="dropMenu">
    
    
    
    </div>
    
    <body>
    
      <div id="nav">
        <img src="menu.jpg" class="menuButton">
        <h1 class="a"> REVIEWS. </h1>
        <img src="search.jpg" class="searchButton">
    
    
      </div>
    
      <div id="content">
    
    
    
      </div>
    
      <div id="footer">
    
    
    
      </div>
    
    
    </body>