skip to Main Content

I am trying to make a webpage for practice and I am having problem with the search bar. It is not displaying the search bar to the right side of the page in the header. I have made also made the site responsive so there is no issue with the media queries.

function toggleSidebar() {
  var sidebar = document.querySelector('.sidebar');
  sidebar.classList.toggle('sidebar-collapsed');
}
/* General styles */

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

header {
  background-color: #e9e9e9;
  color: #fff;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.toggle-search-bar {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font-size: 24px;
  color: #333;
  margin-right: 10px;
}

.search-bar {
  display: flex;
  align-items: center;
}

.search-bar input[type="text"] {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.search-bar button {
  background: none;
  border: none;
  padding: 10px;
  cursor: pointer;
  font-size: 18px;
  color: #333;
  height: 100%;
}

.search-bar button i {
  margin-right: 5px;
}


/* Sidebar styles */

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 200px;
  height: 100%;
  background-color: #333;
  color: #fff;
  transition: width 0.3s ease;
  overflow: hidden;
}

.sidebar-collapsed {
  width: 0;
}

.sidebar nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: block;
  transition: opacity 0.3s ease;
  opacity: 1;
}

.nav-collapsed {
  opacity: 0;
  pointer-events: none;
}

.sidebar nav ul li {
  margin-bottom: 10px;
}

.sidebar nav ul li a {
  color: #fff;
  text-decoration: none;
}

.sidebar-toggle {
  display: none;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font-size: 24px;
  color: #fff;
  margin-right: 10px;
}

.sidebar-collapse {
  display: none;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font-size: 24px;
  color: #fff;
  position: absolute;
  top: 10px;
  right: 10px;
}


/* Content styles */

.content {
  margin-left: 200px;
  padding: 20px;
  text-align: left;
}


/* Media query for small devices */

@media screen and (max-width: 480px) {
  .toggle-search-bar {
    justify-content: flex-start;
    margin-left: 10px;
  }
  .search-bar {
    order: 1;
    margin-left: 10px;
  }
  .sidebar {
    width: 0;
  }
  .sidebar-collapsed {
    width: 200px;
  }
  .sidebar-toggle {
    display: block;
  }
  .sidebar-collapse {
    display: block;
  }
  .content {
    margin-left: 0;
  }
}


/* Media query for larger devices */

@media screen and (min-width: 481px) {
  .toggle-search-bar {
    justify-content: flex-end;
  }
  .search-bar {
    order: 2;
    margin-right: 10px;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<header>
  <div class="toggle-search-bar">
    <button class="sidebar-toggle" onclick="toggleSidebar()"><i class="fa fa-bars"></i></button>
    <div class="search-bar">
      <input type="text" placeholder="Search">
      <button type="submit"><i class="fa fa-search"></i></button>
    </div>
  </div>
</header>

<div class="sidebar">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
    <button class="sidebar-collapse" onclick="toggleSidebar()"><i class="fa fa-times"></i></button>
  </nav>
</div>

<div class="content">
  <section>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eu arcu sit amet nisl tincidunt consequat id eu lacus.</p>
  </section>
</div>

2

Answers


  1. Chosen as BEST ANSWER
    <header>
        <div class="toggle-search-bar">
          <button class="sidebar-toggle" onclick="toggleSidebar()"><i class="fa fa-bars"></i></button>
          <div class="search-bar">
            <input type="text" placeholder="Search">
            <button type="submit"><i class="fa fa-search"></i></button>
          </div>
        </div>
      </header>
    

    removed the div tag class name search-bar outside of the sidebar-toggle and placed outside the div tag

    <header>
        <div class="toggle-search-bar">
          <button class="sidebar-toggle" onclick="toggleSidebar()"><i class="fa fa-bars"></i></button>
        </div>
    <div class="search-bar">
            <input type="text" placeholder="Search">
            <button type="submit"><i class="fa fa-search"></i></button>
          </div>
      </header>
    

  2. Your trouble came from the flex usage. When a parent container have flex, childs are not automatically expand full-width anymore as a any block element will usually do.

    Instead, you have to specify that a given item in a flex container should expand (or shrink).

    I have fixed your code by removing useless .toggle-search-bar (element and style) and adding two rules to .search-bar to tell browser to expand it and align content to the right.

    function toggleSidebar() {
          var sidebar = document.querySelector('.sidebar');
          sidebar.classList.toggle('sidebar-collapsed');
        }
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }
    
    header {
      background-color: #e9e9e9;
      color: #fff;
      padding: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    .sidebar-toggle {
      background: none;
      border: none;
      padding: 0;
      cursor: pointer;
      font-size: 24px;
      color: #333;
      margin-right: 10px;
    }
    
    .search-bar {
      display: flex;
      align-items: center;
      flex: 1;
      justify-content: right;
    }
    
    .search-bar input[type="text"] {
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    
    .search-bar button {
      background: none;
      border: none;
      padding: 10px;
      cursor: pointer;
      font-size: 18px;
      color: #333;
      height: 100%;
    }
    
    .search-bar button i {
      margin-right: 5px;
    }
    
    /* Sidebar styles */
    .sidebar {
      position: fixed;
      top: 0;
      left: 0;
      width: 200px;
      height: 100%;
      background-color: #333;
      color: #fff;
      transition: width 0.3s ease;
      overflow: hidden;
    }
    
    .sidebar-collapsed {
      width: 0;
    }
    
    .sidebar nav ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      display: block;
      transition: opacity 0.3s ease;
      opacity: 1;
    }
    
    .nav-collapsed {
      opacity: 0;
      pointer-events: none;
    }
    <!DOCTYPE html>
    <html>
    <head>
      <title>Responsive Website</title>
      <link rel="stylesheet" type="text/css" href="styles.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
      <header>
        <button class="sidebar-toggle" onclick="toggleSidebar()"><i class="fa fa-bars"></i></button>
        <div class="search-bar">
          <input type="text" placeholder="Search">
          <button type="submit"><i class="fa fa-search"></i></button>
        </div>
      </header>
    
      <div class="sidebar">
        <nav>
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
          <button class="sidebar-collapse" onclick="toggleSidebar()"><i class="fa fa-times"></i></button>
        </nav>
      </div>
    
      <div class="content">
        <section>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eu arcu sit amet nisl tincidunt consequat id eu lacus.</p>
        </section>
      </div>
     </body>
     </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search