Struggling to align a search box with other items in a custom navbar using Bootstrap 4?

I created a unique navigation bar using Bootstrap 4's grid system. The navbar is designed with two sections: one for the navigation items on the left and another for the search box on the right. Unfortunately, I am facing an issue where the search box keeps getting placed below the navigation items instead of staying in its designated section.

Below is the HTML & CSS code I used:

.custom-navbar-buttons p {
  display: inline;
  color: black;
  margin-right: 0.4rem;
}

.custom-navbar-buttons {
  padding-top: 0.4rem;
  padding-bottom: 0.4rem;
}
<body>
  <div class="container">
    <div class="row">
      <div class="custom-navbar col-12 bg-success">
        <div class="custom-navbar-buttons col-8">
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
        </div>
        <div class="custom-navbar-search col-4">
          <input type="text" name="search" placeholder="search this site">
        </div>
      </div>
    </div>
  </div>
</body>

Does anyone know how to correctly position the search box on the same line as the navigation items within the navbar?

P.S.: There are no tags surrounding the "input" element because I have already experimented with adding and removing them without success.

P.P.S.: I have already searched through Google and Stackoverflow for solutions before reaching out for help. Unfortunately, none of the suggestions provided solved my issue. Some have mentioned that too many navigation items could be causing the problem, but even with just 2 items in the navbar, the search box still doesn't align properly.

Answer №1

Does this meet your needs?

To align the searchbar to the right, you can include:

class="float-right"

.custom-navbar-buttons p {
  display: inline;
  color: black;
  margin-right: 0.4rem;
}

.custom-navbar-buttons {
  padding-top: 0.4rem;
  padding-bottom: 0.4rem;
}
<body>
  <div class="container">
    <div class="row">
      <div class="custom-navbar col-12 bg-success">
        <div class="custom-navbar-buttons col-8">
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
        
          <input type="text" name="search" placeholder="search this site">
        </div>
      </div>
    </div>
  </div>
</body>

Answer №2

When dealing with the positioning of elements in your custom navigation bar, it's important to note that both the custom-navbar-buttons and custom-navbar-search are block level elements. This causes them to stack on top of each other within their parent div (custom-navbar) and take up the entire width.

To resolve this issue, you should set both elements to display inline.

.custom-navbar-buttons p {
  display: inline;
  color: black;
  margin-right: 0.4rem;
}

.custom-navbar-buttons {
  padding-top: 0.4rem;
  padding-bottom: 0.4rem;
  display:inline-block;
}

.custom-navbar-search {
  display: inline-block;
}
<body>
  <div class="container">
    <div class="row">
      <div class="custom-navbar col-12 bg-success">
        <div class="custom-navbar-buttons col-8">
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
        </div>
        <div class="custom-navbar-search col-4">
          <input type="text" name="search" placeholder="search this site">
        </div>
      </div>
    </div>
  </div>
</body>

Alternatively, you can utilize flexbox for better control over your navbar layout.

.custom-navbar {
  display:flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: space-between;
 }
 
.custom-navbar-buttons p {
  display: inline;
  color: black;
  margin-right: 0.4rem;
}

.custom-navbar-buttons {
  padding-top: 0.4rem;
  padding-bottom: 0.4rem;
}
<body>
  <div class="container">
    <div class="row">
      <div class="custom-navbar col-12 bg-success">
        <div class="custom-navbar-buttons col-8">
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
          <p>Button</p>
        </div>
        <div class="custom-navbar-search col-4">
          <input type="text" name="search" placeholder="search this site">
        </div>
      </div>
    </div>
  </div>
</body>

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

Ways to align the contents of a div in the center

Looking at the content in this div, it seems that the top is positioned higher than the bottom. Can anyone provide some guidance on how to fix this? .dtestContactDet{ border-bottom:1px solid #D7D7DE; border-bottom:1px solid #D7D7DE ; -khtml-border-bo ...

Setting up the file structure for customizing Bootstrap with Sass

Hello, I am currently setting up Bootstrap for customization using Sass. After creating an HTML page, I attempted to add my own custom properties to the custom.scss file. Unfortunately, these changes do not seem to affect my page as expected. I followed s ...

Scraping a website where the page source doesn't match what is displayed on the browser

Imagine I'm interested in extracting marathon running time data from a specific website: Upon inspecting the webpage using Google Chrome, I noticed that the desired data is not directly visible in the page source. Although I have successfully scraped ...

Ways to adjust the number of columns in a div using Bootstrap for varying screen sizes of larger devices

https://i.sstatic.net/1nHlm.pngI am new to coding and exploring html, css, and bootstrap. Is it possible to assign varying numbers of columns to a div based on different resolution ranges for larger devices? Specifically looking at the "lg" range which co ...

Tips for choosing a specific cell in a table

I currently have a total of 14 cells that have been generated. Is there a way for me to individually select and manipulate a specific cell out of the 14? I am looking to make only one cell unique, while leaving the rest to be displayed as they are in the ...

Creating tilted divs with dynamic height to perfectly fit the content

I'm struggling to incorporate this design into my webpage; I can't seem to get the right div's height to match the left div as depicted in the second image. Can someone offer some assistance? Additionally, when viewed on mobile, the squares ...

positioning a div based on the location of another div

I am currently working on aligning a div that sits at the bottom of all other divs. The last div is set to float and aligned at the top using CSS. I am unsure how to align it from the left side without any issues when the screen resolution changes. Is th ...

Ways to adjust image placement and size post-rotation with CSS/JS to ensure it stays within the containing div and avoids being cut off

Check out this jsfiddle I've been experimenting with. The jsfiddle is designed to rotate an image by 90 degrees in either clockwise or anti-clockwise direction upon button click. However, the rotated image currently appears outside of its parent div. ...

Comparing the use of visibility: hidden with -webkit-transform: translate3d() within a PhoneGap application

Currently, I am creating a hybrid application using PhoneGap. As part of the design, I have several divs (each representing a page) that I toggle between by changing their visibility in CSS using "visibility: hidden" and "visible". However, I recently ca ...

Verify whether the chosen options from a dropdown list coincide with a predetermined value

I am working with a form that generates dropdown lists dynamically. <c:forEach var="companyList" items="${company.detailList}" varStatus="status"> <tr> <td>c:out value="${companyList.employeeName}" /></td> <td> <select ...

How to show a picture stored in a MySQL database using PHP

I have been experimenting with displaying images uploaded to the database using a simple insert form. So far, I have managed to retrieve the image name in the "data-file". However, I am curious about how I can easily display the actual image in the same lo ...

Automatically substitute a term with a clickable link

Is there a way to automatically turn every word into a hyperlink? I have specific words that need to be linked. For example, I want Ronaldo (Only for the First Appearance) to link to a certain page. However, my attempted method did not work. <p> Ro ...

Revamp the angular design of the mat-tree UI bottom border line issue

Can you help me with changing the direction of the mat tree from right to left? I need to remove the bottom border, please refer to the image https://i.sstatic.net/ecRIO.png here ...

Utilize CSS styling on dynamically loaded HTML content

I am working on a project similar to the StackOverflow editor. I am fetching markdown text, converting it to HTML, and then displaying it in a preview area. However, when I try to style the injected HTML elements using CSS, the styles are being ignored. Up ...

Convert an HTML table into an Excel file using PHP

After successfully generating DATA from PHP into an HTML table format, I utilized the code below to export it to EXCEL: header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="BASIC_Data.xls"&apos ...

What is the best way to adjust the size of a column when hovering

What I am attempting to achieve is, I would like the column box to have a 100% width, and when it is hovered over, I want it to transition to the left, with the width of the column box being about 60%, revealing a second block that shows up as 20% width o ...

Tips on eliminating certain text from a hyperlink

I need assistance with removing the text  Title from my link while preserving the image. <tr id="group0"> <td colspan="100" nowrap="" class="ms-gb"> <a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...

What could be causing my HTML table to stretch all the way to the bottom and right?

https://i.sstatic.net/SpG52.png The layout appears to be extending to the right and bottom, despite my attempts to fix it. I am utilizing the Bootstrap table class in my code. Here is the code snippet: HTML: <head> <link rel="stylesheet" ...

Having trouble with Isotope masonry functionality

While experimenting with Isotope from , I tested the reLayout method using the provided example found at . I copied the css and js to my page () but encountered an issue where clicking on the first element caused all other elements to move to the first col ...