Struggling to properly position a search input and submit button in a navbar on smaller screens

While creating a search view for a website, I noticed that the search box and submit button placed next to it (aligned to the right at most screen widths) would become misaligned in very small max-widths.

Using Bootstrap 4, I utilized ".ml-auto" to align these elements to the left of the navbar. It worked fine except for this particular case (as I understand that elements in navbars behave differently in Bootstrap). The CSS and JS files are imported correctly (I double-checked before writing this). Here are snippets of the code displaying the issue:

  • HTML

      <div class="container">
      <header id="head">
          <h1 class="page-header float-left">bitDump</h1>
          <div class="clearfix"></div>
      </header>
      <section id="mainBody">
          <nav class="navbar navbar-expand-sm navbar-dark navbar-tweak">
              <form class="form-inline my-2 my-md-0 ml-auto">
                  <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                  <button class="btn btn-outline-success my-2 my-sm-0 ml-auto" type="submit">O</button>
              </form>
          </nav>
      </section>
    
  • Custom CSS (complementing Bootstrap's)

* {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: #000;
}

a:hover {
  text-decoration: none;
  color: #000;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: #ddd;
  font-weight: normal;
} 

.navbar-tweak {
    border-radius: 5px;
    background-color: #6c757d;
    border-color: #5a6268;
    transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.page-header {
  padding-bottom: 5px;
  margin: 20px 0 10px;
  border-bottom: 1px solid #eee;
}

The misalignment can be viewed on , specifically on vertical phone resolution (approximately 360px wide).

Any suggestions on how to rectify this issue?

Answer №1

To create a more responsive design, you can include the following CSS:

@media (max-width: 480px){
  .form-control{
    width: auto;
  }
}

Instead of using width: 100% which causes the input to take up all available space, this new style allows for better spacing and alignment of elements on smaller screens.

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

Issue with nested tabs functionality within a pill tab panel in Bootstrap not functioning as expected

I'm facing a challenge with integrating tabs into a vertical pill tab panel in Bootstrap 4. Below is a brief code snippet: <html> <head> <link href="bootstrap.min.css" rel="stylesheet"> </head> <body> <script ...

Leveraging the power of Bootstrap 4 variables within my project's code

Is it possible to incorporate Bootstrap 4 variables into custom CSS rules, such as $font-family-serif? My approach involves using Webpack to import Bootstrap variables and functions into my scss file: @import "~bootstrap/scss/functions"; @import "~bootstr ...

The rotation feature for legends in Highcharts does not function properly on Internet Explorer 10 and 11 if the document mode is

When using Internet Explorer 10 or 11, rotation works perfectly fine if we select edge mode, as shown in the image. This information was taken from the High Chart Demo Charts. However, if we select Document mode 8 in IE 10 or IE 11, the rotation does not ...

What's causing my Bootstrap cards to overlap on smaller screens?

When using 3 bootstrap cards in the following code, they are perfectly displayed side by side on large screens. However, on medium screens, the cards overlap, and on small screens, they completely overlap. I tried adjusting the class="col-lg-4 col-md- ...

Exploring the varying heights of select options in HTML5 and Bootstrap Select

I am encountering an issue with the height of a button element generated by Bootstrap Select within an HTML select element. When using HTML5 and including the declaration as the first line, the button renders correctly with a height of 24. However, when ...

The function toggleClass() is malfunctioning

The .toggleClass() method seems to only be adding the "class" attribute to my selection, resulting in: <div id="toggle" class> ... rather than: <div id="toggle" class="on"> ... I've been trying to solve this issue for about 2 hours now ...

What is the best way to manage the delay that occurs when using the append() function?

I'm using jQuery to append an array to a div and I want to display a loading indicator while this process is happening. I added a callback function to my append logic, which works fine. However, the loader disappears before the array is fully rendered ...

"CSS styles applied to the body element do not automatically transfer to the h1

Explore the World of Coding: <body class="pageDesign"> <h1>CSS - A Creative Journey</h1> <p><strong>Discover the beauty of coding</strong> and unlock a world of possibilities with CSS. Let your imagination soar as you d ...

Animating the left and right positioning of a single element using CSS transitions

I am currently working with the following CSS code: #masthead { transition: left linear 0.25s; -moz-transition: left linear 0.25s; -o-transition: left linear 0.25s; -webkit-transition: left linear 0.25s; -ms-transition: left linear 0.2 ...

All elements on my webpage are enhanced with a CSS filter

I have a button centered on my page, but the CSS filter I applied to the HTML tag is also affecting the button. How can I prevent this from happening? HTML <body> <div class="button"> <a href="#">START EXPERIENCE</a> ...

What is the best way to update the video source upon clicking a link with JQuery?

Is there a way to change the video src using jQuery when a link is clicked? For example, if the user clicks on "#staff", the video src will be updated. <ul class="nav nav-pills" > <li role="presentation" class="loginPills"><a ...

Update the select tag to display as radio buttons

Hey there, I've got a webpage where users can rate different systems. I've written a JavaScript function to dynamically add and remove systems for evaluation. The only issue is that I have to manually change the radio button names to prevent th ...

Utilizing the float-left class in Bootstrap 4 on a div cancels out the link between <a><img></a> elements

Strange scenario... when using this code, a picture appears with the related text BELOW it. It's crucial that the picture remains clickable, but the text isn't wrapping as desired (see below): <div class ...

What is the best way to position a div in relation to a table header (th) but not inside it?

I'm trying to create a table header that displays a help text (div) when clicked on. However, the help div is causing the table header to be larger than the other headers. Below is the code snippet: #help_box { border-radius: 20px; ba ...

Having issues with image hover and click functionality

My website has an image with the cursor set to pointer and a function that should show a hidden element when clicked. However, it's not working at all. When I hover over it or click on it, nothing happens. Here is a screenshot of the issue along with ...

My CSS nesting seems to be posing a problem - any idea what

Something unexpected happened while I was working on a project with others. Last night, some files were edited and when I updated my subversion today, the nested divs seemed to have lost their hierarchy. I have been trying all day to fix this issue but wit ...

The specified number of columns and width values are being disregarded

The Challenge While using the CSS column-property to create columns on my webpage, I have encountered some unusual behavior with the layout engine. Specifically, I have set up the following styling: body { padding: 0; background-color: black; margi ...

Utilizing Bootstrap Columns for Image Alignment: Struggling to Position an Image in the Center

For my latest project, I used Bootstrap to create a template that adapts well to different device breakpoints. Everything is working smoothly except for the positioning of the arrows on the sides of the image. I want them to be centered halfway down the ...

Issue arises when applying both overflow-x:scroll and justify-content:center

Encountering a problem with using overflow-x: scroll and justify-content: center on a flex parent container. Here is my code snippet: The issue: the first flex child item is not visible as it is cropped on the left side. Please refer to the screenshot and ...

Show a picture upon hovering the button

Welcome to my website. My goal: Show an image when a user hovers over the links. I must be making some silly error, but I can't pinpoint it. This is what I've attempted so far: HTML <ul class="nm"> <li><a href="#">Cork& ...