I'm having trouble getting my dropdown content to align properly with my centered search bar. Any suggestions on how to fix this issue?

How can I align the dropdown content to the center in its td element? I haven't included all of my code here, so feel free to ask if you need more information. The search bar is supposed to be at the center of this table navbar.

This is the HTML:

<table class="navbar-table">
    <tr>
      <td style= "margin: 0px 0px; padding: 0px 0px; width:33.33%; float: left;">
        <div class="dropdown">
          <span class="dropbtn">&#9776;  Menu</span>
          <div class="dropdown-content">
            <a href="../index.html">Home</a>
            <a href="../leaderboard.html">Leaderboard</a>
            <a href="../about_us.html">About Us</a>
            <a class="active" href="slide1.html">Slides</a>
            <a style=" display: none;" id= "profile-button-text" href= "../profile/achievements.html" >Profile</a>
            <a style= "background-color: #ab727a; display: none;" id= "logout-button-text">Log Out</a>
          </div>
        </div>
      </td>

        <td class= "search_bar_td">
          <div class="dropdown-search">
            <input autocomplete="off" class= "search_bar" name="search" placeholder="Search..">
            <div class="dropdown-search-content">
              <a href="../index.html">Home</a>
              <a href="../leaderboard.html">Leaderboard</a>
              <a href="../about_us.html">About Us</a>
              <a class="active" href="slide1.html">Slides</a>
              <a style=" display: none;" id= "profile-button-text" href= "../profile/achievements.html" >Profile</a>
              <a style= "background-color: #ab727a; display: none;" id= "logout-button-text">Log Out</a>
            </div>
          </div>
        </td>

      <td style= "margin: 0px 0px; padding: 0px 0px; width:33.33%;">
        <ul style= "margin: 0px 0px; padding: 0px 0px;">
          <li class="navbar-logsin" id= "signup-button" style=" background-color: #72ab99">
            <a class= "topnav_a" id= "signup-button-text">Sign Up</a>
          </li>
          <li class="navbar-logsin" id= "login-button" style=" background-color: #73a872">
            <a class= "topnav_a" id= "login-button-text">Login</a>
          </li>
        </ul>
      </td>
    </tr>
</table>

This is the CSS:

.search_bar {
  width: 240px;
  float: none;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  padding: 6px 10px 6px 30px;
  margin: 0 auto;
  outline: none;
}

.search_bar_td {
   float: center; 
   width: 33.33%;
   margin: 6px 0px;
   padding: 0px 0px;
}

.dropdown-search {
  width:100%;
  margin:0 auto;
  padding: 0px 0px;
  float: center;
  display:block;
  color: #f2f2f2;
  text-align: center;
  text-decoration: none;
}

.dropdown-search-content {
  display: block;
  position: absolute;
  background-color: #f9f9f9;
  height: 0px;
  width: 0px;
  max-height:150px;
  overflow-y:scroll;
  z-index: 1;
  float: center;
  margin: 6px 0px;
}

.search_bar:focus + .dropdown-search-content {
  height: auto;
  width: 240px;
}

Answer №1

According to @cloned, using tables is unnecessary. It would be better to utilize flexbox for designing a navigation bar with a search input that is center-aligned. To address the dropdown issue, simply position the search dropdown absolutely relative to the parent wrapper.

nav {
 display: flex;
 justify-content: space-between;
 align-items: center;
}
 nav > ul {
 display: flex;
 align-items: center;
 list-style: none;
 width: 70%;
 justify-content: space-around;
 margin-left: 0px;
 padding-left: 0px;
}
 nav .search {
 width: 30%;
   position: relative;
}

.search ul {
  list-style: none;
  position: absolute;
  left: 0;
  padding-left: 0px;
}
<nav>
  <ul>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li>
    <li><a href="#">Item 4</a></li>
    <li><a href="#">Item 5</a></li>
    <li><a href="#">Item 6</a></li>
  </ul>
  <div class="search">
    <input type="text" name="" id="searc-bar">
    <ul>
       <li><a href="#">Item 1</a></li>
       <li><a href="#">Item 2</a></li>
       <li><a href="#">Item 3</a></li>
       <li><a href="#">Item 4</a></li>
    </ul>
  </div>
</nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing User Experience with Animated jQuery Progress Bar

I came across a cool tutorial on animating progress bars: The only issue was that the progress bar didn't utilize jquery, and there wasn't information on linking multiple buttons to it. After some searching, I found another tutorial that address ...

The functionality of jquery .serialize is not effective when used on forms that are generated dynamically

Currently, I am utilizing Java/Spring within a .jsp file. The issue arises when attempting to dynamically load a form (including form tags) from myForm.jsp into a div in myPage.jsp and making an ajax post call with the data. Upon using jQuery .serialize a ...

Using Linux to send beautifully formatted HTML emails

I am currently working on a Python 2.2 script that generates an HTML string by adding various elements to it. Here is a snippet of the code: html_string = "" html_string = html_string + "MIME-Version: 1.0\n" html_string = html_string + "Content-type: ...

Ways to change specific CSS class properties in a unique style

As a Java programmer using primefaces 5.1, I've encountered some challenges with handling CSS classes. One particular issue I'm facing is the need to override certain CSS properties to remove the rounded corners of a DIV element. The rendered cod ...

Creating a New Section in your HTML Table

Can a page break be implemented in an HTML table? I've attempted to add page breaks to the following HTML: <html> <title>testfile</title> <meta http-equiv="expires" content="0" /> <meta http-equiv="cache-contr ...

Enhancing Title and Meta Tags with Decorative Styles

Looking to style the <title><p>AAA</p></title> and <meta name="decrition" content="AAA">. But when I added <p> and applied CSS with: p{color:red;size:1000px} The title is being displayed as <p>AAA</p> Any ...

Are "Color Scheme" and "Color Palette" distinct from each other?

What is the difference between a color scheme and a color palette? In one of my projects, which is a web template, there are options for customers to customize. However, I find myself confused with the naming conventions for two color-related options. Op ...

Ensure YouTube iframe remains visible above all other elements on mobile devices at all times

Currently, my method involves utilizing YouTube for embedding videos and incorporating certain elements that should display on top of the video when clicked on. Regrettably, this functionality operates flawlessly on desktop browsers but encounters issues ...

What is the best way to show emojis in AngularJS?

Recently starting to work with angularjs, I've incorporated "emoji-min.js" and "emoji-min.css" into my project as an attempt to display emojis within a text field. Despite my efforts, the emojis are not displaying as intended. Here is a snippet of my ...

Protect your website's ajax-generated content from being indexed by search engines with these strategies

Recently, Google made an update allowing its crawler to index ajax-generated content on web pages by following specific guidelines. However, my requirement is to ensure that no search engine can crawl my ajax-generated content. So, my question is: What ...

Set the search input to occupy the full width of the container by utilizing Bootstrap's

I'm having trouble adjusting the width of my search input to 100% on screen resize (using @media (max-width: 1200px)). The issue: https://i.sstatic.net/EMr6W.jpg Here's how it should look (I can achieve this with a specific pixel width, but not ...

Is there a way to modify the size and color of a specific word in a CSS animation?

As a newcomer to CSS, I am exploring ways to change the font size and color of a specific word in my sentence after an animation. My initial attempt was to enclose the word within a span class like this: <h2>I'm <span style = "font-size: ...

Why is the last move of the previous player not displayed in this game of tic tac toe?

Why does the last move of the previous player not show up in this tic-tac-toe game? When it's the final turn, the square remains empty with neither an "O" nor an "X". What could be causing this issue? Here is the code snippet: The HTML code: <div ...

Encountering an 'undefined' error while attempting to showcase predefined JSON data on an HTML webpage

As I try to display the predefined JSON data from https://www.reddit.com/r/science.json on an HTML page, I keep encountering the message "undefined." Below is a snippet of my HTML code: $.getJSON('https://www.reddit.com/r/science.json', funct ...

Clickable link unresponsive on parallax-enhanced webpage

Currently, I am utilizing Zurb foundation's Manifesto theme for creating a parallax scrolling landing page. The anchor tag is essential for the scrolling effect on this page, causing a conflict when regular anchor links are included. Here is the HTML ...

Is there a quick way to retrieve the IDs of all the checked boxes on a displayed database in QUICKFIX?

I have created a display of data from my database in the form of a table with checkboxes on the left. My goal is to link these checkboxes to the question number (ID) so that when someone selects certain questions and submits, the selected IDs will be echoe ...

Use CSS alignment to combine both the profile picture and title on a webpage

Is there a way to seamlessly integrate a profile picture with text (title) on my tumblr theme? My main challenge lies in getting the alignment right. Additionally, I want it to look good in responsive view. If the title becomes too long, it should wrap un ...

What is the best way to make a black background div that perfectly aligns with another div?

When hovering over an image, I want it to fade out to opacity: 0.4, but instead of fading to white, I would like it to fade to black. To achieve this, I decided to change the background color of the body to black so that the fadeout will be towards black. ...

Align the responsive image in the center of a container

I am facing a challenge in centering an image wrapped by an anchor tag inside a div. I have tried using display flexbox and justify-content:center for the image, which works to some extent. However, the height of the image remains constant while the width ...

Is Javascript necessary for submitting a form to a PHP script?

In my current project, I am working on a page to edit information in a database. While I know how to create the form in HTML and the PHP script that runs on the server, I am facing a challenge with JavaScript. My understanding of JavaScript is limited, and ...